Bird
Raised Fist0
C++programming~5 mins

Access control in inheritance in C++

Choose your learning style10 modes available
Introduction
Access control in inheritance helps decide which parts of a parent class can be used or changed by a child class. It keeps code safe and organized.
When you want to hide some details of a parent class from its child classes.
When you want child classes to use some parts of the parent class but not all.
When you want to control how child classes access or change parent class data.
When you want to protect important data from accidental changes in child classes.
Syntax
C++
class DerivedClass : access_specifier BaseClass {
    // members
};
access_specifier can be public, protected, or private.
It controls how the base class members are inherited by the derived class.
Examples
Public inheritance keeps public members public in the child.
C++
class Base {
public:
    int x;
};

class Derived : public Base {
    // x is public here
};
Protected inheritance keeps public and protected members protected in the child.
C++
class Base {
protected:
    int y;
};

class Derived : protected Base {
    // y is protected here
};
Private inheritance makes all inherited members private in the child.
C++
class Base {
private:
    int z;
};

class Derived : private Base {
    // Base members are private here
};
Sample Program
This program shows how public, protected, and private inheritance affect access to base class members in derived classes and outside them.
C++
#include <iostream>
using namespace std;

class Base {
public:
    int a = 10;
protected:
    int b = 20;
private:
    int c = 30;
};

class PublicDerived : public Base {
public:
    void show() {
        cout << "a = " << a << "\n";   // accessible
        cout << "b = " << b << "\n";   // accessible
        // cout << "c = " << c << "\n"; // not accessible, private in Base
    }
};

class ProtectedDerived : protected Base {
public:
    void show() {
        cout << "a = " << a << "\n";   // accessible
        cout << "b = " << b << "\n";   // accessible
    }
};

class PrivateDerived : private Base {
public:
    void show() {
        cout << "a = " << a << "\n";   // accessible
        cout << "b = " << b << "\n";   // accessible
    }
};

int main() {
    PublicDerived pd;
    pd.show();
    cout << "pd.a = " << pd.a << "\n"; // accessible

    ProtectedDerived prd;
    prd.show();
    // cout << prd.a; // error: 'a' is protected in ProtectedDerived

    PrivateDerived pvd;
    pvd.show();
    // cout << pvd.a; // error: 'a' is private in PrivateDerived

    return 0;
}
OutputSuccess
Important Notes
Private members of the base class are never accessible directly in derived classes.
Public inheritance models 'is-a' relationship, keeping access levels mostly unchanged.
Protected inheritance hides public members from outside but keeps them accessible to derived classes.
Private inheritance hides all base class members from outside and derived classes.
Summary
Access control in inheritance controls how base class members are seen in derived classes.
Public inheritance keeps public members public, protected inheritance makes them protected, and private inheritance makes them private.
Private members of the base class are always hidden from derived classes.

Practice

(1/5)
1. In C++, if a class Derived inherits from a class Base using public inheritance, what happens to the public members of Base in Derived?
easy
A. They are not accessible in Derived.
B. They remain public in Derived.
C. They become private in Derived.
D. They become protected in Derived.

Solution

  1. Step 1: Understand public inheritance effect

    In public inheritance, the access levels of the base class members remain the same in the derived class.
  2. Step 2: Apply to public members

    Public members of the base class stay public in the derived class, so they can be accessed from outside through derived objects.
  3. Final Answer:

    They remain public in Derived. -> Option B
  4. Quick Check:

    Public members stay public with public inheritance [OK]
Hint: Public inheritance keeps public members public [OK]
Common Mistakes:
  • Thinking public members become protected or private
  • Confusing inheritance types
  • Assuming private members become accessible
2. Which of the following is the correct syntax for declaring a class Derived that inherits privately from a class Base in C++?
easy
A. class Derived : private Base {};
B. class Derived : public Base {};
C. class Derived : protected Base {};
D. class Derived private Base {};

Solution

  1. Step 1: Recall private inheritance syntax

    Private inheritance is declared using the keyword private before the base class name.
  2. Step 2: Match syntax options

    class Derived : private Base {}; uses private correctly: class Derived : private Base {};
  3. Final Answer:

    class Derived : private Base {}; -> Option A
  4. Quick Check:

    Private inheritance uses 'private' keyword [OK]
Hint: Use 'private' keyword for private inheritance [OK]
Common Mistakes:
  • Omitting inheritance access specifier
  • Using 'public' or 'protected' instead of 'private'
  • Confusing default inheritance for classes
3. Consider the following code:
class Base {
public:
    int x;
protected:
    int y;
private:
    int z;
};

class Derived : protected Base {
public:
    void func() {
        x = 1;
        y = 2;
        // z = 3; // Line A
    }
};

int main() {
    Derived d;
    // d.x = 5; // Line B
    return 0;
}

Which statements are true?
medium
A. Line A causes a compile error; Line B causes a compile error.
B. Line A is fine; Line B causes a compile error.
C. Line A causes a compile error; Line B is fine.
D. Both Line A and Line B are fine.

Solution

  1. Step 1: Analyze access inside Derived::func()

    Private member z of Base is not accessible in Derived, so Line A causes a compile error.
  2. Step 2: Analyze access from main()

    Derived inherits Base protectedly, so Base's public x becomes protected in Derived. Hence, d.x is inaccessible outside Derived, so Line B causes a compile error.
  3. Final Answer:

    Line A causes a compile error; Line B causes a compile error. -> Option A
  4. Quick Check:

    Private hidden; protected inheritance hides public as protected [OK]
Hint: Private members always inaccessible; protected inheritance hides public [OK]
Common Mistakes:
  • Assuming private members accessible in derived
  • Thinking public members stay public with protected inheritance
  • Ignoring access from outside derived class
4. Given the code below, identify the error and fix it:
class Base {
private:
    int secret;
public:
    Base() : secret(42) {}
};

class Derived : public Base {
public:
    void reveal() {
        std::cout << secret << std::endl;
    }
};
medium
A. No error; code works fine.
B. Error: 'secret' is private in Base; fix by inheriting Base privately.
C. Error: 'secret' is private in Base; fix by using virtual inheritance.
D. Error: 'secret' is private in Base; fix by making 'secret' protected.

Solution

  1. Step 1: Identify access error

    Member secret is private in Base, so Derived cannot access it directly in reveal().
  2. Step 2: Fix access by changing access specifier

    Changing secret to protected allows Derived to access it directly.
  3. Final Answer:

    Error: 'secret' is private in Base; fix by making 'secret' protected. -> Option D
  4. Quick Check:

    Private members hidden; protected allows derived access [OK]
Hint: Private members inaccessible; use protected for derived access [OK]
Common Mistakes:
  • Trying to access private members directly
  • Using friend function incorrectly
  • Changing inheritance type unnecessarily
5. You have a base class Base with a public member int a; and a protected member int b;. You want to create a derived class Derived that hides a from outside users but allows access to b inside Derived and its subclasses. Which inheritance type should you use?
hard
A. Public inheritance
B. Private inheritance
C. Protected inheritance
D. No inheritance; copy members manually

Solution

  1. Step 1: Understand access changes with inheritance types

    Public inheritance keeps a public, so it remains accessible outside. Private inheritance hides a but also hides b from further derived classes.
  2. Step 2: Choose inheritance that hides a but keeps b accessible in Derived and subclasses

    Protected inheritance makes public members protected (hiding a from outside) and keeps protected members protected (allowing access to b in Derived and subclasses).
  3. Final Answer:

    Protected inheritance -> Option C
  4. Quick Check:

    Protected inheritance hides public, keeps protected accessible [OK]
Hint: Protected inheritance hides public, keeps protected accessible [OK]
Common Mistakes:
  • Choosing public inheritance which keeps public members public
  • Choosing private inheritance which hides protected members too
  • Thinking manual copying is needed
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard