Derived Class Declaration and Access Specifier in C++
Object Oriented Programming in C++ – Part 7
Forward: In this part of the series, I explain how the access type specifier for the declaration of the derived class affects the access rights that external functions and external classes have on the derived class.
By: Chrysanthus Date Published: 23 Aug 2012
Introduction
Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.
Derived Class Declaration with the public specifier
With the public access specifier the external function or external class can access the inherited public member of the derived class. Read and try the following code, which works:
#include <iostream>
using namespace std;
class MyClass
{
public:
int num1;
protected:
int num2;
private:
int num3;
};
class ChildClass: public MyClass
{
};
int main()
{
ChildClass childObj;
childObj.num1 = 10;
cout << childObj.num1;
return 0;
}
In the above example, the child class inherits the public member num1 from the base class. In the base class, num1 is public. In the derived class declarator, the access specifier is public. So the main function can access the inherited num1 member.
When the access specifier of the derived class declarator is public, the public, protected and private members of the bases class are inherited without their access specifiers (specification) changed. So an external function or external class would access the inherited public members of the derived class. It would not access the inherited protected and private members of the derived class; this is to be expected. However, when the access specifier of the derived class declarator is protected or private the access rights of the inherited public member is affected (see below)
When the access specifier of the derived class declarator is protected, the inherited public member in the derived class becomes inaccessible. The inherited protected and private members retain their access specifications and so are still not accessible by the external functions and external classes.
The following code where the access specifier, protected, has been used in the derived class declarator, will not compile; read through the code, try it and read the error message displayed by the compiler:
#include <iostream>
using namespace std;
class MyClass
{
public:
int num1;
protected:
int num2;
private:
int num3;
};
class ChildClass: protected MyClass
{
};
int main()
{
ChildClass childObj;
childObj.num1 = 10;
childObj.num2 = 20;
childObj.num3 = 30;
return 0;
}
Derived Class Declaration with private Specifier
When the access specifier of the derived class declarator is private, the inherited public member in the derived class still becomes inaccessible. The inherited protected and private members retain their access specifications and so are still not accessible by the external functions and classes.
The following code where the access specifier, private, has been used in the derived class declarator, will not compile; read through the code, try it and read the error message displayed by the compiler:
#include <iostream>
using namespace std;
class MyClass
{
public:
int num1;
protected:
int num2;
private:
int num3;
};
class ChildClass: private MyClass
{
};
int main()
{
ChildClass childObj;
childObj.num1 = 10;
childObj.num2 = 20;
childObj.num3 = 30;
return 0;
}
In the derive class declaration, if the access specifier is omitted, the default, which is private, is assumed.
Access specifiers are the keywords, public, protected and private. Each of them can be used within a class description and in the declaration of a derived class.
When used in a class description, the members can be accessed by external functions (and external classes), derived classes and friends, if the access specifier is public; the members can be accessed only by derived classes and friends, if the access specifier is protected; the members can be accessed just by friends only, if the access specifier is private. I hope you see the three forms of access, when the access specifier is in the class description.
An access specifier can be in the decelerator of a derived class. Only one access specifier can be in the declarator of the derived class. While an access specifier can be in the declarator of the derived class, one, two or all three access specifiers can be in the class description of the base class (all three can still be inside the derived class description). If the access specifier of the declaration (declerator) of the derived class is not public, then the inherited public members in the derived class cannot be accessed from the derived class, by external functions and external classes.
Well, we have seen some new things, let us take a break here and continue in the next part of the series.
Chrys
Related Courses
C++ CourseRelational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT