You are on page 1of 15

Click to editInheritance

Master title style


and its types

Single inheritance


Multiple inheritance


Multilevel inheritance


Hierarchical inheritance


Hybrid inheritance

1
Access specifies :
Click
Public to
mode: edit Master title style
If we derive a sub class from a public base class. Then the public
member of the base class will become public in the derived class and protected
members of the base class will become protected in derived class.
Protected mode:
If we derive a sub class from a Protected base class. Then both public
member and protected members of the base class will become protected in derived
class.
Private mode:
If we derive a sub class from a Private base class. Then both public
member and protected members of the base class will become Private in derived
class. 

2 2
Single inheritance :
Click
In to edita class
single inheritance, Master title
is allowed style
to inherit from only one class. i.e. one sub
class is inherited by one base class only.

Syntax:
class subclass_name : access_mode base_class
{
//body of subclass
};
3 3
Example program for single inheritance
Click to edit Master title style
#include < iostream >
using namespace std;
// base class
class Vehicle
{
public:
Vehicle()
{ cout << "This is a Vehicle" << endl; }
};
// sub class derived from a single base classes
class Car: public Vehicle{
};
int main()
{ Output :
Car obj;
return 0; This is a Vehicle 4 4
}
Multiple inheritance :
ClickInheritance
Multiple to editis aMaster title
feature of C++ wherestyle
a class can inherit from more than
one classes. i.e one sub class is inherited from more than one base classes.

Syntax :
class subclass_name : access_mode base_class1,
access_mode base_class2, ….
{
//body of subclass
};
5 5
Example program for Multiple inheritance :
Click to edit Master title style
#include <iostream>
using namespace std;
class Vehicle { // first base class
public:
Vehicle()
{
cout << “This is a Vehicle” << endl;
}
};
class FourWheeler { // second base class
public:
FourWheeler()
{
cout << “This is a 4 wheeler Vehicle” << endl;
}
6 6
};
Click to edit Master title style
Class Car: public
Vehicle, public
FourWheeler
{ }; // sub
class derived
from two base
classes
int main()
{
Car obj;
return 0;
}
Output :

This is a Vehicle
This is a 4 wheeler Vehicle 7
Multilevel inheritance :
Click
A derivedto classedit Master
inherits title
from another style
derived
class in this type of inheritance.

Syntax:
class C { //Base Class
//body
};
class B : access_specifier C { //Derived from C
//body
};
class A : access_specifier B { //Derived from B
//body
};
8 8
Example program for multilevel inheritance :
Click to edit Master title style
#include <iostream>
using namespace std;
class Animal {
public:
void fun1() {
cout<<“Animal”<<endl;
}
};
class PetAnimal : public Animal {
public:
void fun2() {
cout<<“Pet animal”<<endl;
}
};

9 9
Dog : public PetAnimal {
Click to edit Master title style
public:
void fun3() {
fun1(); public:
void fun3() {
fun1();
fun2();
cout<<“Dog”<<endl;
}
};
int main() {
Dog obj;
obj.fun3(); Output
return 0;
} Animal
Pet animal
Dog 10
Hierarchical inheritance :
InClick
this typeto edit Master
of inheritance, more thantitle
one substyle
class is inherited from a single base class.
i.e. more than one derived class is created
from a single base class.
Syntax:
class base {
//body
};
class derived1 : access_specifier base {
//body;
};
class derived2 : access_specifier base {
//body;
}; 1111
Hybrid inheritance : Syntax :
Click
Hybrid to edit
Inheritance Master
is implemented title style
Class A
by combining more than one type {
// Body
of inheritance. For example:
};
Combining Hierarchical inheritance class B : public A
and Multiple Inheritance. {
// Body
};
class C
{
// Body
};
class D : public B, public C
{
// Body
}; 12
12
“<<a+b<<endl;
}
Click to edit Master title style
};
class B : public Values {
public:
void subtract() {
cout<<“subtraction = “<<a-b<<endl;
}
};
int main() {
A obj1;
B obj2;
obj1.initialize(4.5,8.7);
obj1.add();
Output
obj2.initialize(3.6,11);
obj2.subtract();
addition = 13.2
return 0;
subtraction = -7.4
} 13
Example program for Hybrid inheritance :
Click to edit Master title style
#include <iostream>
using namespace std;
class A {
protected:
float a;
public:
void seta(float n1)
{ a = n1; }
};
class B : public A {
public:
void modifyA()
{ a/=2; }
};
class c {
protected: 14
14
float c;
Click to edit Master title style
public:
void setc(float n2)
{ c = n2; }
};
class D : public B, public C {
public:
float modify()
{ modifyA();
cout<<“Result = “<<a*c; } };
int main() {
D obj;
obj.seta(15.6);
obj.setc(9.7);
obj.modify(); Output
return 0;
} Result = 75.66
15

You might also like