Inheritance in C++
Re-usability is one of the important feature in OOPs that we can achieve by inheritance. It is a concept of derived class and Base class.
It is the mechanism of deriving new class from old class is called inheritance.
Here the old class is called derived class and new class is called derived class.
The derived class inherit some or all the properties of the base class.A class can also inherit the properties of more than one class or more than one level.
It is the mechanism of deriving new class from old class is called inheritance.
Here the old class is called derived class and new class is called derived class.
The derived class inherit some or all the properties of the base class.A class can also inherit the properties of more than one class or more than one level.
Derived Class
Derived class is define by specifying its relationship with the base class in addition to its own details. The general form of defining derived class is
class derived class-name : visibility mode base class-name
{
members of derived class;
}
Here the (:) colon indicate that derived class-name is derived from its base class-name. The visibility mode is optional and if present may be either private or public. The default visibility mode is private visibility mode specifies whether the feature of base class are privately or publically derived.
{
members of derived class;
}
Here the (:) colon indicate that derived class-name is derived from its base class-name. The visibility mode is optional and if present may be either private or public. The default visibility mode is private visibility mode specifies whether the feature of base class are privately or publically derived.
On the basis of accessibility
There are three type of accessibility
- Private
- Public
- Protected
Example
class base
{
members of base
};
CASE I:
class derived-1 : public base
{
members of derived-1 + Public member of base and protected member of base
};
CASE II:
class derived-2 : protected base
{
members of derived-2 + public and protected member of base
};
CASE III:
class derived-3 : private base
{
members of derived-3 + public and protected member of base
};
Let we discuss about the cases
Case I : When base class is inherited publicaly by derived 1 class than.
- public member of base class are inherited as a public.
- private member of base class are not accessable.
- protected member of base class are inherited as a protected.
Case II : When base class is inherited as a protected by derived 2 class than.
- public member of base class are inherited as a protected.
- private member of base class are not inherited.
- protected member of base class are inherited as a protected.
public :
int a2;
protected :
int c2 , x , z;
int b2;
Case III : When base class is inherited as a private by derived 3 class than.
- public member of base class are inherited as a private.
- private member of base class are not inherited.
- protected member of base class are inherited as a private.
public :
int a3;
protected :
int c3;
int b2,x,z;
Type of inheritance
- Based on structure of inheritance
- Based on accessibility of inheritance
Based on structure of inheritance
- Simple inheritance or single inheritance
- Multilevel inheritance
- Hierarchical inheritance
- Multiple inheritance
- Hybrid inheritance
Single inheritance
A derived class with only one base class is called single inheritance
Example
Copy Code
#include <iostream>
using namespace std;
class person //base class
{
string name;
string add;
public:
person()
{
name='\0';
add='\0';
}
person(string n)
{
name=n;
add='\0';
}
person(string n,string a)
{
name=n;
add=a;
}
void read()
{
cout<<"Enter name:";
cin>>name;
cout<<"Enter address:";
cin>>add;
}
void print()
{
cout<<"Name="<<name<<"\n";
cout<<"Address="<<add<<"\n";
}
};
class stud:public person
{
string scno;
int marks;
public:
stud()
{
scno='\0';
marks=0;
}
stud(string s)
{
scno=s;
marks=0;
}
stud(string s,int m)
{
scno=s;
marks=m;
}
void read()
{
person::read();
cout<<"Enter scno:";
cin>>scno;
cout<<"Enter marks:";
cin>>marks;
}
void print()
{
person::print();
cout<<"SCNO="<<scno<<"\n";
cout<<"Marks:"<<marks;
}
};
int main()
{
stud st;
st.read();
st.print();
return 0;
}
Output
Enter name:shailu
Enter address:bhopal
Enter scno:23
Enter marks:60
Name=shailu
Address=bhopal
SCNO=23
Marks:60
Multilevel inheritance
The mechanism of deriving a class from another derived class is called multilevel inheritance
Hierarchical inheritance
One class may be inherited by more than one derived class is called hierarchical inheritance.
Multiple inheritance
One derived class with several base class is called multiple inheritance.
Hybrid inheritance
Combination of one or more above inheritance is called hybrid inheritance.
code