You must Sign In to post a response.
  • Category: Programming

    Program for multiple inheritance using C++


    Want to write a C++ program for multiple inheritance? Looking out for the correct solution here? Our ISC experts shall provide you with the program on this page.

    Following is the question asked in MSBTE(Maharashtra State Board of Technical Education, Mumbai) Diploma in Computer Engineering question paper for Summer 2018 examination.
    This question is asked for 4 marks and hence requires some explanation along with program code. Please let me know the solution.
    Q) Write a program for multiple inheritance. [4 Marks]
  • Answers

    1 Answers found.
  • Inheritance is one of the feature provided by Object Oriented Programming. Inheritance is actually used for re-usability of existing class. Using inheritance it is possible to declare a class with required details and implement the same class. But after implementing this class if further requirements are there to extend the same class with additional new features, then a new class is written with new requirements and this new class is connected with previously defined class and this concept of extending the old class with new features is called as inheritance.
    In inheritance old class is called as base class and new class is called as derived class.
    There are five different types of inheritance. They are
    1) Single/Simple inheritance
    2) Multiple inheritance
    3) Multi level inheritance
    4) Hierarchical inheritance
    5) Hybrid inheritance
    General syntax for deriving a new class from old class is as follows
    Derived class/new class : access specifier base class
    {
    -----------------
    -----------------
    };
    In our program we are using multiple inheritance and hence I will say something about this inheritance. Here multiple word is used for base classes. It means in multiple inheritance there may be more than two base classes and each are separated by using comma.

    e.g. class abc : public xyz, public mnp

    here abc is the derived class and xyz and mnp are base classes. in syntax after derived class : (colon) is used as a separator between derived class and base class.

    Following is the program to show the multiple inheritance.

    Please include iostream.h and conio.h header files before declaration of class test1.


    //PROGRAM TO IMPLEMENT MULTIPLE INHERITANCE.
    #include
    #include
    class test1
    {
    protected:
    int t1;
    public:
    void get(int m1)
    {
    t1=m1;
    }
    };
    class test2
    {
    protected:
    int t2;
    public:
    void input(int m2)
    {
    t2=m2;
    }
    };
    class result:public test1, public test2
    {
    protected:
    int total;
    float avg;
    public:
    void display(void)
    {
    cout<<"\nMarks in first test are "< cout<<"\nMarks in second test are "< total=t1+t2;
    cout<<"\nTotal of two test marks are "< avg=float(total)/2;
    cout<<"\nAverage of two test marks are "< }
    };
    void main(void)
    {
    int x,y;
    clrscr();
    cout<<"\nEnter the marks(out of 25) in first test\n";
    cin>>x;
    cout<<"\nEnter the marks(out of 25) in second test\n";
    cin>>y;
    class result r;
    r.get(x);
    r.input(y);
    clrscr();
    r.display();
    getch();
    }

    While writing programs using inheritance, normally object of derived class is created. As derived class can have access to its own members(variables and functions) as well as members of base class/classes.
    In above program both base class have protected members. When base class having protected members gets inherited in public mode then all protected members of base class becomes protected members of derived class and ready for further inheritance. Protected accesses specifier give access to base class members from derived class. If base class members are private then they can not be accessed in any way by derived class object.

    Here in above program object of derived class(result is the derived class) is created. Using this derived class object program get access to base class members as well as its own members.

    Above program has a function named as display() in derived class. In this function one statement is used which is
    avg=float(total)/2;
    This is called as casting of a variable. Here casting means to change the type of total variable which is integer basically to float. But this is a temporary change during this statement only.


  • Sign In to post your comments