Sample Code in C++ to demonstrate C++ Class Inheritance

One of the most useful features of C++ is class Inheritance. Inheritance in C+ Classes allows Software Developers to distribute code in multiple classes so that they can concentrate on the task in hand. This post presents a C++ sample code to demonstrate C++ class inheritance using two classes i.e. a Sim Card Class and another Mobile Phone Class. In case you want to make call from your Mobile Phone having a prepaid SIM Card, you must have sufficient balance to make the phone call. Have a look at the given below SIM Card Class which checks the pre paid balance and in case the SIM Card has sufficient balance , allows the Mobile Phone to make a phone call.

Sample Pre Paid SIM Card Class to teach C++ Inheritance

Sample Pre Paid SIM Card Class to teach C++ Inheritance

Have a close look at the above screenshot displaying a Pre Paid SIM Card Class. Yes in a practical world, the Pre Paid SIM Card class would contain more member functions and more member variables. The actual Pre Paid SIM Card class must inherit with a SM Card class with which the Post Paid SIM Card Class can inherit as well. The above class has a protected member variable depicting the pre paid balance of the SIM card, the constructor of the class initialize the pre paid balance to 10 units and a constant is also defined which assigns the minimum call charge to be 12 units. Finally a public member function is there in the Pre-Paid SIM Card class which returns a bool value confirming whether the call can be made or not.

Sample Mobile Phone Class to teach C++ Inheritance

Sample Mobile Phone Class to teach C++ Inheritance

Now have a look at the above C++ Class depicting a Mobile Phone and note the statement

class CMobile : public CPrePaidSimCard

which indicates that the Mobile Phone class inherits the functionality offered by the Pre Paid SIM Card Class. Once the Mobile Phone class has inherited the Pre Paid SIM card class, it can call the non private functions of the Pre Paid SIM Card Class and access non private member variables of the Pre Paid SIM card Class. In Practical world as well, the Mobile Phone cannot determine whether a phone call can be made or not and instead it relies on the permission of the SIM card and hence the Mobile Network Operator.

Main Function of the C++ Sample Project trying to make a Phone Call

Main Function of the C++ Sample Project trying to make a Phone Call

Finally have a look at the above screenshot and check how the Mobile Phone is trying to make a phone call. Internally the Mobile Phone Class inherits member functions and member variables of the SIM card, checks whether the phone call can be made or not and depending on the confirmation from the SIM card class, either makes a phone call or displays a message that phone call cannot be made due to insufficient funds. Download the full c++ code of this sample project and build the project on your own machine to understand C++ inheritance.

// CInheritance.cpp : Defines the entry point for the console application.// Visit http://www.TapKaa.com/ for more C++ Sample Projects and Tutorials#include "stdafx.h"#include <string>#include <iostream>using namespace std;#define MIN_CALL_CHARGES 12class CPrePaidSimCard{protected:int m_iMobileBalance;public:CPrePaidSimCard(){m_iMobileBalance=10;}bool CanMakeCall(){if(m_iMobileBalance > MIN_CALL_CHARGES)return true;return false;}};class CMobile : public CPrePaidSimCard{void CallTargetPhoneNumber(string cr_sMobileNumber){cout << "Calling Phone Number " << cr_sMobileNumber << "n";}public:bool MakeCall(string cr_sMobileNumber){bool vl_bCanMakeCall=CanMakeCall();if(vl_bCanMakeCall==false){cout << "Could Not Dial Phone Number as Mobile PrePaid Balance is Insufficient " << m_iMobileBalance << "n";return false;}return true;}};int _tmain(int argc, _TCHAR* argv[]){CMobile vl_oMobileClassObject;vl_oMobileClassObject.MakeCall("7878787878");return 0;}