Applying inheritance in C++ programs

Applying inheritance in C++ programs

In this tutorial you will learn: 1. How to use inheritanace in program? 2. The advantages of inheritance? 3. How base and derived class functions work together? 4. C++ syntax As told before inheritance is very useful in terms of reliability and reuse of the code. In this part of the tutorial I have made a small program to demonstrate how can we make better use of the inheritance by implementing it in some practical situation like account management of some client of a bank. There are two type of accounts in bank, one is the current account and other one is the savings account. The current account only provides the features of credit(adding money) and debit( deducting money) from the account while the saving account also provide users with a feature of interest rate, in this case some amount of money according to the interest percentage is given to the customer above his actual amount(balance). Saving account class is also the current account as we can debit and credit the money from it as well so we can make the current account as the base class while the savings account class as the derived class(because additional functionality is added in the saving account). Here current account is named as “Account” while saving account as “SavingsAccount”. Lets start with the code. strong>Inheritance Basic Step: Open Dev C++ then File > new > source file and start writing the code below.
  1. #include<iostream>
  2. #include<conio.h>
  3. using namespace std;
These are the basic header files which are required for C++ programming. Using namespace std is used at the top of the program because if its not written then we have to write ‘std::’ with every line of the program. #include includes the C++ built in libraries and iostream is used for input and output of the program. Conio.h is used to show output on the screen, if it is not present , the screen flashes and disappears hence not showing output.
  1. class Account
  2. {
  3. private:
  4. double balance;
  5. public:
  6. Account()
  7. {
  8. balance=0;
  9. }
  10. Account(int x)
  11. {
  12. balance=x;
  13. }
  14. void setBalance()
  15. {
  16. cout<<"\nEnter initial balance you have in your account:";
  17. int x;
  18. cin>>x;
  19. balance=x;
  20. }
  21. void credit()
  22. {
  23. cout<<"\nHow much money you want to add in your account:";
  24. int x;
  25. cin>>x;
  26. balance=balance+x;
  27. }
  28. void debit()
  29. { cout<<"\nHow much money you want to deduct from your account:";
  30. int x;
  31. cin>>x;
  32. balance=balance-x;
  33. }
  34. double getBalance()
  35. {
  36. return balance;
  37. }
  38.  
  39. };
The first class in the program we have is the base class named “Account”, as it is the current account so it contains only one data member that is “balance”. The functions of this class are the constructor, debit, credit and getBalance. Debit function adds some amount(provided by the user) to the original balance while the debit function deducts some amount which user want to remove from his account. Both function have the return type void because they are just adding and subtracting value not returning anything, Another function is the getBalance function which returns the amount of balance currently present in the account of the person. Its return type is double(which is that of balance).
  1. class SavingsAccount:public Account
  2. {
  3.  
  4. private:
  5. double interestRate;
  6. public:
  7.  
  8. SavingsAccount()
  9. {
  10. Account::setBalance();
  11. interestRate=0;
  12. }
  13. void setInterest()
  14. { cout<<"\nThe amount of interest you want to imply on the account:";
  15. int y;
  16. cin>>y;
  17. interestRate=y;
  18. }
  19. double calculateInterest()
  20. {
  21. cout<<"\nThe amount of interest in $ is:";
  22. return((interestRate/100)*getBalance());
  23. }
  24. };
In this code we are working on our derived class “SavingsAccount” which is publicly derived from the base class “Accounts”, it has a data member interest rate. The interest is the only additional feature present in the savingsAccount and it also makes savingAccounts different from the current account as also explained before. So in this class we have a constructor(default) which set the interest rate equal to zero and the base class constructor is called using binary scope resolution operator which sets the balance as input by the user. Next we have functions setInterest and calculateInterest in which user inputs interest she would get from the bank and the calculated interest after multiplication is returned respectively. In calaculate interest function we have divided the interest rate by hundred because the user inputs the interest rate in the form of percentage so this conversion is necessary. getBalance function of the base class is used in the calculate interest function because as already told before that the private data members of the base class can only be accessed by the functions of derived class using the public functions of base class.
  1. void main()
  2. {
  3. SavingsAccount s;
  4. s.credit();
  5. s.debit();
  6. s.setInterest();
  7. cout<<s.calculateInterest();
  8. }
In the main of the program we have just made an object of the derived class so that all the aspects of the base class are also covered. Then we called different functions of the base class on the derived class object to demonstrate that derived class object can access function of base class object. Finally we displayed the result after finding the interest on the balance as shown in the output. Basic Step: Open Dev C++ then File > new > source file and start writing the code below. Execute > compile then Execute > run Output Output

Add new comment