Wednesday, 3 December 2014

What are the functions that don’t automatically inherited from the base class?

What are the functions that don’t automatically inherited from the base class?

The functions that are not inherited from the base class to the derived class are constructors & destructors. These two are deals with the object creation and deletion, so they have the knowledge about their particular class.

There is another function operator=, which also not inherited by the derived class. Because it is also behaves like constructor to create the object.

Let us see the below example that the functions that are synthesized by the compiler.

#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
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 behaves 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.

No comments: