Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Classes- Inheritance Multiple Inheritance It is possible to derive a new class from more than one base class. This is called Multiple Inheritance. Under.

Similar presentations


Presentation on theme: "1 Classes- Inheritance Multiple Inheritance It is possible to derive a new class from more than one base class. This is called Multiple Inheritance. Under."— Presentation transcript:

1 1 Classes- Inheritance Multiple Inheritance It is possible to derive a new class from more than one base class. This is called Multiple Inheritance. Under multiple inheritance, a derived class inherits all of the members of its base classes. As before, each of the base classes may be private, public, or protected. The same base member access principles apply. class B1 {//...}; class B2{ //...}; class D : public B1, public B2 { //...};

2 2 Classes- Inheritance Since the base classes have constructors that take arguments, the constructor for the derived class should invoke these in its member initialization list constructorName (parameterList) : initialisationList { } The order in which the base class constructors are invoked is the same as the order in which they are specified in the derived class header. The destructors are applied in the reverse order.

3 3 Classes- Inheritance class point { //...}; class circle : public point{//...}; class rectangle : public point {//...}; class figure : public circle, public rectangle {//...}; The class figure has two member xVal, and two members yVal. The problem is overcome by making virtual base classes. A base class is made virtual by placing the keyword virtual before its name in the derived class header: class circle : virtual public point{//...}; class rectangle : virtual public point {//...};

4 4 Classes- Inheritance Virtual Functions Connecting a function call to a function body is called binding. When binding is performed before the program is run (by the compiler and linker), it’s called early binding. If the binding occurs at runtime, it is called late binding (dynamic binding or runtime binding); it is based on the type of the object. When a language implements late binding, there must be some mechanism to determine the type of the object at runtime and call the appropriate member function. To cause late binding to occur for a particular function, C++ requires that you use the virtual keyword when declaring the function in the base class. virtual void print(void);

5 5 Classes- Inheritance If a function is declared as virtual in the base class, it is virtual in all the derived classes. You don’t need to repeat the keyword virtual in any of the derived-class function redefinitions. Only nonstatic member functions can be declared as virtual. A virtual function redefined in a derived class must have exactly the same prototype as the one in the base class. If the prototypes of virtual functions are not the same, that means the function is overloaded.

6 6 Classes- Inheritance Abstract base classes and pure virtual functions a pure virtual function uses the virtual keyword and is followed by = 0. virtual void X() = 0; A class with a pure virtual function is named abstract class An abstract class is only an interface for its derived classes. If anyone tries to make an object of an abstract class, the compiler prevents them. When an abstract class is inherited, all pure virtual functions must be implemented, or the inherited class becomes abstract as well.

7 7 Classes- Inheritance // Pure virtual base definition class Base { public: virtual void v() const = 0; virtual void f() const = 0; // Inline pure virtual definitions illegal: //! virtual void g() const = 0 { } }; // OK, not defined inline void Base::f() const { cout << "Base::f()\n"; } void Base::v() const { cout << "Base::v()\n";} class D : public Base { public: // Use the common Base code: void v() const { Base::v(); } void f() const { Base::f(); } }; int main() { D d; d.v(); d.f(); } ///:~


Download ppt "1 Classes- Inheritance Multiple Inheritance It is possible to derive a new class from more than one base class. This is called Multiple Inheritance. Under."

Similar presentations


Ads by Google