2079 8

CSC166 ยท 2079 ยท 5 marks

What are the various class access specifies? How public inheritance differs from private inheritance?

What are the various class access specifies? How public inheritance differs from private inheritance? [5]

Object Oriented ProgrammingChapter 5: InheritanceAccess Specifiers

Quick answer

A class uses access specifiers to control the visibility of its data members and member functions.

Class Access Specifiers and Public vs Private Inheritance

Access Specifiers in C++

A class uses access specifiers to control the visibility of its data members and member functions. There are three access specifiers in C++:


1. Private

  • Members declared as private are visible only to member functions of their own class.
  • They cannot be accessed from outside the class or by derived classes directly.
  • If no access specifier is mentioned, members are private by default.
class Example {
    int x;  // private by default
private:
    int y;  // explicitly private
};

2. Protected

  • Members declared as protected are visible to member functions of their own class and derived classes.
  • They cannot be accessed from outside these classes (data hiding is maintained).
  • Useful when inheritance is involved.
class Example {
protected:
    int x;  // accessible in derived class
};

3. Public

  • Members declared as public are visible to all functions in the program.
  • They can be accessed from anywhere using the dot operator (.) with an object.
class Example {
public:
    int x;  // accessible from anywhere
};

Summary Table

Access SpecifierOwn ClassDerived ClassOutside Class
privateYesNoNo
protectedYesYesNo
publicYesYesYes

Public Inheritance vs Private Inheritance

When a derived class inherits from a base class, the mode of inheritance determines how the base class members are accessible in the derived class.


Public Inheritance

class Derived : public Base {
    // ...
};
  • public members of Base remain public in Derived.
  • protected members of Base remain protected in Derived.
  • private members of Base are not accessible in Derived.
  • Objects of the derived class can access the public members of the base class.
  • This represents an "is-a" relationship.

Private Inheritance

class Derived : private Base {
    // ...
};
  • public members of Base become private in Derived.
  • protected members of Base become private in Derived.
  • private members of Base are not accessible in Derived.
  • Objects of the derived class cannot access any inherited members from outside.
  • This represents a "has-a" (implementation) relationship.

Comparison Table

FeaturePublic InheritancePrivate Inheritance
Base public membersRemain public in DerivedBecome private in Derived
Base protected membersRemain protected in DerivedBecome private in Derived
Base private membersNot accessibleNot accessible
Access via objectAllowedNot allowed
Relationship"is-a""has-a"

Example

class Base {
public:
    int x;
protected:
    int y;
private:
    int z;
};

class PublicDerived : public Base {
    // x is public, y is protected, z is not accessible
};

class PrivateDerived : private Base {
    // x is private, y is private, z is not accessible
};

int main() {
    PublicDerived pd;
    pd.x = 10;   // Allowed (x is public)

    PrivateDerived pvd;
    pvd.x = 10;  // ERROR: x is private in PrivateDerived
}

Key Point: Public inheritance preserves the access levels of base class members, while private inheritance makes all inherited members private, restricting access from outside the derived class.

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.