Notes
C Plus Plus
C Plus Plus
  • Roadmap
    • 🛣️C Plus Plus: Docs
  • Build
    • 🔥Build Process
    • 🔧Connect multiple C++ Files
  • Code
    • ⏮️Pre-Processors
      • #include
      • #define
      • #ifdef
      • #pragma
      • Predefined Macros
    • LValue and RValue
    • 🪅Data Types
      • Enum
      • TypeDef
      • const in c++
      • extern vs inline
    • 🎭Casting
    • 🔃Operator overloading
      • Available Operators
      • Examples of operator overloading
    • 🗺️Namespace
      • Namespace Example
      • Using directive
    • 🕵️Header File
      • For C++ Classes
    • 🏗️Structure
      • Struct vs Class
      • Public vs Private inheritance
    • 🏢Classes
      • Friend Function
      • Copy Constructor
      • Explicit Constructor
      • Move Constructor
        • Move Semantics
      • Other constructors
      • Virtual functions
      • Pure virtual function
      • Other function declaration
      • const function vs final function
  • Memory
    • 🧠Memory Introduction
    • ✨Heap and Stack
    • 🎯Pointers
      • Dangling Pointer
      • 'this' Pointer
      • Function Pointer
      • Smart Pointers
        • Unique Pointer
        • Shared Pointer
        • Weak Pointer
      • Reference count
    • 👨‍🏭Helper function
    • 🍡Vector [ArrayList]
      • Custom vector, part 1
      • Custom vector, part 2
      • Custom vector, part 3
      • std::vector
    • ♻️Union
      • Type Punning
      • Type Punning, part 2
      • Type Punning, part 3
      • Union, part 1
      • Union, Part 2
  • Thread
    • 🧵Threading
      • std::thread
      • Detach a thread
  • Misc
    • 🗂️Execution Order
    • 🧠Print memory
Powered by GitBook
On this page
  1. Code
  2. Structure

Public vs Private inheritance

Public and private inheritance are important concepts in C++ that affect how the members of a base class are accessed in the derived class.

  1. Public Inheritance:

    • Syntax: class Derived : public Base

    • Public members of the base class remain public in the derived class.

    • Protected members of the base class remain protected in the derived class.

    • Private members of the base class remain inaccessible in the derived class.

    • Preserves the "is-a" relationship between base and derived classes.

  2. Private Inheritance:

    • Syntax: class Derived : private Base

    • Public and protected members of the base class become private in the derived class.

    • Private members of the base class remain inaccessible in the derived class.

    • Often used to implement a "has-a" relationship.

Here's a simple example to illustrate these concepts:

class Base {
public:
    int publicVar;
protected:
    int protectedVar;
private:
    int privateVar;
};

class PublicDerived : public Base {
    // publicVar is public
    // protectedVar is protected
    // privateVar is not accessible
};

class PrivateDerived : private Base {
    // publicVar is private
    // protectedVar is private
    // privateVar is not accessible
};

int main() {
    PublicDerived pub;
    pub.publicVar = 1;  // OK
    // pub.protectedVar = 2;  // Error: protected
    // pub.privateVar = 3;    // Error: private

    PrivateDerived priv;
    // priv.publicVar = 1;    // Error: private in PrivateDerived
    // priv.protectedVar = 2; // Error: private in PrivateDerived
    // priv.privateVar = 3;   // Error: not accessible

    return 0;
}

Key points to remember:

  1. With public inheritance, the public interface of the base class becomes part of the public interface of the derived class.

  2. With private inheritance, all members of the base class become private in the derived class, regardless of their original access level.

  3. Private inheritance is often used when you want to use the implementation of a base class but not inherit its interface.

  4. Public inheritance models an "is-a" relationship (e.g., a Dog is an Animal), while private inheritance often models a "has-a" or "is-implemented-in-terms-of" relationship.

PreviousStruct vs ClassNextClasses

Last updated 9 months ago

🏗️