OOP C6 Inheritance

Composition

Aggregation

Aggregation models “has-a” relationships and represents an ownership relationship between two objects.

  • aggregating object: The owner object.
  • aggregating class: The owner object’s class.
  • aggregated object: The subject object.
  • aggregated class: The subject object’s class.

An object may be owned by several other aggregating objects.

Composition

An object can contain another object, the relationship between the two is called composition.

  • Composition constructs new object with existing objects.

Composition is actually a special case of the aggregation relationship. If an object is exclusively owned by an aggregating object, the relationship between these two is referred to as composition.

Ways of inclusion

  • Fully
  • By reference (Inclusion by reference allows sharing)

Embedded objects

  • All embedded objects are initialized. The default constructor is called if no arguments supplied and there is a default constructor (or one can be built)
  • Constructors can have initialization list:
    • Any number of objects separated by commas ,
    • Optional
    • Provide arguments to sub-constructors

public vs. private

It is common to make embedded objects private:

  • they are part of the underlying implementation
  • the new class only has part of the public interface of the old class

However, we can also embed as a public object if we want to have the entire public interface of the sub-object available in the new object.

Inheritance

Inheritance is to define new classes from existing classes; is the ability to define the behavior or implementation of one class as a superset of another class.

Inheritance is the best way to design classes to avoid redundancy / reuse the interface.

  • Language implementation technique
  • Also an important component of the OO design methodology
  • Allows sharing of design for
    • Member data
    • Member functions
    • Interfaces
  • Key technology in C++

Base Classes and Derived Classes

Inheritance enables you to define a general class (i.e base class / superclass / parent class) and later extend it to more specialized classes (i.e derived class / subclass / child classes).

The superclass defines common attributes(accessible data fields and functions), while the subclasses inherit the superclass attributes and add their own attributes.

Example

Inheritance hierarchies

Generic Programming

An object of a derived class can be passed wherever an object of a base type parameter is required. That is to say, if a function’s parameter type is a base class, an object of any of the parameter’s derived classes can be passed.

Thus a function can be used generically for a wide range ­of object arguments, which is called generic programming.

Constructors and Destructors

The constructor of a derived class first calls its base class’s constructor before it executes its own code.

The destructor of a derived class first executes its own code then
automatically calls its base class’s destructor.

1
2
3
4
5
6
DerivedClass(paramaterList):BaseClass(){
// Perform initialization
}
DerivedClass(parameterList): BaseClass(argumentList) {
// Perform initialization
}

A constructor in a derived class always invokes a constructor in its base class explicitly or implicitly.

  • If a base constructor is not invoked explicitly, the base class’s no-arg constructor is invoked by default.

    1
    2
    3
    4
    5
    6
    public Circle() {
    radius = 1;
    }
    public Circle(): GeometricObject() {
    radius = 1;
    }
    1
    2
    3
    4
    5
    6
    public Circle(double radius) {
    this->radius = radius;
    }
    public Circle(double radius): GeometricObject(){
    this->radius = radius;
    }

If the base class has a customized copy constructor and assignment operator, you should customize these in the derived classes to ensure that the data fields in the base class are properly copied.

Redefining Functions

A function defined in the base class can be redefined in the derived classes.

  • If we redefine a member function in the derived class, all other overloaded functions in the base class are inaccessible. This is name hiding in C++.

  • Please use scope resolution operator :: with the base class if we want to invoke the overload function in the base class.

    1
    2
    Derived.function(1);           //invoke the redefined function in Derived
    Derived.Base::function(); //invoke the function in Base

Overloading vs. Redefining

  • Overloading a function: a way to provide more than one function with the same name but with different signatures to distinguish them.
  • Redefining a function: the function must be defined in the derived class using the same signature and same return type as in its base class.

Advantage

  • Avoiding code duplication
  • Code reuse
  • Easier maintenance
  • Extendibility

Access protection

RECALL

  • Public: visible to all clients
  • Protected: visible to self, classes derived from self and to friends.
  • Private: visible only to self and to friends!
1
2
3
4
5
6
7
8
9
10
11
12
13
class DerivedA : public Base{
private:
int derivedAttri;
} //Public
class DerivedB : protected Base{
private:
int derivedAttri;
} //Protected
class DerivedC : private Base{
private:
int derivedAttri;
} //Private

Derived is public protected private
public Base public in Derived protectd in Derived hidden
protected Base protected in Derived protected in Derived hidden
private Base(default) private in Derived (kinda composition, but renaming) private in Derived hidden

When is protected not protected?

Protected is public to all derived classes. And the derived classes are ill-behaved!

For this reason, please

  • make member functions protected
  • keep member variables private

When is not inherited?

  • Constructors
    • synthesized constructors use memberwise initialization
    • In explicit copy ctor, explicity call base-class copy ctor or the default ctor will be called instead.
  • Destructors
  • Assignment operation
    • synthesized operator= uses memberwise assignment
    • explicit operator= be sure to explicity call the base class version of operator=
  • Private data is hidden, but still present

OOP C6 Inheritance
http://example.com/2023/04/15/OOP-6/
Author
Tekhne Chen
Posted on
April 15, 2023
Licensed under