inheritance: C++ supports multiple inheritance and multiple level of inheritance. C++ also supports multiple way to inherit a class. Let's start with multiple inheritance. C++ let's derived class inherit more than one class.
- Constructors are called in the order in which the classes are declared in the derived class declaration not in the order in which they are mentioned in costructor
Example:
class Base1 {
private:
int num;
public:
public Base1(int n) { num = n; }
};
class Base2 {
};
class Derived : public Base, public Base1 {
private:
int num1;
public:
// Base's constructor will be called first rather than Base1
// It will follow class declaration above
Derived(int n1, int n2) : Base1(n2), Base() { num1 = n1;
class Base2 {
Type of inheritance: C++ let's class to be inherited publicly, protectedly or privately. Default access specifier/inheritance is private.
}
};
- In case if both the base classes contains same method or variable, conflict can be resolved using resolution operator and class name.
Example:
class Base1 {
public:
void display();
}
class Base2 {
public:
void display();
...
}
class Derived : public Base1, public Base2 {
}
...
Derived d;
// Conflict resolution
d.Base1::display();
Type of inheritance: C++ let's class to be inherited publicly, protectedly or privately. Default access specifier/inheritance is private.
- In public inheritance public and protected methods of base class remains as public and protected methods of derived class
public:
void display();
protected:
void inheritable();
protected:
void inheritable();
};
class Derived : public Base {
}class DoubleDerived : public Derived {// inheritable is visible here}...DoubleDerived dd;dd.display();
- In private inheritance public and protected methods of base class become private methods of derived class
class Derived : private Base {}class DoubleDerived : public Derived {// nheritable is NOT visible here}...DoubleDerived dd;dd.display(); // Error, not accessible
- Similarly, in protected inheritance public and protected methods of base class becomes protected methods of derived class
class Derived : private Base {}class DoubleDerived : public Derived {// nheritable is visible here}...DoubleDerived dd;dd.display(); // Error, not accessible
Selective private inheritance: It is possible to inherit class privately yet have public access to specific base method.
Example:
class Base {
public:
void display();
};
class Derived : private Base {
public:
// Note that we are specifying only function name parentheses are missing
Base::display;
};
Order of calling of constructors and destructors: In case of In case of inheritance base class's constructor is called first and then the constructor of the derived class is called. It's because when we are in derived class we should be sure that base class's part is already constructed. Similarly when we are in derived class's destructor we can safely assume that base class's part of the object is still there. To justify that derived class's destructor is called prior to base class's destructor.
Note: zero argument constructor, destructor, copy constructor and assignment operators are not inherited.
No comments:
Post a Comment