Add member method to change the member field value - C++ Class

C++ examples for Class:Member Field

Description

Add member method to change the member field value

Demo Code

class MyAccount/*  w ww.j a v a 2  s.  c om*/
{
  public:
    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