Depict the difference between private and public derivation. Explain derived class constructor with suitable program.

This answer is restricted. Please login to view the answer of this question.

Login Now

The difference between public and private declaration are

Public Private
All the class members declared under public will be available to everyone. The class members declared as private can be accessed only by the functions inside the class.
The data members and member functions declared public can be accessed by other classes too. Only the member functions or the friend functions are allowed to access the private data members of a class.
The public members of a class can be accessed from anywhere in the program using the direct member access operator (.) with the object of that class. They are not allowed to be accessed directly by any object or function outside the class.

For multiple inheritance order of constructor call is, the base class’s constructors are called in the order of inheritance and then the derived class’s constructor.

#include <iostream>
using namespace std;

class Parent1
{
    public:
        Parent1()
        {
            cout << "Inside first base class" << endl;
        }
};

class Parent2
{
    public:
        Parent2()
        {
            cout << "Inside second base class" << endl;
        }
};

class Child : public Parent1, public Parent2
{
    public:
        Child()
        {
            cout << "Inside child class" << endl;
        }
};


int main()
{
    Child obj1;
    return 0;
}

The output of above program is

Inside first base class
Inside second base class
Inside child class
If you found any type of error on the answer then please mention on the comment or report an answer or submit your new answer.
Leave your Answer:

Click here to submit your answer.

Discussion
0 Comments
  Loading . . .