Professional Documents
Culture Documents
Chapter 3
Inheritance
• Inheritance is the process of
creating new classes, called
derived classes, from existing or
base classes.
• Its big payoff is that it permits
code reusability.
– saves time
– can be distributed as libraries.
Example : Banking
Scenario: Consider Bank Accounts which are of
various types such as Savings, Current, Loan, PPF.
• It is obvious that these types of accounts are similar.
Also they have some specific attributes/ behaviors.
• Through inheritance a base class can be created
and all common attributes/ behaviors can be moved
to the base class.
Base Class/ Derived class/
Parent Class/
Super class
Example : Banking Child Class/
Sub class
class BankAccount{ class SavingAccount: public BankAccount
public: Object of SavingAccount {
s1 public:
int accno; Syntax for
int NumberOfTransactions;
accno
float accBalance; inheriting class
static int minimumBalance,
string name; accBalance Inherited
transactionsLimit;
members
long mobile[2];name
mobile[] };// end of class SavingAccount
};// end of class BankAccount
NumberOfTran Specialized
sactions; members int main()
{
SavingAccount s1,s2;
}
Example : Banking
void debit(float amount)
Add following {
cout<<"base class method call debit(float
methods to amount)"<<endl;
accBalance=accBalance-amount;
BankAccount class cout<<"Amount Rs"<<amount<<" debited from your
acoount. current balance= Rs"<<accBalance<<endl;
void printaccountdetails(){ }
cout<<" Account void credit(float amount)
{
Num:"<<accno<<" cout<<"base class method credit(float
Balance="<<accBalance<<" amount)"<<endl;
name:"<<name<<endl<<" Mobile accBalance=accBalance+amount;
num 1:"<<mobile[0]<< cout<<"Amount Rs"<<amount<<" credited to your
account. current balance= Rs"<<accBalance;
"Mobile num
}
2"<<mobile[1]<<endl;
}
Example : Banking
Object of SavingAccount
Add following methods to a1
Derived class accno
bool verifyMinimumBalance() accBalance
{ return accBalance<minimumBalance? name
true:false; mobile[]
} Inherited
printaccountdetails() members
Main function
debit(float amount)
int main()
{ credit(float amount)
SavingAccount a1,a2;
NumberOfTransactions;
a1.accBalance=500; Specialized
verifyMinimumBalance() members
a2.accBalance=1000;
int amount= 100;
Example : Banking
Continued program from previous slide
if( a1.verifyMinimumBalance() ) What is the output?
{
cout<<"a1 has minimum
balance"<<endl;
if(a1.accBalance-amount>0)
{ a1.debit(amount);
cout<<"amount
"<<amount<<"debited from a1.
balance="<<a1.accBalance<<""<<endl;
}
}
cout<<"crediting to a2 account"<<endl;
a2.credit(amount);
cout<<" balance after credit
operation="<<a2.accBalance<<endl;
}
Generalization in UML Class Diagrams
Constructors and inheritance
• Constructors are used to initialize the
members of object.
• A constructor can be added to Derived class
and Its good practice to reuse the constructor
defined in Base class. i.e derived class
constructor can call base class constructor.
Constructors and inheritance
User defined
Default constructor
Add following constructors to base class Add following constructors to Derived
BankAccount():accno(0),accBalance(0),name("") class
{ Calling base class SavingAccount(): BankAccount()
Parameterized
mobile[0]=0; default {
constructor
mobile[1]=0; constructor while cout<<"Derived class constructor
cout<<"base class constructor BankAccount()
Redefining Default SavingAccount() invoked"<<endl;
invoked"<<endl;
constructor for
}
derived class }
BankAccount(int n,float m,string n1):
accno(n),accBalance(m),name(n1)
{
Derived class SavingAccount(int n,float m,string n1):
mobile[0]=12345678; Parameterized BankAccount(n, m, n1)
mobile[1]=23456789; constructor calling {
base
cout<<"base class constructor (intclass’s
n,float m,string cout<<"Derived class constructor
n1) invoked"<<endl; parameterized SavingAccount(int n,float m,string n1)
} invoked"<<endl;}
constructor
Constructors and inheritance
Predict the output
SavingAccount s1;
SavingAccount s2(2,100000.0f,"ramesh");
MtechTrainee rakesh("rakesh",10,"rocky",2);
cout<<endl<<"## Rakesh details through inherited methods "<<endl;
cout<<"## function call- \"rakesh.Teacher::display()\" "<<endl;
rakesh.Teacher::display();
cout<<"## function call- \"rakesh.PostGraduate::display()\" "<<endl;
rakesh.PostGraduate::display();
}
Multiple Inheritance
If Multiple base classes inherit from same class, then resolving inherited function
call can be difficult.
Thank You