Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

C++ Inheritance: Fundamentals and Advanced Concepts

Tech 1

1. Inheritance Basics

(1) Concept

Objects abstract into classes, and subclasses generalize (or inherit from) parenet classes.

class BaseClass {
    // Base members
};

class DerivedClass : public/protected/private BaseClass { // Inheritance type
    // Derived members
};

(2) Derived Class Creation Process

  1. Absorb Base Members: A base sub-object is created to access the base's members (inheritance grants access, not duplication).
  2. Add New Members (optional).
  3. Hide Base Members (optional): If a derived class has a member with the same name as the base, the base's member is hidden.

Absorption effects:

  • Inheritance Access: Derived classes can access base members (code reuse).
  • Memory Sharing: Base members exist in the derived object's memory (part of the base sub-object).

(3) Access Permissions for Inheritance Types

Three inheritance types:

  • Public Inheritance
  • Protected Inheritance
  • Private Inheritance: The "final" layer, but even private inheritance allows direct derived classes to access the base's public/protected members.

Default is private inheritance if not specified.

Private Inheritance Behavior:
In multi-level inheritance, if one layer uses private inheritance, lower layers cannot access higher base members (direct base's non-private members are still accessible).

Derived Class Access Summary:

  1. Derived classes can never access the base's private members internally.
  2. Derived classes can access all base members except private ones internally.
  3. Externally, derived objects can only access public members of the base (from public inheritance).

(Memory Aid: 1. Private members are inaccessible externally. 2. Inheritance type intersects with base member access permissions.)

Q: Which base members can a derived object access?
A: public members of a public-inherited base.

(2) Inheritance Limitations (*)

  1. Creation/Destruction: Constructors/destructors are not inherited.
  2. Copy Control: Copy constructors/assignment operators are not inherited.
  3. Memory Allocation: operator new/operator delete are not inherited.
  4. Friends: Friendships are not inherited (to preserve encapsulation).

Thus, the 6 compiler-provided functions and friends are not inheritable.

2. Single Inheritance: Object Creation/Destruction

(1) Simple Single Inheritance Structure

  1. If a derived class does not explicitly call a base constructor, the base's default (or parameterized with defaults) constructor is used.

The anonymous base sub-object is created.

  1. If the base has no default constructor and the derived constructor does not explicitly call a base construcotr, the compiler errors (derived object creation is forbidden).

  2. To use a non-default base constructor, explicitly call it in the derived constructor's initializer list.

Constructor Order: Base → Derived
Destructor Order: Derived → Base

(2) Derived Objects with Member Objects

  1. Initialization Order:

    • Constructor: Base sub-object → Member objects (in declaration order) → Derived
    • Destructor: Derived → Member objects (reverse declaration order) → Base sub-object
  2. Initialization Location:
    Member objects are initialized in the initializer list: memberObject(parameters).

3. Multiple Inheritance

(0) Concept

A class inherits from multiple base classes. The diamond inheritance occurs when a class indirectly inherits the same base via two paths, causing data redundancy and ambiguity.

Virtual inheritance resolves this by ensuring a single instance of the shared base.

(1) Multiple Inheritance: Construction/Destruction

class D : public A, B, C { // Defaults to private for B/C
    // D members
};

// To publicly inherit A, B, C:
class D : public A, public B, public C {
    // D members
};
  • Construction Order: Follows the inheritance declaratino order (A → B → C → D).
  • Destruction Order: Reverse of construction (D → C → B → A).

(2) Multiple Inheritance Issues

① Member Access Ambiguity

If a derived class has no print() but multiple bases do, the compiler cannot determine which base to use (ambiguity).

Solutions:

  • Add class scope (not recommended).
  • Hide via a derived class print() (hides base versions).

② Storage Ambiguity (Diamond Inheritance)

The derived class contains multiple base sub-objects (from the diamond structure), causing ambiguity.

Solution: Use virtual inheritance in intermediate bases to share a single base instance.

class A { // Top-level base
public:
    void print() const { /* ... */ }
    double a;
};

class B : virtual public A { // Virtual inheritance
public:
    double b;
};

class C : virtual public A { // Virtual inheritance
public:
    double c;
};

class D : public B, public C {
public:
    double d;
};
  • Virtual Inheritance Memory Layout:
    Intermediate classes (B, C) contain a virtual base pointer to the shared A sub-object. The final derived class (D) has only one A sub-object.

4. Base-Derived Class Conversions

(1) Upcasting (Derived → Base)

Allowed implicitly.

  • Object Assignment: Base b = Derived();
  • Pointer Assignment: Base* ptr = new Derived();
  • Reference Binding: Base& ref = Derived();

(2) Downcasting (Base → Derived)

Not allowed implicitly, but safe with dynamic_cast (for polymorphic types).

5. Derived Class Copy Control

Explicitly call base copy control functions in derived ones.

  • Copy Constructor: Derived::Derived(const Derived& d) : Base(d) { /* ... */ }
  • Assignment Operator: Derived& operator=(const Derived& d) { Base::operator=(d); /* ... */ return *this; }

Rule:

  • If no derived copy control is defined, base copy control is auto-invoked.
  • If derived copy control is defined, base copy control must be explicitly called.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.