The functions that are not inherited from the base class to the derived class are constructors & destructors. These two are deal with object creation and deletion, so they have knowledge about their particular class.
There is another function operator=, which is also not inherited by the derived class. Because it also behaves like a constructor to create the object.
Let us see the below example that the functions that are synthesized by the compiler.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
#include <iostream> using namespace std; class BaseClass { public: BaseClass() { cout << "BaseClass()\n"; } BaseClass (const BaseClass&) { cout << "BaseClass(const BaseClass&)\n"; } BaseClass& operator=(const BaseClass&) { cout << "BaseClass::operator=()\n"; return *this; } ~BaseClass () { cout << "~BaseClass()\n"; } }; class DerivedClass { BaseClass gb; public: DerivedClass() { cout << "DerivedClass()\n"; } DerivedClass(const DerivedClass& g) : gb(g.gb) { cout << "DerivedClass(const DerivedClass&)\n"; } DerivedClass(int) { cout << "DerivedClass(int)\n"; } DerivedClass& operator=(const DerivedClass& g) { gb = g.gb; cout << "DerivedClass::operator=()\n"; return *this; } class Other {}; operator Other() const { cout << "DerivedClass::operator Other()\n"; return Other(); } ~DerivedClass () { cout << "~DerivedClass()\n"; } }; class DerivedClass1 : public DerivedClass {}; void f (DerivedClass::Other) {} class DerivedClass2 : public DerivedClass { public: // Default base-class constructor called: DerivedClass2() { cout << "DerivedClass2()\n"; } DerivedClass2(const DerivedClass2& c) : DerivedClass(c) { cout << "DerivedClass2(const DerivedClass2& c)\n"; } DerivedClass2& operator=(const DerivedClass2& c) { DerivedClass::operator=(c); cout << "DerivedClass2::operator=()\n"; return *this; } }; int main() { DerivedClass1 ocDerived1; DerivedClass1 ocDerived (ocDerived1); //! DerivedClass1 d3(1); ocDerived1 = ocDerived; f(ocDerived1); DerivedClass::Other go; //! ocDerived1 = go; // Operator= not synthesized // for differing types DerivedClass2 ocDerived2, ocDerived4 (ocDerived2); ocDerived2 = ocDerived4; } |
The output of the above program is
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
BaseClass() DerivedClass() BaseClass(const BaseClass&) DerivedClass(const DerivedClass&) BaseClass::operator=() DerivedClass::operator=() DerivedClass::operator Other() BaseClass() DerivedClass() DerivedClass2() BaseClass(const BaseClass&) DerivedClass(const DerivedClass&) DerivedClass2(const DerivedClass2& c) BaseClass::operator=() DerivedClass::operator=() DerivedClass2::operator=() |
Inheritance and static member functions:
The static member functions behave like non-static members of the class
We can inherit them into the derived class. If you redefine a static member, all the other overloaded functions in the base class are hidden.
3. If you change the signature of a function in the base class, all the base class versions with that function name are hidden.
NOTE: The static member functions cannot be virtual.