Post

[C++] Chap 11 - Inheritance

[C++] Chap 11 - Inheritance

Inheritance Basics

Derived class

  • said to “inherit” members from the base class
  • Automatically has base class’s :
    • Member variables
    • Member functions
  • Can then add additional member functions and variables
  • Can redefine existing members and/or add add new members
1
class HourlyEmploee : public Employee // Publicly inherit
  • Note its definition begins with #ifndef : if the macro not defined, define . (same as #pragma once)
  • Derived class interface only lists ‘new’ or ‘to be redefined’ members (or overrides)
  • Cannot omit const tag in derived class basically (since it’s signature of function)
1
2
3
4
5
6
7
8
void HourlyEmployee::printcheck() {
	setNetPay(hour * rate); //Legal : setNetPay is function of parent class
	netPay = hour * rate; //Illeagl
	// Since the function modifies the member variable, must omit 'const'
	// two functions
	// Employee::printcheck() const;
	// HourlyEmployee::printcheck(); is considered differently
	// i.e. Not an override of one to another.

Inheritance Terminology

  • Parent class (base class)
  • Child class (derived class)
  • Ancestor (Class that’s a parent of a parent)
  • Descendant class (Opposite of ancestor)

Constructors of Derived Classes

  • Base class constructors is NOT inhertited in derived classes
  • But can be used in derived class constructor.
  • Includes invocation of Employee class constructor
1
2
HourlyEmployee::HourlyEmployee( ... )
: Employee(theName, theNumber), wageRate(theWageRate), hours(theHours) {}
  • Default version of base class constructor(no arguments)
1
2
HourlyEmployee::HourlyEmployee()
: Employee(), wageRate(0), hours(0) {}
  • Derived class constructor should always invoke one of the base class’s constructors
  • If not, Default base class constructor is automatically called
1
2
HourlyEmployee::HourlyEmployee() 
: wageRate(0), hours(0) {} //Equivalent with the code above
  • Order of constructor call : A→B→C : order when class C is created (Ancestor → Parent → Child )

Using Private Member Variables of the Base Class

  • Derived class inherits private member variables
  • It cannot directly access them by name, not even through derived class member functions
1
2
3
4
5
6
7
8
HourlyEmployee joe("Josephine", "1234", 0, 0);
joe.setName("Mighty-Joe"); //Possible since it uses parent's setter
joe.name = "Mighty-Joe"; //Illegal

void HourlyEmployee::accessName() {
	cout<<name; //Illegal since private member of parent class cannot be accessed
	//even through child class' function.
}
  • Same holds for base class private member functions.
    • Cannot be accessed outside (interface and implementation of the base class)
    • Not even in derived class member function definitions. (Private member variables can be accessed indirectly via accessor or mutator memer functions of parent class, in constrast)

The protected

  • Allows access by “name” in derived class
  • It acts like private in defined class
  • In derived class, considered protected to allow future derivations.

Redefining vs Overloading

  • Different!
  • Redefining in the derived class :
    • SAME parameter list
    • Essentialy re-writes the same function
    • The signatures must be identical
  • Overloading
    • Different parameter list
    • Defined “new” function that takes different parameters
    • Overloaded function must have differnet signatures

Accessing Redefined Base function

  • Specify scope to access redefined base function
1
2
3
4
5
Employee JaneE;
HourlyEmployee SallyH;
JaneE.printCheck(); // Empolyee::printCheck
SallyH.printCheck(); // HourlyEmployee::printCheck
SallyH.Employee::printCheck(); // Calls Employee's printcheck

Functions that are NOT inherited

  • Constructors (but calls the base constructor when initializing derived class’s constructor parameter list)
  • Destructor
  • Copy constructor : but if not defined, generates the “default” one
  • Assignment operator
  • Assignment operator and copy constructors are NOT inherited but can be used in derived class definitions just like Constructor

Assignment Operator Example

1
2
3
4
5
Derived& Derived::operator = (const Derived& rightSide)
{
	Base::operator = (rightSide);
	...
}
  • Calls assignment operator from base class, this takes care of all inherited member variables
  • Would then set new vars from the derived class

Copy Constructor Example

1
2
3
4
Derived::Derived(const Derived& object) : Base(Object), <more_initializations> 
{
...
}
  • Invocate base copy constructor
  • Object’s type is Derived, but it’s also type of Base, allowing it to make Base(Object)

Destructors in Derived Classes

  • When derived class destructor is invoked, automatically cllas base class destructor → no need for explicit call
  • Derived class destructors need only deal with derived class variables
  • Destructor calling order : Opposite of how constructors are called

PFArrayDBak’s alternative implementation

1
2
3
4
5
6
void PFArrayDBak::backup()
{
	usedB = getNumberUsed();
	for (int i = 0;i<usedB;i++)
		b[i] = operator[](i); // same as this-> opeator[](i) 
		//indirect access of a[i] using operator[] function

“Is a” versus “Has a”

  • Inherit or have member class
  • HourlyEmployee is an Employee (is an child class of employee)
  • Employee has a Date (in member)

Protected and Private Inheritance

1
2
class SalariedEmployee : protected Employee
class SalariedEmployee : private Employee
  • Protected : Public in base class → Protected in derived class (private → private)
  • Private : All members in base class become private in derived class

  • The descendent of privatly inheited class cannot access ancestor’s public or protected function because it is defined as a private function in parent class
  • It is considered as private → can access via functions but cannot access outside the class
1
2
3
4
5
6
7
8
9
10
11
12
class A {
public :
	void foo();
};
class B : private A {
};

B b;
b.foo(); //Illegal
A a;
a.foo(); //Legal
b.A::foo(); // Also illegal

Multiple Inheritance

1
2
class derivedMulti : public base1, base2 {}
//Defaulted to private : equivalent : public base1, private base2
This post is licensed under CC BY 4.0 by the author.