Classes in C++

A C++ Class contains:

A sample C++ class can be outlined as:


  // An Example Class
 
  class AnExampleClass 1
  {
    // state definitions 2

    public: 3
	
    // interface declarations 4
	
    private:
	
    // implementation method declarations 5

  }; 6

  // member method implementation 7

1

The class keyword lets the compiler know that a class is about to be defined. The class name follows and should begin with a capital letter.

2

The states are the data values of the class and are usually defined first. Note that variables are defined (not declared). Definition means "make this variable or method" here, allocating storage for the name.

3

The public keyword states that all methods to follow are part of the interface, i.e. publically visible.

4

All methods following the public keyword are part of the interface. i.e. the part of the class that you wish to allow users see outside of the class. Note that the interface methods are declared. Declaration is the introduction of a name (identifier) to the compiler. It notifies the compiler that a method or variable of that name has a certain form, and exists somewhere.

5

All methods following the private keyword are internal methods of the class and are part of the internal implementation.

6

The semi-colon defines the end of the class declaration. If you leave it out your code will not compile, but worse, the error messages you receive will not make it clear that you have forgotten this semi-colon.

7

Once the class is defined you can then write the implementation code for the methods. This keeps the class definition short and easy to read. If you wish, these methods can even be written in a separate file.

Here is an example C++ class of a Bank Account class as shown in Figure 3.1, “A Sample Bank Account Class”, with private notated with "-" and public notated with "+".

Figure 3.1. A Sample Bank Account Class

A Sample Bank Account Class

 1 
 2   // Basic Bank Account Example
 3  
 4   #include<iostream>
 5 
 6   using namespace std;
 7  
 8   class Account{
 9   
10   private:
11 
12     int accountNumber;
13     float balance;
14 
15   public:
16 
17     virtual void display();
18     virtual void makeLodgement(float);
19     virtual void makeWithdrawal(float);
20   };
21 
22   void Account::display(){
23     cout << "account number: " << accountNumber
24       << " has balance: " << balance << " Euro" << endl;
25   }
26 
27   void Account::makeLodgement(float amount){
28     balance = balance + amount;
29   }
30 
31   void Account::makeWithdrawal(float amount){
32     balance = balance - amount;
33   }
34 
35   int main()
36   {
37     Account a;
38     a.display();  //will output rubbish for the states
39   }
40 

The source code for this is in BasicAccount.cpp The Interface:

The code above shows how we can define and implement a class. If we wish to use this class, we can create an object of the class and manipulate it directly, for example:


  int main()
  {
    Account myAccount; 1

    cout << "Account Details:";
    myAccount.display(); 2
	
    myAccount.makeLodgement(2300.00); 3
    cout << "Account Details:";
    myAccount.display();
 
    myAccount.balance = 2300.00; 4
    // ERROR!
	
    cout << "Account Balance:"
      << myAccount.balance << endl; 5
    // ERROR!
  }
  

1

This call creates an object of the Account class called myAccount. The balance and account number are not yet set.

2

The display() method is called directly on the myAccount object. It will display the account details.

3

The makeLodgement() method is called directly on the myAccount object. It will add 2300 Euro to the balance.

4

This call is not allowed. The balance state is a private state of the Account class, so you cannot modify it directly. It is not part of the interface.

5

This call is not allowed. The balance state is not part of the interface and does not even allow the value to be read.

If you wish to assign a new object to an existing reference, even after initialisation use:


  int main()
  {
    Account myAccount; // assigns an object to the reference
    
    myAccount = Account();  // allows the assignment of a new account object
  }