0% found this document useful (0 votes)
9 views37 pages

Unit 5

The document discusses the concept of inheritance in programming, detailing how classes can inherit properties and methods from other classes. It covers various types of inheritance, such as single, multilevel, multiple, hierarchical, and hybrid inheritance, along with examples and syntax. Additionally, it explains access modifiers and their impact on inherited members, as well as method overriding and virtual base classes.

Uploaded by

yadavsushant1502
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views37 pages

Unit 5

The document discusses the concept of inheritance in programming, detailing how classes can inherit properties and methods from other classes. It covers various types of inheritance, such as single, multilevel, multiple, hierarchical, and hybrid inheritance, along with examples and syntax. Additionally, it explains access modifiers and their impact on inherited members, as well as method overriding and virtual base classes.

Uploaded by

yadavsushant1502
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 37

Unit-5

Inheritance
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
class doctor: public
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
 If a class is derived from a single class then it
A
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{ O
} utput:
bool tail = true; Legs=4
public: Tail=1
void display2(){
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
class Student :public };
Person
{
//content of Student
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 class
A B
then it is called multiple inheritance.
 Here, class C is derived from two classes, class A
C and class B.

Example:
class Liquid class Petrol: public
{ Liquid, public Fuel
//content of Liquid {
class //content of
}; Petrol class
class Fuel };
{
//content of Fuel class
};
class Liquid{ int main()
public: {
void display1(){ Liquid l;
cout<<"\nLiquid class"; Fuel f;
} Petrol p;
};
class Fuel{ l.display1();
public:
void display2(){ f.display2();
cout<<"\nFuel class";
} p.display3();
};
class Petrol:public Liquid,public p.display2();
Fuel{ Output:
public: Liquid class
p.display1();
void display3(){ } Fuel class
cout<<"\nPetrol class"; Petrol class
} Fuel class
}; Liquid class
4. Hierarchical Inheritance
 If one or more classes are derived from
A
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
public: Animal{
void display1(){ public:
cout<<"\nAnimal Class"; void display3(){
} cout<<"\nHorse
}; class";
class Elephant:public Animal{ }
public: };
void display2(){ class Cow:public Animal{
cout<<"\nElephant class"; public:
} void display4(){
}; cout<<"\nCow class";
}
int main(){ Output:
};
Animal a; Elephant e; Horse h; Cow Animal Class
c; Elephant class
a.display1(); Animal Class
e.display2(); e.display1(); Horse class
h.display3(); h.display1(); Animal Class
Cow class
c.display4(); c.display1();
Animal Class
}
5. Hybrid Inheritance
A

B C

D
 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 class D is example of Multiple Inheritance
so this hybrid inheritance is combination of Hierarchical and
Multiple Inheritance.
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{ };
public: class HybridCar:public
void display2(){ FuelCar, public ElecCar{
cout<<"\nFuelCar public:
class"; void display4(){
} cout<<"\nHybridCar
}; class";
int main(){ }
Car c; FuelCar f; ElecCar }; Output:
e; HybridCar class
HybridCar h; ElecCar class
h.display4(); FuelCar class
h.display3();
h.display2();
GTU 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
public: ABC{
void setProtMemb(int i){ public:
m_protMemb = i; } void useProtfunc(){
void Display(){ Protfunc(); }
cout<<m_protMemb<<endl;} };
protected:
int m_protMemb;
void Protfunc(){
cout<<"\nAccess allowed\
n";}
}; main() {
int
ABC a; XYZ x;
a.m_protMemb; //error, m_protMemb is
protected public access
a.setProtMemb(0);//OK,uses
a.Display(); function
a.Protfunc(); //error, Protfunc() is
protected public access
x.setProtMemb(5);//OK,uses
x.Display(); function
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
private: base
int z; {
public: // x is private
int x; // y is private
protected: // z is not accessible from
privateDerived
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
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: {
int i; public:
}; int sum;
class B:virtual public A };
{ int main()
public: {
int j; D ob1;
}; ob1.i=10;
class C: public virtual ob1.j=20;
A ob1.k=30;
{ ob1.sum=ob1.i+ob1.j+ob1.
public: k;
int k; cout<<ob1.sum;
}; }
Derived class constructor
class Base{
int x;
public:
Base() { cout << "Base default constrictors"; }
};
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
Base(int i){ d(10,20) ;
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 B();//virtual base


public B A();//base
{ C();derived
};

You might also like