Page 308 - Computer_Science_F5
P. 308

void display () {
                         cout << “accountHolder: “ << accountHolderName << “, Balance: TZS”
                 << balance << endl;
                     }                                                                             Chapter Four: Object oriented programming with C++
          FOR ONLINE READING ONLY
                 };
                 int main () {
                     // Create an object of BankAccount class
                     BankAccount account (“Mkulu Mkuvangwa”, 100000);
                     // Display initial account information
                     cout << “Initial account information:” << endl;
                     account.display ();
                     // Deposit and withdraw some money
                     account.deposit (20000);
                     account.withdraw (15000);
                     // Display updated account information
                     cout << “\nUpdated account information:” << endl;
                     account.display ();
                     return 0;
                 }

                Output:










                                   Figure 4.73: Output of encapsulation program


              This code example  demonstrates the  the  class  itself.  The  member  functions
              concept of encapsulation, which is  are declared  as public,  allowing  them
              one of the pillars  of object-oriented  to be accessed and called from outside
              programming      (OOP).    The    code   the  class.  The  constructor is used to
              defines  a  class  called  BankAccount,  initialize the object with an owner name
              which encapsulates  data members  and initial  balance.  The  deposit  and
              (accountHolder  and balance)  and  withdraw  methods  modify  the  balance
              member  functions  (deposit,  withdraw,  based on the given amount, ensuring that
              and display).  The  data  members  are  the balance is not negative. The display
              declared  as private,  meaning  they can  method is used to print the owner’s name
              only  be  accessed  and  modified  within  and balance.

                                                    299
               Student’s Book  Form Five



     Computer Science Form 5.indd   299                                                     23/07/2024   12:34
   303   304   305   306   307   308   309   310   311   312   313