#include <iostream>
// first base class
class Vehicle
{
public:
Vehicle() { std::cout << "This is a Vehicle\n"; }
};
// second base class
class FourWheeler
{
public:
FourWheeler()
{
std::cout << "This is a 4 wheeler Vehicle\n";
}
};
// sub class derived from two base classes
class Car : private Vehicle, private FourWheeler
{
};
// main function
int main()
{
// Creating object of sub class will
// invoke the constructor of base classes?
Car obj;
return 0;
}
I was expecting there to be no output but it gives output (ie. it runs the constructor of super classes). Why does this happen? Please enlighten me.
Private is used to describe the access control of the class. The inherited classes will run their respective constructors regardless of private or public inheritance.
Private should be used to define methods and functions you do not want to be exposed to methods and functions outside the class. It is not meant to hide or lock functions.