Access Specifiers In C++

 

Access Specifiers In C++

There are 3 access specifiers which determine the accessibility / visibility of the class members. 

Access Specifiers

1) public
2) private
3) protected

Let's take a look at an example.

 1 
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
// access_specifier.h
#pragma once
using namespace std;
class base {
// public members of the base class
public:
	void public_func() {
		cout << "Public member function in the base class.\n";
	}
// protected members of the base class
protected:
	void protected_func() {
		cout << "Protected member function in the base class.\n";
	}
// private members of the base class
private:
	void private_func() {
		cout << "Private member function in the base class.\n";
	}
};

In the above header file, we have defined a class named base. on line 6, we specified the "public" access specifier and under it, we defined a public function named public_func().

On line 11, we specified the "protected" access specifier and under it, we defined a protected function named protected_func().

On line 16, we specified the "private" access specifier and under it, we defined a private function named private_func().

Note :- If we do not specify any access specifier, then by default the members of the class are private.

 1 
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
// access_specifier.cpp
#include<iostream>
// Header file which contains the base and derived class
#include "access_specifier.h"
using namespace std;

int main() {
	base obj_b;              // creating an object of the base class
	obj_b.public_func();     // ok: accessing the public member of the base class
	obj_b.protected_func();  // error: accessing the protected member of the base class
	obj_b.private_func();    // error: accessing the private member of the base class
	return 0;
}

In the above code, on line 8, we made an object named obj_b of the base class.

base obj_b;              // creating an object of the base class

On line 9, we call the public function named public_func() through the object obj_b.

obj_b.public_func();     // ok: accessing the public member of the base class

But on line 10 and 11,

obj_b.protected_func();  // error: accessing the protected member of the base class
obj_b.private_func();  // error: accessing the private member of the base class

through the object obj_b, we try to access the functions protected_func() and private_func(), which are defined in the protected and private access specifiers respectively in the base class, which is an error because the protected members (except by the derived class) and private members of the base class cannot be accessed by code outside the class.

Although we can access the the protected and private members through a public function.

Lets look at an example.

 1 
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
// access_specifier.h
#pragma once
using namespace std;
class base {
public:
	void public_func() {
		cout << "Public member function in the base class.\n";
	}
	// public function that has access to the protected and private members of the base class
	void pro_pri_mem() {
		protected_func();
		private_func();
	}
protected:
	void protected_func() {
		cout << "Protected member function in the base class.\n";
	}
private:
	void private_func() {
		cout << "Private member function in the base class.\n";
	}
};

The members under the class have access to the other members of the class whether they are public, protected or private. So the public members can access the protected and private members of the class. Thus we can call the protected_func() and private_func() functions in pro_pri_mem() which is a public function.

 1 
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
// access_specifier.cpp
#include<iostream>
// Header file which contains the base and derived class
#include "access_specifier.h"
using namespace std;

int main() {
	base obj_b;              // creating an object of the base class
	obj_b.public_func();     // ok: accessing the public member of the base class
	obj_b.pro_pri_mem();	 // accessing the public function
	return 0;
}

In the above code on line 10, we call the pro_pri_mem() function which calls the protected and private functions of the class.

Inheritance and Access control

The access specifiers also play a role in inheritance (a class derived from a base class). They decide the visibility mode of the base class members for the derived class. A "derived class" can be inherited from "public", "protected" or "private" base class.

When a derived class is inherited from a public base class, the public and protected members of the base class are respectively the public and protected members of the derived class. The derived class cannot access the private members of the base class.

When a derived class is inherited from a protected base class, the public members and the protected members of the base class, both are protected members of the derived class. Here also, private members of the base class are not accessible in the derived class.

Finally if the derived class is inherited from a private base class, the public and protected members of the base class are private members of the derived class and the private members of the base class are not accessible in the derived class.

Let's see an example,

 1 
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// access_specifier.h
#pragma once
using namespace std;
class base {
public:
	void public_func() {
		cout << "Public member function in the base class.\n";
	}
protected:
	void protected_func() {
		cout << "Protected member function in the base class.\n";
	}
private:
	void private_func() {
		cout << "Private member function in the base class.\n";
	}
};
// inheriting a derived class from a public base class
class derived_1 : public base {
public:
	void der_public_func_1() {    // defining public function
		public_func();	      // derived class can access public members of the base class
		protected_func();     // derived class can access protected members of the base class
	        private_func();	      // error: derived class cannot access private members of the base class
	}
};
// inheriting a derived class from a protected base class
class derived_2 : protected base {
public:
	void der_public_func_2() {    // defining public function
		public_func();	      // derived class can access public members of the base class
		protected_func();     // derived class can access protected members of the base class
	        private_func();	      // error: derived class cannot access private members of the base class
	}
};
// inheriting a derived class from a private base class
class derived_3 : private base {
public:
	void der_public_func_3() {    // defining public function
		public_func();	     // derived class can access public members of the base class
		protected_func();    // derived class can access protected members of the base class
	        private_func();	     // error: derived class cannot access private members of the base class
	}
};

In the above code we inherited 3 derived classes, on line 19, derived_1 was inherited from the public base class,

class derived_1 : public base {

 on line 28 derived_2 was derived from the protected base class

class derived_2 : protected base {

 and on line 37, derived_3 was inherited from the private base class.

class derived_3 : private base {

In each of the derived classes we define a public function in which we try to access the members inherited from the base class.

Since class derived_1 is inherited from a public base class, public_func() and protected_func() function members of the base class are also public and protected respectively in derived_1.

derived_2is inherited from a protected base class, so both  public_func() and protected_func() function members of the base class are protected in derived_2.

And derived_3 is inherited from a private base class, so both the public_func() and protected_func() function members of the base class are private in derived_3.

The derived classes cannot access the private_func() function member of the base class since it is a private member.

Let's see an example of this.

 1 
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
// access_specifier.cpp
#include<iostream>
#include "access_specifier.h"
using namespace std;

int main() {
	// object of the derived class inherited from public base class
	derived_1 derived_obj_1;		 
	// object of the derived class inherited from protected base class
	derived_2 derived_obj_2;		 
	// object of the derived class inherited from private base class
	derived_3 derived_obj_3;		 

	// public_func() is public as the derived class is inherited from the public base class
	derived_obj_1.public_func();	 
	// error: public_func() is protected as the derived class is inherited from the protected base class
	derived_obj_2.public_func();	 
	// error: public_func() is private as the derived class is inherited from the private base class
	derived_obj_3.public_func();	 

	return 0;
}

In the above code, on line 8, we created an object of class derived_1 which is inherited from public base class.

derived_1 derived_obj_1;

On line 10, we created an object of class derived_2 which is inherited from protected base class.

derived_2 derived_obj_2;		 

And on line 12, we created an object of class derived_3 which is inherited from private base class.

derived_3 derived_obj_3;		 

Now on line 15, we try to access public_func() through derived_obj_1 which is an object of class derived_1.

derived_obj_1.public_func();	 

Here public_func() is a public member of the class derived_1 as it is inherited from the public base class. 

But on line 17, we try to access public_func() through derived_obj_2 which is an object of class derived_2.

derived_obj_2.public_func();

It is an error because here public_func() is protected member of class derived_2 as it is inherited from the protected base class.

Likewise on line 19, we try to access  public_func() through derived_obj_3 which is an object of class derived_3.

derived_obj_3.public_func();

It is also an error because here public_func() is private member of class derived_3 as it is inherited from the private base class.

Comments