Defining a Member Function in the Class - C++ Class

C++ examples for Class:Member Function

Description

Defining a Member Function in the Class

Demo Code

class MyAccount { 
  public: //from  w ww.j av  a 2 s. c om
    // define a member function deposit() 
    double deposit(double amount) 
    { 
        balance += amount; 
        return balance; 
    } 

    unsigned int accountNumber; 
    double  balance; 
}; 
#include <cstdio> 
#include <cstdlib> 
#include <iostream> 

using namespace std; 

int main(int nNumberofArgs, char* pszArgs[]) { 
    MyAccount s; 
    s.accountNumber = 123456; 
    s.balance = 0.0; 

    cout << "Depositing 10 to account " << s.accountNumber << endl; 
    s.deposit(10); 
    cout << "Balance is " << s.balance << endl; 

    return 0; 
}

Result


Related Tutorials