Académique Documents
Professionnel Documents
Culture Documents
Inheritance
Advantages of inheritance
Categories of inheritance
Single inheritance
Multiple inheritance
Multilevel inheritance
Types of inheritance
Public inheritance
Private inheritance
Protected inheritance
Inheritance
A programming technique that is used to reuse an existing
class to build a new class
Terminologies
Base/parent/super class
That is reused to create a new class
Example
Vehicle
Private Yes No No
Categories
Single Multiple Multilevel
Child/
Parent level 2
Child
Child Child 1 Child 2
Syntax
Single inheritance
class sub_class: specifier parent_class
{
body
}
Multiple inheritance
class sub_class: specifier parent_class1 , specifier parent_class2
{
body
}
Multilevel inheritance
class sub_class: specifier grandparent_class
{
body
}
class sub_class: specifier parent_class
{
body
}
Example single inheritance
class Person Void showinfo()
{ {
protected: cout<< your personal info is;
int id; cout<< id is<<id<<endl;
Char name[50], address[100]; cout<< name is<<name<<endl;
public: cout<< address is<<address<<endl;
Person() }
{ };
id=0;
name[0]=\0;
address[0]= \0;
}
void getinfo()
{
cout<< Enter ur id, name. address;
cin>>id;
gets(name);
gets(address);
}
Continued
class Student: public Person void showedu()
{ {
cout<< ur education info is;
private: cout<< Roll no is<<rmo<<endl;
int rno, marks; cout<< Marks are<<marks<<endl;
public: }
Student() };
{ void main(void)
Person:: Person(); {
rno=marks=0; Student s;
} s.getinfo();
void getedu() s.getedu();
{ s.showinfo();
cout<<enter R# & marks; s.showedu();
cin>>rno>>marks; getch();
} }
Multilevel inheritance ( Example)
class A class B : public A
{ {
private: private:
int a; int b;
public: public:
void in() void in()
{ {
cout<< Enter value for a; A::in();
cin>>a; cout<< Enter value for b;
} cin>>b;
void out() }
{ void out()
cout<< value of a is<<a; {
} A::out();
}; cout<< value of b is<<b;
}
};
Continued
class C : public B void main(void)
{ {
private: C obj;
int c; obj.in();
public: obj.out();
void in() getch();
{ }
B::in();
cout<< Enter value for c;
cin>>c;
}
void out()
{
B::out();
cout<< value of c is<<c;
}
};
Multiple inheritance ( Example)
class A class B
{ {
private: private:
int a; int b;
public: public:
void in() void input()
{ {
cout<< Enter value for a; cout<< Enter value for b;
cin>>a; cin>>b;
} }
void out() void output()
{ {
cout<< value of a is<<a; cout<< value of b is<<b;
} }
}; };
Continued
class C : public A, public B void main(void)
{ {
private: C obj;
int c; obj.get();
public: obj.show();
void get() getch();
{ }
A::in();
B::input();
cout<< Enter value for c;
cin>>c;
}
void show()
{
A::out();
B::output();
cout<< value of c is<<c;
}
};
Ambiguity in multiple inheritance
Ambiguity will be created in multiple inheritance
when the names of functions is similar in two or more
parent classes, so compiler cant determine which
function to execute when the object of the derived
class attempts to execute such function
Removal of such ambiguity
By overloading
By use of scope resolution operator
Types of inheritance
Three types
Public inheritance
Private inheritance
Protected inheritance
Public inheritance
In which access level in both parent and child class is
same
Public members become public in child class
Private members become private in child class
Protected members become protected in child class
Syntax
class child_class : public Parent_class
{
Body
};
Example
class Parent void main(void)
{ {
public: Child obj;
int a; Obj.in();
Private: Obj.show();
int b; getch();
protected : }
int c;
};
class Child : public Parent
{
public:
void in()
{
cout<< enter values for a & b;
cin>>a>>b;
}
void out()
{
cout<<Values of a & b are<<a<<b;
}
};
Protected inheritance
In which access level in both parent and child class is
restricted
Public members become public in child class
Private members become private in child class
Protected members become protected in child class
Syntax
class child_class : protected Parent_class
{
Body
};
Example
{ void main(void)
public: {
int a; Child obj;
Private: Obj.in();
int b; Obj.show();
protected : getch();
int c; }
};
class Child : protected Parent
{
public:
void in()
{
cout<< enter values for a & b;
cin>>a>>b;
}
void out()
{
cout<<Values of a & b are<<a<<b;
}
};
Private
In which access level in both parent and child class is
restricted
Public members become private in child class
Private members become private in child class
Protected members become private in child class
Syntax
class child_class : protected Parent_class
{
Body
};
Example
{ void main(void)
public: {
int a; Child obj;
Private: Obj.in();
int b; Obj.show();
protected : getch();
int c; }
};
class Child : private Parent
{
public:
void in()
{
cout<< enter values for a & b;
cin>>a>>b;
}
void out()
{
cout<<Values of a & b are<<a<<b;
}
};