0% found this document useful (0 votes)
45 views

CPP OOPS Module 5

The document discusses the concept of inheritance in object-oriented programming. It defines inheritance as a mechanism where a class can acquire the properties and methods of another class. The class that inherits is called the derived class, while the class being inherited from is called the base class. The derived class can have all the features of the base class and additional new features. Different types of inheritance are described such as single, multilevel, multiple, hierarchical, and hybrid inheritance.

Uploaded by

Tanisha Singhal
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

CPP OOPS Module 5

The document discusses the concept of inheritance in object-oriented programming. It defines inheritance as a mechanism where a class can acquire the properties and methods of another class. The class that inherits is called the derived class, while the class being inherited from is called the base class. The derived class can have all the features of the base class and additional new features. Different types of inheritance are described such as single, multilevel, multiple, hierarchical, and hybrid inheritance.

Uploaded by

Tanisha Singhal
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 37

Inheritance

Concept of Inheritance
class Doctor class Footballer class Businessman
Attributes: Attributes: Attributes:
Age, Height, Weight Age, Height, Weight Age, Height, Weight
Methods: Methods: Methods:
Talk() Talk() Talk()
Walk() Walk() Walk()
Eat() Eat() Eat()
Diagnose() Playfootball() Runbusiness()
 All of the classes have common attributes (Age, Height, Weight)
and methods (Walk, Talk, Eat).
 However, they have some special skills like Diagnose, Playfootball
and Runbusiness.
 In each of the classes, you would be copying the same code for
Walk, Talk and Eat for each character.
Concept of Inheritance(Cont…)
class Person class Person
is called
Attributes:
Base class
Age, Height, Weight
Methods:
Talk() , Walk(), Eat()

class Doctor class Footballer class Businessman


Methods: Methods: Methods:
Diagnose() Playfootball() Runbusiness()

These classes
are called
Derived class
Concept of Inheritance(Cont…)
class Vehicle
Attributes:
Engineno, color
Methods:
applybreaks()

class Car class Bus class Truck


Attributes: Attributes: Attributes:
privatevehicle publicvehicle goodsvehicle
Inheritance
• Inheritance is the process, by which class can acquire(reuse)
the properties and methods of another class.
• The mechanism of deriving a new class from an old class is
called inheritance.
• The new class is called derived class and old class is called
base class.
• The derived class may have all the features of the base class
and the programmer can add new features to the derived
class.
Syntax to Inherit class
Syntax:
class derived-class-name : access-mode base-class-name
{
// body of class
};

Example:
class person{
//body of class Base Class
};
Access Mode
public
class doctor: protected
private person
{ Derived Class
//body of class
} By default access mode is
private
Types of Inheritance
1. Single Inheritance
2. Multilevel Inheritance
3. Multiple Inheritance
4. Hierarchical Inheritance
5. Hybrid Inheritance (also known as Virtual
Inheritance)
1. Single Inheritance
A • If a class is derived from a single class
then it is called single inheritance.
• Class B is derived from class A
B

Example:
class Animal
{ public:
int legs = 4;
};
class Dog : public Animal
{ public:
int tail = 1;
};
Single Inheritance Program
class Animal{ int main()
int legs=4; {
public: Animal a1;
void display1(){ Dog d1;
cout<<"\nLegs="<<legs; d1.display1();
} d1.display2();
}; }
class Dog : public Animal{
bool tail = true; Output:
public: Legs=4
void display2(){ Tail=1
cout<<"\nTail="<<tail;
}
};
2. Multilevel Inheritance
A • Any class is derived from a class which is derived
from another class then it is called multilevel
inheritance.
B
• Here, class C is derived from class B and class B
is derived from class A, so it is called multilevel
C inheritance.
Example:
class Person class ITStudent :public
{ Student
//content of class person {
}; //content of ITStudent
class Student :public Person class
{ };
//content of Student class
};
class Person{ int main()
public: {
void display1(){ Person p;
cout<<"\nPerson class"; Student s;
} ITStudent i;
}; p.display1();
class Student:public Person{ s.display2();
public: s.display1();
void display2(){ i.display3();
cout<<"\nStudent class"; i.display2();
} i.display1();
}; }
Output:
class ITStudent:public Student{ Person class
public: Student class
void display3(){ Person class
cout<<"\nITStudent class"; ITStudent class
} Student class
}; Person class
3. Multiple Inheritance
• If a class is derived from more than one
A B
class then it is called multiple inheritance.
• Here, class C is derived from two classes,
C class A and class B.

Example:
class Liquid class Petrol: public
{ Liquid, public Fuel
//content of Liquid class {
}; //content of Petrol
class Fuel class
{ };
//content of Fuel class
};
class Liquid{ int main()
public: {
void display1(){ Liquid l;
cout<<"\nLiquid class"; Fuel f;
} Petrol p;
}; l.display1();
class Fuel{ f.display2();
public: p.display3();
void display2(){ p.display2();
cout<<"\nFuel class"; p.display1();
} }
};
class Petrol:public Liquid,public
Fuel{ Output:
public: Liquid class
void display3(){ Fuel class
cout<<"\nPetrol class"; Petrol class
} Fuel class
}; Liquid class
4. Hierarchical Inheritance
• If one or more classes are derived
A
from one class then it is called
hierarchical inheritance.
• Here, class B, class C and class D are
B C D derived from class A.
Example:
class Animal class Horse :public Animal
{ {
//content of class Animal //content of class Horse
}; };
class Elephant :public Animal class Cow :public Animal
{ {
//content of class Elephant //content of class Cow
}; };
class Animal{ class Horse:public Animal{
public: public:
void display1(){ void display3(){
cout<<"\nAnimal Class"; cout<<"\nHorse class";
} }
}; };
class Elephant:public Animal{ class Cow:public Animal{
public: public:
void display2(){ void display4(){
cout<<"\nElephant class"; cout<<"\nCow class";
} }
}; };
int main(){ Output:
Animal a; Elephant e; Horse h; Cow c; Animal Class
a.display1(); Elephant class
e.display2(); e.display1(); Animal Class
h.display3(); h.display1(); Horse class
c.display4(); c.display1(); Animal Class
Cow class
}
Animal Class
5. Hybrid Inheritance
A

B C

• It is a combination of any other inheritance types.


That is either multiple or multilevel or hierarchical
or any other combination.
• Here, class B and class C are derived from class A
and class D is derived from class B and class C.
• class A, class B and class C is example of
Hierarchical Inheritance and class B, class C and
Hybrid Inheritance (Cont…)
class Car
{
//content of class Car
};
class FuelCar:public Car
{
//content of class FuelCar
};
Class ElectricCar:public Car
{
//content of class ElectricCar
};
Class HybridCar:public FuelCar, public ElectricCar
{
//content of classHybridCar
};
class Car{ class ElecCar:public Car{
public: public:
void display1(){ void display3(){
cout<<"\nCar class"; cout<<"\nElecCar class";
} }
}; };
class FuelCar:public Car{ class HybridCar:public
public: FuelCar, public ElecCar{
void display2(){ public:
cout<<"\nFuelCar class"; void display4(){
} cout<<"\nHybridCar class";
}; }
};
int main(){
Car c; FuelCar f; ElecCar e; Output:
HybridCar h; HybridCar class
h.display4(); ElecCar class
h.display3(); FuelCar class
h.display2();
}
KEC Program
• Create a class student that stores roll_no, name. Create a class test
that stores marks obtained in five subjects. Class result derived
from student and test contains the total marks and percentage
obtained in test. Input and display information of a student.

class student Class test


Attributes:
Attributes:
roll_no
marks[5]
name

class result
Attributes:
totalmarks
percentage
Protected access modifier
• Protected access modifier plays a key role in inheritance.
• Protected members of the class can be accessed within the
class and from derived class but cannot be accessed from any
other class or program.
• It works like public for derived class and private for other class.
class ABC { class XYZ : public ABC{
public: public:
void setProtMemb(int i){ void useProtfunc(){
m_protMemb = i; } Protfunc(); }
void Display(){ };
cout<<m_protMemb<<endl;}
protected:
int m_protMemb;
void Protfunc(){
cout<<"\nAccess allowed\n";}
};
int main() {
ABC a; XYZ x;
a.m_protMemb; //error, m_protMemb is protected
a.setProtMemb(0); //OK,uses public access function
a.Display();
a.Protfunc(); //error, Protfunc() is protected
x.setProtMemb(5); //OK,uses public access function
x.Display();
x.useProtfunc();} // OK, uses public access function
Class access modifiers
• public – Public members are visible to all classes.
• private – Private members are visible only to the class
to which they belong.
• protected – Protected members are visible only to
the class to which they belong, and derived classes.
Access modifiers
Base class How inherited base class
members
members appear in derived class
private: x x is inaccessible
public
protected: y protected: y
base class
public: z public: z

private: x x is inaccessible
private
protected:y private: y
base class
public: z private: z

private: x x is inaccessible
protected protected: y
protected:y
base class protected: z
public: z
class base{ class privateDerived: private base
private: {
int z; // x is private
public: // y is private
int x; // z is not accessible from
privateDerived
protected:
int y; };
};
class publicDerived: public base{
// x is public
// y is protected
// z is not accessible from publicDerived
};
class protectedDerived: protected base{
// x is protected
// y is protected
// z is not accessible from
protectedDerived
};
Inheritance using Public Access
class Grade class Test : public Grade
private members: private members:
char letter; int numQuestions;
float score; float pointsEach;
void calcGrade(); int numMissed;
public members: public members:
void setScore(float); Test(int, int);
float getScore();
char getLetter();
private members:
int numQuestions;
When Test class inherits float pointsEach;
int numMissed;
from Grade class using public members:
public class access, it Test(int, int);
looks like this: void setScore(float);
float getScore();
char getLetter();
Inheritance using Private Access
class Grade class Test : private Grade
private members: private members:
char letter; int numQuestions;
float score; float pointsEach;
void calcGrade(); int numMissed;
public members: public members:
void setScore(float); Test(int, int);
float getScore();
char getLetter();
private members:
int numQuestions:
float pointsEach;
When Test class inherits int numMissed;
from Grade class using void setScore(float);
private class access, it float getScore();
looks like this: float getLetter();
public members:
Test(int, int);
Inheritance using Protected Access
class Grade class Test : protected Grade
private members: private members:
char letter; int numQuestions;
float score; float pointsEach;
void calcGrade(); int numMissed;
public members: public members:
void setScore(float); Test(int, int);
float getScore();
char getLetter(); private members:
int numQuestions:
float pointsEach;
When Test class inherits int numMissed;
public members:
from Grade class using Test(int, int);
protected class access, it protected members:
looks like this: void setScore(float);
float getScore();
float getLetter();
Visibility of inherited members
Derived class visibility
Base class
visibility
Public Private Protected
derivation derivation derivation

Private Not inherited Not inherited Not inherited

Protected Protected Private Protected

Public Public Private Protected


Function Overriding / Method Overriding
• If base class and derived class have member
functions with same name and arguments then
method is said to be overridden and it is called
function overriding or method overriding in C++.
class ABC
{
public:
void display(){
cout<<"This is parent class";
}
};
class XYZ:public ABC{
public:
void display(){//overrides the display()mehtod of class ABC
cout<<"\nThis is child class";
}
};
int main(){
XYZ x;
x.display();//method of class XYZ invokes, instead of class ABC
x.ABC::display();
}
Virtual Base Class
A we can prevent multiple copies
memberA of the base class by declaring
the base class as virtual when
B C it is being inherited.
memberA memberA

D
memberA Multiple copies of member A
Virtual base class (Cont…)
• Virtual base class is used to prevent the
duplication/ambiguity.
• In hybrid inheritance child class has two direct parents
which themselves have a common base class.
• So, the child class inherits the grandparent via two
separate paths. it is also called as indirect parent class.
• All the public and protected member of grandparent
are inherited twice into child.
• We can stop this duplication by making base class
virtual.
class A class D:public B, public C
{ {
public: public:
int i; int sum;
}; };
class B:virtual public A int main()
{ {
public: D ob1;
int j; ob1.i=10;
}; ob1.j=20;
class C: public virtual A ob1.k=30;
{ ob1.sum=ob1.i+ob1.j+ob1.k;
public: cout<<ob1.sum;
int k; }
};
Derived class constructor
class Base{
int x;
public:
Base() { cout << "Base default constructor"; }
};
class Derived : public Base
{ int y;
public:
Derived() { cout<<"Derived default constructor"; }
Derived(int i) { cout<<"Derived parameterized constructor";}
};
int main(){
Base b;
Derived d1;
Derived d2(10);
}
Derived class constructor (Cont…)
class Base int main()
{ int x; {
public: Derived d(10,20) ;
Base(int i){ }
x = i; cout << "x="<<x;
}
};
class Derived : public Base {
int y;
public:
Derived(int i,int j) : Base(j)
{ y = i; cout << "y="<<y;
}
};
Execution of base class constructor
Method of inheritance Order of execution
class Derived: public Base Base();
{ Derived();
};

class C: public A, public B A();//base(first)


{ B();//base(Second)
}; C();derived

class C:public A, virtual public B B();//virtual base


{ A();//base
}; C();derived
Thank You

You might also like