Access Specifiers in C++ Classes
Object Oriented Programming in C++ – Part 5
Forward: In this part of the series I explain access specifiers in C++ classes.
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.
Class Structure
The structure of a class is as follows:
class className {
accessSpecifier1:
members for accessSpecifier1
accessSpecifier2:
members for accessSpecifier2
accessSpecifier3:
members for accessSpecifier3
accessSpecifier4:
members for accessSpecifier4
...
}
An access specifier is the keyword, public or protected or private. Every member (property or method) must be under an access specifier. To do this you type the access specifier, then you type a colon. After that you type the members for that specifier, until you type another specifier, which will have its own members. Of course the last specifier and its members do not have any specifier below its set.
A class description must have at least one access specifier. If no access specifier is there (typed), then the default access specifier, which is private, is assumed for all the members.
Members of a class can access other members (properties and methods) of the same class. Functions, operators and other classes (corresponding objects) outside the class description of a particular class can also access members of that class. An access specifier decides whether or not a function or operator or class, outside the class description can access the members it controls inside its class description. The members an access specifier controls are the members typed under it in the class description (until the next specifier).
I will use functions and classes in the illustrations of accesses to class members. I will not use operators for the illustrations.
I will be using the phrase, external function. This refers to a function or class method that is not a member of the class description in question. When I say an external function can access a class member, I mean the external function can use the name (identifier of property or name of method) of the member as its argument or as an identifier inside its definition.
The public Access Specifier
With the public access specifier, an external function can access the public members of the class. The following code illustrates this (read the explanation below):
#include <iostream>
using namespace std;
class Calculator
{
public:
int num1;
int num2;
int add()
{
int sum = num1 + num2;
return sum;
}
};
int myFn(int par)
{
return par;
}
int main()
{
Calculator obj;
obj.num1 = 2;
obj.num2 = 3;
int result = obj.add();
cout << result; cout << "\n";
int myVar = myFn(obj.num1);
cout << myVar;
return 0;
}
There are two functions in the code: myFn() and main(). The first line in the main function instantiates a class object called, obj. In main, lines 2 and 3 use the properties of the class as identifiers. Because the class members are public, the main() function can access the members of the class, in its definition. Line 4 of the main function also demonstrates this. In line 6 of the main function, the function, myFn() uses the property num1 of the class as its argument. It could do so because the member, num1 is public in the class.
With the private access specifier, an external function cannot access the private members of the class. With the private specifier, only a member of a class can access the private member of the class. The following code shows how only a member of a class can access a private member of the class (read the explanation below):
#include <iostream>
using namespace std;
class Calculator
{
private:
int num1;
int num2;
public:
int add()
{
num1 = 2;
num2 = 3;
int sum = num1 + num2;
return sum;
}
};
int main()
{
Calculator obj;
int result = obj.add();
cout << result;
return 0;
}
The class has two private members (properties) and one public member (method). In the class description, the add() method uses the names of the private members as identifiers. So the add() method, a member of the class has accessed the private members of the class.
The main function definition (second line) has been able to access the add() method of the class because the add() method is public (it has a public access specifier).
#include <iostream>
using namespace std;
class Calculator
{
private:
int num1;
int num2;
public:
int add()
{
num1;
num2 = 3;
int sum = num1 + num2;
return sum;
}
};
int main()
{
Calculator obj;
obj.num1 = 2;
int result = obj.add();
cout << result;
return 0;
}
The second line in the main function is wrong because at that line, main tries to access (use as identifier) the private member, num1.
The protected Access Specifier
If a member of a class is public, it can be accessed by an external function including a derived class. If a member of a class is private, it cannot be accessed by an external function; even a derived class cannot access it.
The question is, should a derived class not really be able to access a private member of its base class (since the derived class and base class are related)? Well, to solve this problem you have another access specifier called, protected. If a member of a class is protected, it can be accessed by a derived class, but it cannot be accessed by an external function. It can also be accessed by members within the class. The following code illustrates how a derived class can access a protected member of a base class:
#include <iostream>
using namespace std;
class Calculator
{
protected:
int num1;
int num2;
};
class ChildCalculator: public Calculator
{
public:
int add()
{
num1 = 2;
num2 = 3;
int sum = num1 + num2;
return sum;
}
};
int main()
{
ChildCalculator myChildObj;
int result = myChildObj.add();
cout << result;
return 0;
}
The base class has just two properties and no method; these properties are protected. The derived class has one method and no property. Inside the derived class, the protected properties of the base class are used as identifiers. Generally, when a derived class is using a member of a base class, it is a method of the derived class that is using the member, as in this example. The above code is OK.
#include <iostream>
using namespace std;
class Calculator
{
protected:
int num1;
int num2;
};
class ChildCalculator: public Calculator
{
public:
int add()
{
num1;
num2 = 3;
int sum = num1 + num2;
return sum;
}
};
int main()
{
Calculator obj;
obj.num1 = 2;
ChildCalculator myChildObj;
int result = myChildObj.add();
cout << result;;
return 0;
}
An external function cannot access a protected member of a class (base class); however, a derived class method can access a protected member of the base class.
Note: A member of a class can access any member of the same class independent of whether the member is public, protected or private. This would take place in the class description.
You should now know the role of the access specifiers: public, protected and private as applied to classes. In one of the following parts of the series, we shall see the role of the access specifiers in the declarator of a derived class.
A public member of a class is accessible by external functions and a derived class. A private member of a class is accessible only by other members of the class; it is not accessible by external functions and it is not accessible by a derived class. A protected member of a class is accessible by a derived class (and other members of the class); it is not accessible by an external function.
Let us stop here for this part of the series and continue in the next.
Chrys
Related Courses
Storage Duration in C++Scopes in C++
Function and Operator Overloading in C++
Specifiers in C++
Some Features of C++ Entities
C++ Preprocessing Directives
Writing a C++ Container
C++ Namespace
Writing a C++ Application
C++ Templates
Exception Handling in C++
Container Library Sequences in C++ Simplified
Associative Container in C++ Simplified
String in C++ Standard Library Simplified
Date and Time in C++ Simplified
C++ Course
Relational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT