Lab 4 - Multiple Inheritance

Example code

Ambiguous calls

#include <iostream>
using namespace std;
 
class A {
      public:
             void display() {
                  cout << "This is from class A" << endl;
             }
};
 
class B : public A{
      public:
             void display() {
                  cout << "This is from class B" << endl;
             }
};
 
int main() { 
    A *objPtr = new B();
    objPtr -> display(); //prints from A or from B?
    system("pause");
}

Using virtual functions to resolve ambiguous calls

We now make the display() function in class A as a virtual function as shown in the code below.
What does the program print now? Does it print from A or from B?

#include <iostream>
using namespace std;
 
class A {
    public:
        virtual void display() {
            cout << "This is from class A" << endl;
        }
};
 
class B : public A {
    public:
        void display() {
            cout << "This is from class B" << endl;
        }
};
 
int main() {
    A *objPtr = new B();
    objPtr -> display(); //prints from A or from B?
    system("pause");
}

Multiple Inheritance Example

#include <iostream>
using namespace std;
 
class A {
      protected:
                int a;
      public:
             void setA(int);
             int getA();
             void printFieldValues();
};
 
class B {
      protected:
                int b;
      public:
             void setB(int);
             int getB();
             void printFieldValues();
 
};
 
class AB : public A, public B {
      public:
             void getValues(void);
             void display(void);
};
 
int A::getA() {
    return a;
}
 
void A::setA(int _a) {
     a = _a;
}
 
void A::printFieldValues() {
     cout << "a = " << a << endl;
}
 
int B::getB() {
    return b;
}
 
void B::setB(int _b) {
     b = _b;
}
 
void B :: printFieldValues() {
     cout << "b = " << b << endl;
}
 
void AB::getValues() {
     cout << "Enter value for a : " ;
     cin >> a;
     cout << "Enter value for b : ";
     cin >> b;
 
}
 
void AB::display() {
     cout << "a from A = " << a;
     cout << "b from B = " << b;
     cout << endl;
}
 
int main() {
    AB ab;
    //ab.getValues();
    //ab.display();
    ab.setA(7);
    ab.setB(3);
    //ab.printFieldValues(); ERROR - ambiguous call
    ab.A::printFieldValues(); //using disambiguation
    ab.B::printFieldValues(); //using disambiguation
 
    //what is the uouput of the following statements
    A *aobject = new AB();
    aobject->printFieldValues();
    system("pause");
}

L6 Slide 12 Code Example

#include <iostream>
using namespace std;
 
class A {
    int a;
    public:
        A(){a = 1;};
        virtual void print(){
            cout << a << endl;
        };
 
        void printa(){
            cout << a << endl;
        }
};
 
class B : public A {
    int a;
    public:
        B(){a = 2;};
 
        virtual void print(){
            cout << a<< endl;
        }
};
 
int main() {
  B *b = new B();
  A *a = dynamic_cast<A*>(b);
  b->print();
  b->printa();
  a->print();
  a->printa();
 
  B b2;
  A a2 = static_cast<A>(b2);
  a2.print();
  a2.printa();
}

Exercises

TBA

Notes on the lecture

See slides from the textbook for Multiple Inheritance

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License