Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 67

Inheritance

This Photo by Unknown author is licensed under CC BY-SA.


WHAT IS INHERITANCE IN C++
• Inheritance allows us to define a class in term of another class.
• Provides REAUSABILITY and MAINTAIBILITY of Code.
• The idea of inheritance implements realtionship. For example,
mammal is animal, dog is a mammal hence dog is a animal as well
and so on.
• The existing class is called BASE CLASS.
• The new class which is inherited
is called Derived class.
• There are different types of inheritance.
SYNTAX
class derived_class name : visibility_mode base_class_name
{
//code;
}
EXAMPLE
#include <iostream>
using namespace std;

class Animal
{
public: int legs = 4;
};
class Dog : public Animal
{
public:
int tail = 1;
};
EXAMPLE
int main()
{
Dog d;
cout <<"Legs are: "<<d.legs<<endl;
cout << "Tail is: "<<d.tail;
}
SINGLE INHERITANCE
• There is a single parent class and one child class.
• One derived class with only one base class.
SYNTAX OF SINGLE INHERITANCE
class base_classname
{
properties...
methods... }
;

class derived_classname : visibility_mode base_classname


{
properties...
methods...
};
EXAMPLE
class Person
{
public:
string name;
int age;
};
class Student : public Person
{
public:
void show()
{
cout<<"Name is: "<<name<<endl;
cout<<"Age is: "<<age;
}
};
int main()
{
//Below Creating an object of Child Class(Student)
Student s;
s.name="Ahmad Butt";
s.age=20;
//Calling Child Class Function
s.show();
return 0;
}
Multiple Inheritance
• Multiple inheritance occurs when
a class inherits from more than
one base class. So the class can
inherit features from multiple
base classes using multiple
inheritance.

This Photo by Unknown author is licensed under CC BY-SA.


Multiple Inheritance
• Multiple inheritance
includes Multiple
parents and one
child.
Example of Multiple Inheritance
#include <iostream>

using namespace std;

class A {

   public:

   int a = 5;

   A() {

      cout << "Constructor for class A" << endl;

   }
Continue...
• class B {
   public:
   int b = 10;
   B() {
      cout << "Constructor for class B" << endl;
   }
};
Continue...
• class C: public A, public B {
   public:
   int c = 20;
   C() {
      cout << "Constructor for class C" << endl;
      cout<<"Class C inherits from class A and class B" << endl;
   }
};

• Class C inherits from both classes A and B.


Continue...
• int main() {
   C obj;
   cout<<"a = "<< obj.a <<endl;
   cout<<"b = "<< obj.b <<endl;
   cout<<"c = "<< obj.c <<endl;
   return 0;
}
• In main() function, an object obj of class C is defined. The constructors of
Class A, B and C are automatically called and their contents are displayed. Then
the values of a, b and c are printed.
Output
Constructor for class A

Constructor for class B

Constructor for class C

Class C inherits from class A and class B

a = 5

b = 10
• c = 20
Hierarchical Inheritance
• Hierarchical inheritance
is a kind of inheritance
where more than one
class is inherited from
a single parent or base
class. Especially those
features which are
common in the parent
class is also common
with the base class.
This Photo by Unknown author is licensed under CC BY-NC.
Hierarchical
Inheritance
• Hierarchical
inheritance includes
one parent and
multiple childrens.
class A // base class

    ..............

}; Syntax
class B : access_specifier A // derived
class from A

    ...........

} ;

class C : access_specifier A //
derived class from A

    ...........

} ;

class D : access_specifier A //
derived class from A

{
#include <iostream>

Example of using namespace std;

Hierarchical class A //single base class

Inheritance     public:

int x, y;

void getdata()

       cout << "\nEnter value of x


and y:\n"; cin >> x >> y;

}
Continue...
class B : public A //B is
derived from class base

    public:

void product()

    cout << "\nProduct= "


<< x * y;

}
• };
Continue...
class C : public A //C is
also derived from class base

    public:

void sum()

        cout << "\nSum= " <<


x + y;

}
• };
Continue...
int main()

    B obj1;          
•     C obj2;  
•        
•      obj1.getdata();
•      obj1.product();
•      obj2.getdata();
•      obj2.sum();
•      return 0;
}    //end of program
Output
Enter value of x and y:

Product= 6

Enter value of x and y:

3
• Sum= 5
Specifying a
Derived Class
in C++
The process of specifying a derived
class is same as specifying simple
class.  Additionally, the reference
of parent is specified along with
derived class name to inherit the
capabilities of parent class. 

This Photo by Unknown author is licensed under CC BY


Syntax
• class sub_class :
specifier parent_class 
• { 
•   body of the class 
• }; 

This Photo by Unknown author is licensed under CC BY-NC.


Example
• #include<iostream> 

• using namespace std; 


• class Move 
• { 
•   protected: 
• int position; 
• public: 

• Move() 
• {  This Photo by Unknown author is licensed under CC BY-NC.

• position = 0; 
• } 
Continue...
• void forward() 
• {
position ++; 
• } 
• void show() 
• {
cout<<”Position =
“<<position<<endl; 
• }  This Photo by Unknown author is licensed under CC BY-NC.

• }; 
Continue...
• class Move2 : public Move 
• { 
• public: 
• void backward() 

• {
position--; 
• } 
• }; 
This Photo by Unknown author is licensed under CC BY-NC.
Continue...
• int main()
{
Move2 m; 
• m.show(); 

• m.forward(); 
• m.show(); 
• m.backward(); 
• m.show(); } //End Program This Photo by Unknown author is licensed under CC BY-NC.
Type of
inheritance
The type of
inheritance defines
the access status of
parent class members
in derived class. 
This Photo by Unknown author is licensed under CC BY-NC.
Public
Inheritance
In public inheritance, the
access status of parent class
members in the derived class
remains the same. The public
members of parent class become
the public members of derived
class. The protected members
of parent class become
This Photo by Unknown author is licensed under CC BY-NC.
protected members of derived
class. The private members of
parent class become private
Syntax
class child_class :
public parent_class 

   Body of the class
 }
This Photo by Unknown author is licensed under CC BY-NC.
Accessibility of
derived class
The derived class can
access 
+ Public members of
parent class.
+ Protected members of
parent class.
+ Cannot access the
This Photo by Unknown author is licensed under CC BY-NC.

private members of
parent class.
Accessibility of an
object of derived
class
+ The object of derived
class can access the
public members of parent
class.
+ Object of derived class
cannot access the
protected members of
parent class.
This Photo by Unknown author is licensed under CC BY-NC.

+ Object of derived class


cannot access the private
members of parent class.
Example 
+ #include<iostream> 
+ using namespace std; 
+ class Parent 
+ { 
+ public: 
+ int a; 
+ protected: 
+ int b;  This Photo by Unknown author is licensed under CC BY-NC.

+ private: 
+
Continue... 
+ class Child : public
Parent 
+ { 
+ public: 
+ void in() 
+ { 
+ cout<<"Enter a: "; 
+ cin>>a; 
+ cout<<"Enter b; ";  This Photo by Unknown author is licensed under CC BY-NC.

+ cin>>b; 
+ } 
Continue... 
+ class Child : public
Parent 
+ { 
+ public: 
+ void in() 
+ { 
+ cout<<"Enter a: "; 
+ cin>>a; 
+ cout<<"Enter b; ";  This Photo by Unknown author is licensed under CC BY-NC.

+ cin>>b; 
+ } 
Continue... 
+ void out() 
+ { 
+ cout<<"a= "<<a<<endl; 
+ cout<<"b= "<<b<<endl; 
+ } 
+ }; 
+ int main() 
+ { 
+ Child obj; 
+ obj.in(); 
This Photo by Unknown author is licensed under CC BY-NC.

+ obj.out(); 
Protected Inheritance
Protected Inheritance
• Def:
“When we use access
specifier ‘Protected’ during
creating the child class then
the inheritance is known as
protected inheritance”.
Some key Points
• Protected access specifier only
access in child class but not in the
main.

• The protected inheritance is


similar private inheritance.
• If we create a function inside the
protected class, then we could’t
call it in main.
Continuing key points…

• We can say that it is also private


but it can be inherit.
• It provides less security than
private class.
• ‘Protected’ in derived class can
be access directly by its
member.
class Base
Syntax: {
....
...
....
};

class Derived : Protected Base


{
....
...
....
};
#include<iostream>
using namespace std;
Example: class parent{
private:
void f1()
{
cout<<"Function 1 is called"<<endl;
}
protected:
void f2()
{
cout<<"Function 2 is called"<<endl;
}
public:
void f3()
{
cout<<"Function 3 is called"<<endl;
}
};
class child : protected parent
Continuing… {
private:
void f4()
{
cout<<"Function 4 is called"<<endl;
}
protected:
void f5()
{
cout<<"Function 5 is called"<<endl;
}
public:
void f6()
{
cout<<"Function 6 is called"<<endl;
}
};
Continuing…
int main()
{
child c1;

c1.f1();
c1.f2();
c1.f3();
c1.f4();
c1.f5();
c1.f6();

return 0;

}
Output of Example
#include<iostream>
Example: using namespace std;
class parent{
private:
void f1()
{
cout<<"Function 1 is called"<<endl;
}
protected:
void f2()
{
cout<<"Function 2 is called"<<endl;
}
public:
void f3()
{
cout<<"Function 3 is called"<<endl;
}
};
class child : protected parent
{
Continuing… private:
void f4()
{
cout<<"Function 4 is called"<<endl;
}
protected:
void f5()
{
cout<<"Function 5 is called"<<endl;
}
public:
void f6()
{
cout<<"Function 6 is called"<<endl;
}
};
Continuing…
int main()
{
child c1;

c1.f6();

return 0;

}
Output of Example
Example
#include<iostream>
using namespace std;

class parent{
private:
int a;
protected:
int b;
public:
int c;
};
Continue … class child : protected parent{
public:
void input(){
cout<<"Enter value of b:";
cin>>b;

cout<<"Enter value if c:";


cin>>c;

cout<<endl;

}
Continue … void show(){
cout<<"The value of b is:"<<b<<endl;
cout<<"The value of c is:"<<c<<endl;

}
};
int main(){
child obj;
obj.input();
obj.show();

return 0;
}
Output of example
 In public inheritance, the public and protected
members of the Base class become public and
protected members of Derived class respectively.

 In protected inheritance, the public and protected


members of Base class become protected members
of Derived class.

 And in Private inheritance, the public and protected


Members of Base class become private members of
Derived class.
THE PRIVATE MEMBERS OF THE BASE CLASS ARE INACCESSIBLE TO
THE DERIVED CLASS
#include <iostream> class pvtDerived : private Base {
using namespace std; public:

class Base { string getProt() {


return prot;
private: }
string pvt = "Keyboard";
string getPub() {
protected: return pub;
string prot = "Mouse"; }

};
public:
string pub = "Speakers"; int main() {
pvtDerived object;
string getPvt() {
cout << object.getProt() << endl;
return pvt; cout << object.getPub() << endl;
} cout << object.getPvt() << endl;
return 0;
}; }
OUTPUT
#include <iostream> class pvtDerived : private Base {
using namespace std; public:

class Base { string getProt() {


return prot;
private: }
string pvt = "Keyboard";
string getPub() {
protected: return pub;
string prot = "Mouse"; }

};
public:
string pub = "Speakers"; int main() {
pvtDerived object;
string getPvt() {
cout << object.getProt() << endl;
return pvt; cout << object.getPub() << endl;
} // cout << object.getPvt() << endl;
return 0;
}; }
OUTPUT
#include <iostream> class pvtDerived : private Base {
using namespace std; public:

string getProt() {
class Base { return prot;
}

protected: string getPub() {


return pub;
string prot = "Mouse"; }
string getPvt() {
return pvt;
public: }
string pub = "Speakers"; };

string pvt = "Keyboard"; int main() {


pvtDerived object;

cout << object.getProt() << endl;


}; cout << object.getPub() << endl;
cout << object.getPvt() << endl;
return 0;
}
OUTPUT

You might also like