There is very little difference between pointers to variables as discussed in the section called “Pointers in C/C++” and pointers to objects. First off, we can define a pointer Account anAccount(5.0, 12345), *p; So, pointer If we wish to make the pointer Account anAccount(5.0, 12345), *p; p = &anAccount; // alternative notation would be (just like pointers to variables): Account anAccount(5.0, 12345); Account *p = &anAccount; Previously, several methods were defined for the In the same way that we applied the . operator to objects we can apply the -> operator to pointers to objects. The notation is simply a - followed by a > So for example, to demonstrate the object notation and the pointer notation: //object notation Account anAccount = Account(35.00, 12344); anAccount.makeLodgement(20.0); anAccount.display(); //pointer notation Account testAccount = Account(5.0, 12345); Account *testPtr = &testAccount; testPtr->makeLodgement(20.0); testPtr->display(); A full source code example for this segment is listed in The output from this example would be: account number: 12344 has balance: 55 Euro account number: 12345 has balance: 25 Euro Note that These notes are copyright Dr. Derek Molloy, School of Electronic Engineering, Dublin City University, Ireland 2013-present. Please contact him directly before reproducing any of the content in any way. |