Constructors under Multiple Inheritance Ambiguity in Multiple Inheritance Multiple Inheritance with a Common Base
The names of both the base classes are provided separated by a comma. The rules of inheritance and access for multiple inheritance are the same as for single inheritance.
Linux, C, C++ / Object Oriented Programming with C++ / Session 12 / 5 of 37
Constructors (1)
class Teacher{ private: int x; public: Teacher(){x =0;} //constructors Teacher(int s){x = s;} }; class Student{ private: int y; public: Student(){y = 0;} //constructor Student(int a){y = a;} };
Linux, C, C++ / Object Oriented Programming with C++ / Session 12 / 6 of 37
Constructors (2)
class Teach_asst: public Teacher,public Student { private: int z; public: Teach_asst():Teacher(),Student() //constructor {z = 0;} Teach_asst(int s,int a,int b): Teacher(s),Student(a) {z = b;} };
Linux, C, C++ / Object Oriented Programming with C++ / Session 12 / 7 of 37
Constructors (3)
Constructors have to be defined to initialise the data members of all classes. The names of the base class constructor follow the colon and are separated by commas: If the constructors had arguments then:
Teach_asst(int s, int a, int b): Teacher(s), Student(a) {z = b;} arg for Teacher class arg for Student class arg for this class call Teacher constructor call Student constructor set own data member
Base classes as they appear in the list of base classes: Teacher, Student Member objects are initialised next, in the order they appear in the derived class declaration. The object itself (using the code in its constructor).
The destructor of the class is called first, then those of member objects, and then the base classes.
//ambiguous: cannot be
To access the correct function or data member you will need to use the scope resolution operator. obj.Alpha::display(); obj.Beta::display();
Linux, C, C++ / Object Oriented Programming with C++ / Session 12 / 11 of 37
Common Base
Both Teacher and Student contain a copy of the Person class members.
Teaching assistant is derived from both Teacher and Student it contains two copies of the Person class members giving rise to ambiguity between the base class data members.
Another problem is that declaring an object of class Teaching assistant will invoke the Person class constructor twice.
The call for the destructor follows the same rules, but in the reverse order.
Pointers to Objects
Pointers can point to objects as well as to simple data types. Base* ptr; ptr = new Base; To refer to member functions in the object pointed to by ptr we cannot use a dot operator. Since ptr is a pointer the arrow operator (->) is used to access a member function, ptr->show();
Output
Draw Base Draw Base
A virtual function allows derived classes to replace the implementation provided by the base class. The compiler makes sure the replacement is always called whenever the object in question is actually of the derived class.
Polymorphism
Polymorphism" means "many forms".
It is the process of defining a number of objects of different classes in a group and using different function calls to carry out the operations of the objects.
For example: Class mammals: Different mammals have different responses to the function EAT.
The facility to invoke an appropriate function from any of the given classes using the pointer of the base class is a form of polymorphism.
Dynamic binding
Non-virtual member functions are selected statically (at compile-time) based on the type of the pointer (or reference) to the object. Virtual member functions are resolved dynamically (at runtime) based on the type of the object. Requires some overhead in processing but provides increased power and flexibility in programming. If the keyword virtual is used with the function declaration in the base class, the system will use dynamic binding else static binding will be used.
Linux, C, C++ / Object Oriented Programming with C++ / Session 12 / 29 of 37