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]
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
privateare 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
protectedare 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
publicare 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 Specifier | Own Class | Derived Class | Outside Class |
|---|---|---|---|
| private | Yes | No | No |
| protected | Yes | Yes | No |
| public | Yes | Yes | Yes |
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 {
// ...
};
publicmembers of Base remain public in Derived.protectedmembers of Base remain protected in Derived.privatemembers 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 {
// ...
};
publicmembers of Base become private in Derived.protectedmembers of Base become private in Derived.privatemembers 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
| Feature | Public Inheritance | Private Inheritance |
|---|---|---|
| Base public members | Remain public in Derived | Become private in Derived |
| Base protected members | Remain protected in Derived | Become private in Derived |
| Base private members | Not accessible | Not accessible |
| Access via object | Allowed | Not 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.