B.TECH CSE BT ECE(oop's) : Inheritance in C++
Inheritance in C++
Modes of Inheritance
- Public mode: 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.

For Example:
#include <iostream>
using
namespace
std;
// first base class
class
Vehicle {
public
:
Vehicle()
{
cout <<
"This is a Vehicle"
<< endl;
}
};
// second base class
class
FourWheeler {
public
:
FourWheeler()
{
cout <<
"This is a 4 wheeler Vehicle"
<< endl;
}
};
// sub class derived from two base classes
class
Car:
public
Vehicle,
public
FourWheeler {
};
// main function
int
main()
{
// creating object of sub class will
// invoke the constructor of base classes
Car obj;
return
0;
}
Comments
Post a Comment