You must Sign In to post a response.
  • Implementation of multiple inheritance with use of constructors in derived class.


    Preparing for Object Oriented Programming Exam of Engineering? Searching for detailed solution to the problem of multiple inheritance implementation? Here, on this Ask Expert page you can check out the detailed solution and prepare well for exam.

    Following is a question that was asked in the question paper of BE(E&E)(December 2016)conducted by Bangalore University, for subject Object Oriented Programming using C++(EE-305).

    This question is asked for 10 marks and hence requires some explanation along with the program code. Please let me know the solution.

    Q) Write a C++ program to implement multiple inheritance and illustrate the use of constructors in derived class. [8 Marks]
  • Answers

    1 Answers found.
  • Constructor is a special member function which constructs(initialize ) the data members of the object, whenever object of the class is created. Name of the constructor function is same as that of the class name.
    Constructor in derived class:- When base class contains constructor then by default derived class must have constructor to supply the values for initialization of members of base class as well members of derived class. The reason is that in inheritance normally object of derived class is created and hence now it is responsibility of derived class to initialize members of base class because base class object is not created and hence base class constructor is not invoked. This mechanism is used in this program.
    syntax for constructor in derived class is as below

    class derived : public class base
    {
    data type var_d;
    derived (data type var1, data type var2, data type var3,....., data type var_n):base(var1, var2)
    {
    var_d=var_n;
    }
    };
    when derived class constructor get invoked then it has variables for base class as well as for its own. First base class constructor values are supplied and the derived class constructor values are supplied.
    Please include iostream.h and conio.h header files before declaration of class.

    //PROGRAM TO IMPLEMENT MULTIPLE INHERITANCE USING CONSTRUCTOR IN DERIVED CLASS
    #include
    #include
    class test1
    {
    protected:
    int t1;
    public:
    test1(int a)
    {
    t1=a;
    cout<<"\nMarks obtained in test1 are "< }
    };
    class test2
    {
    protected:
    int t2;
    public:
    test2(int b)
    {
    t2=b;
    cout<<"\nMarks obtained in test2 are "< }
    };
    class result:public test1, public test2
    {
    protected:
    int total;
    float avg;
    public:
    result(int x, int y):test1(x),test2(y)
    {
    total=t1+t2;
    avg=float(total)/2;
    cout<<"\nTotal marks are "< cout<<"\nAverage marks are "< }
    };
    void main(void)
    {
    int m,n;
    clrscr();
    cout<<"\nEnter marks in first test(out of 25)\n";
    cin>>m;
    cout<<"Enter marks in second test(out of 25)\n";
    cin>>n;
    class result r(m,n);
    getch();
    }


  • Sign In to post your comments