Short note: Inheritance in object
Oriented programming..
1] Explain types of inheritance with example.
- Inheritance is the process, by which class can acquire the properties and methods of another class.
- The mechanism of deriving a new class from an 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.
πTypes of Inheritance:
A] Single inheritance:
inheritance.
☺Class B is derived from class A.
class then it is called multilevel inheritance.
☺Here, class C is derived from class B and class B is derived from
class A, so it is called multilevel inheritance.
C] Multiple Inheritance:
multiple inheritance.
☺Here, class C is derived from two classes, class A and class B.
called hierarchical inheritance.
☺Here, class B, class C and class D are derived from class A.
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.
πExample:
#include<iostream>
using namespace std;
class A
{
public:
void disp A( )
{
cout<<"class A method"<<endl;
}
};
class B : class A // Single Inheritance - class B is derived from
class A
{
public:
void disp B( )
{
cout<<"class B method";
}
};
class C : public B //Multilevel Inheritance - class C is derived
from class B
{
public:
void disp C( )
{
cout<<"class C method "<<endl;
}
};
class D
{
public:
void disp D( )
{
cout<<"class D method"<<endl;
}
};
class E: public A, public D //Multiple Inheritance: class E is
derived from class A and D
{
public:
void disp E( )
{
cout<<"class E method";
}
};
class F: public B, public C //Hybrid Inheritance: class F is
derived from class B and C
{
public:
void disp F( )
{
cout<<"class F method";
}
};
int main( )
{
A a;
B b;
C c;
E e;
F f;
b.dsplayA( );
f.displayF( );
f.displayA( );
}
Output:
class A method
class B method
class C method
class D method
- Class B and class E are derived from class A so it is example of Hierarchal Inheritance.
- Class F is derived from class B and class C, class B is derived from class A so displayA( ) is not a member of class F then also we can access it using object of class F.
2] List out visibility of inherited members in various categories
of inheritance.
3] Explain protected access modifier for class members.
- This 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 programs
πExample:
#include<iostream>
using namespace std;
class A
{
protected:
int a;
public:
void getdata( )
{
cout<<"Enter value of a:";
cin>>a;
}
};
class B : public A
{
public:
void getshow( )
{
cout<<"Enter value of a is :"<<a;
}
};
int main ( )
{
B x;
x.getdata ( );
x.show ( );
}
Output:
Enter value of a:5
Value of a is:5
4] What is overriding in C++. Explain with suitable example.
- If base class and derived class have member functions with same name and arguments.
- If you create an object of derived class and write code to access that member function then, the member function in derived class is only invokes.
- Means the member function of derived class overrides the member function of base class.
- This is called function overriding or method overriding in C++.
πExample:
#include<iostream>
using namespace std;
class A
{
public:
void display( )
{
cout<<"This is parent class";
}
};
class B:public A
{
public:
void display( ) //overriding the display( ) function of
class A
{
cout<<"\nThis is child class";
}
};
int main( )
{
B x;
x.display( ); //method of class B invokes, instead of class A
}
Output:
This is child class
5] Explain virtual base class with example.
- It 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.
![]() |
Multiple Inheritance |
- We can stop this duplication by making base class virtual.
πExample:
class A
{
public:
int i;
};
class B : virtual public A
{
public:
int j;
};
class C: virtual public A
{
public:
int k;
};
class D: public B, public C
{
public:
int sum;
};
- The keywords virtual and public may be used in either order.
- If we use virtual base class, then it will inherit only single copy of base class to child class.
6] List out various situations for the execution of base class constructor in inheritance.
Comments
Post a Comment