What we have in this page?
Adding the Account Class
The next stage involves creating the Account class, using the Generic C++ Class Wizard in very much the same way.
1. Add a class named Account to the project used in the previous exercise, in exactly the same way as you added the Bank class.
|

The header file named Account.h that looks like this:
#pragma once
ref class Account
{
public:
Account(void);
};
and an implementation file Account.cpp, that looks like this:
#include "StdAfx.h"
#include "Account.h"
Account::Account(void)
{ }
2. Add some structure to the Account class. Accounts will have an account number, a balance, and an overdraft limit, so add three private members to the Account class definition in Account.h, like this:
long accNumber; // the account number
double balance; // the current balance
double limit; // the overdraft limit

3. Edit the constructor definition and implementation as follows so that three values are passed in and used to initialize these three variables. Add the following to the Account.h under the public:
Account(long num, double bal, double lim);

4. And edit the Account.cpp file as shown below:
Account::Account(long num, double bal, double lim)
{
Console::WriteLine(L"Account: constructor");
// Basic sanity check
if (num < 0 || lim < 0)
throw gcnew ArgumentException(L"Bad arguments to constructor");
// Initialize values
accNumber = num;
balance = bal;
limit = lim;
}
![]() |
5. Again, because we are using the Console::WriteLine, we must include the using namespace System;. Add the following after the #include preprocessor directive.
using namespace System;

The basic sanity check simply checks that the account number and overdraft limit aren’t negative, and it throws an ArgumentException if they are.
Creating Account Class Properties
Once the Account class has been constructed, you can add properties to allow access to the three data members. All three members are scalar so the properties are easy to implement.
1. Add a public get method to Account.h to allow read-only access to the account number, as shown here:
property long AccountNumber
{
long get() { return accNumber; }
}
2. You can add the function definition inline in the class definition. Remember to put it in the public section! You also need to add a read-only property for the balance member as shown below because in real life, you don’t want people simply modifying the balances in their accounts from code.
property double Balance
{
double get() { return balance; }
}
3. Add a read/write property for the overdraft limit because it’s quite possible that the limit might get changed from time to time.
property double Limit
{
double get() { return limit; }
void set(double lim)
{
if (lim < 0)
throw gcnew ArgumentException(L"Limit can’t be negative");
limit = lim;
}
}
4. You’ll need to add a using namespace System; line or fully qualify the name of ArgumentException before the code will compile. So add using namespace System; to the Account.h. Our final code is shown below.
#pragma once
using namespace System;
ref class Account
{
long accNumber; // the account number
double balance; // the current balance
double limit; // the overdraft limit
public:
Account(long num, double bal, double lim);
property long AccountNumber
{
long get() { return accNumber; }
}
property double Balance
{
double get() { return balance; }
}
property double Limit
{
double get() { return limit; }
void set(double lim)
{
if (lim < 0)
throw gcnew ArgumentException(L"Limit can’t be negative");
limit = lim;
}
}
};
5. Test out your implementation by adding some code to the main() function in Banker.cpp to create a new Account object and access its properties. Include the Account.h file, #include "Account.h" and then add the following code to create an Account object, like this:
// Create an Account object
Account^ theAccount = gcnew Account(123456, 0.0, 0.0);

6. Build and run your program. The following output should be expected.
