Access Specifiers: public, private, protected in c++

Access Specifiers: public, private, protected in c++

Class access specifiers in the C++ programming language control or manage the visibility and accessibility of user-defined class members (including both existing class data members and class-declared member functions) within the current program. In a simple C++ program, class visibility features allow you to define multiple individual public, private, and protected behaviors for the variable parameter data types of individual classes declared in the current program.

Access Specifiers public, private, protected in c++

There are three main types of access specifiers in the C++ programming language.

  • Public
  • Private
  • Protected

In C++ programming, the use of public, private, and protected class access specifiers helps control or define the accessibility of class members from internal or derived subclasses within a class.

Public Class Access Specifier in C++.

Variable parameter class members declared as public class data types in a C++ program can be accessed and managed from anywhere in the program. These class-declared data type variables can be accessed and managed internally within the class or through any object of that class, such as a subclass. Remember, user-defined public class members can be called directly from objects of the class and other class functions.

Example of a public class access specifier in C++.

#include <iostream>

using namespace std;

class Triangle {

public:

// Here we declare a public class data member type

int Tribase;

int Triheight;

// Here we declare a public class member function

float DisplaytriArea() {

return 0.5 * Tribase * Triheight;

}

};

int main() {

Triangle tri;

tri.Tribase = 20; // Here we try to access a public data member

tri.Triheight = 10; // Here we try to access a public data member

cout << “Area of ​​triangle with public class member – ” << tri.DisplaytriArea() << endl; // Here it is calling the public class member function

return 0;

}

Explanation of the public class access specifier in C++.

  • The Tribase and Triheight class members declared in this program, as well as the DisplaytriArea() class member function, are declared as public data types. This allows them to be accessed and controlled from outside the class, even in the main function.

Private class access specifier in C++.

Data type members declared as private class data member variables in a C++ program cannot be accessed or controlled from outside the class. Private class members can only be accessed and controlled by member functions or friend functions of the same class. By default, if no class member access specifier is defined in the current class, all data type members of the current class are defined as private.

Defining Private Class Data Type Members in a C++ Program Define data type class members with the private keyword to encapsulate them and prevent unauthorized access or modification. Private class member data types are a key feature of encapsulation in object-oriented programming.

Example of a private class access specifier in C++.

#include <iostream>

using namespace std;

class Triangle {

private:

// here we declare a private triangle class data member type

int Tribase;

int Triheight;

Public:

// here we declare a public class member function data type

void setDimensions(int tb, int th) {

Tribase = tb; //here we Modifying the private class data type

Triheight = th; //here we Modifying the private class data type

}

float DisplaytriArea() {

return 0.5 * Tribase * Triheight;

}

};

int main() {

triangle tri;

tri.setDimensions(20, 10); // here we Accessing private data with public class method data type

cout << “the Area of ​​triangle with private class member – ” << tri.DisplaytriArea() << endl; // here it is Calling public member function

return 0;

}

Explanation of the private class access specifier in C++.

  • Here in this program, the Tribase and Triheight class members declared are private in nature, which means that these private class member data types cannot be directly accessed from outside. For example, access to these private class members in these user-defined classes from the current main() class is only possible through the setDimensions() method, which is a public class method.

Protected Class Access Specifier in C++.

User-defined class member data types declared as protected class members in a C++ program can be accessed as some particular custom class member. Protected class members can be accessed from outside the class by calling member functions of derived classes or subclasses, even if they are defined outside the derived class. Similarly, protected class member data types declared in a user-defined class can only be accessed from outside the class through the derived class.

Declaring protected class member data types in a C++ program is commonly used in designing class hierarchies where C++ users want derived classes or subclasses to have access to certain class members, but do not want to expose them to the outside world.

Example of a protected class access specifier in C++.

#include <iostream>

using namespace std;

class Shape {

protected:

// here we declare a Protected data type members

int TriBase, TriHeight;

Public:

void setDimensions(int tb, int th) {

Tribase = tb;

Triheight = th;

}

};

class Triangle : public Shape {

Public:

// hee we use Public class member function that can access protected class data members of the base class

float DisplayTriArea() {

return 0.5 * Tribase * Triheight; // here we Accessing a protected class members of Shape class

}

};

int main() {

triangle tri;

tri.setDimensions(20, 10); // here we Accessing a protected class members via derived subclass method

cout << “the Area of ​​triangle with protected class member – ” << tri.DisplayTriArea() << endl; // here it is calling the derived class member function

return 0;

}

Explanation of a protected access specifier in C++.

  • In this example, the Tribase and Triheight members in the Shape class are defined as data type protected. Protected class members declared in the Shape class cannot be directly accessed from the main() class function.
  • For example, in this example, the Triangle class, which is a subclass derived from Shape, can access the protected members in the existing class and use its own methods, such as DisplayTriArea().

Access control and inheritance in C++.

When using the inheritance concept in a C++ program, user-defined access specifiers declared in a class affect how base class members and data type variables defined in the current class are inherited and accessed in derived classes or subclasses.

Public inheritance concept in C++.

  • Public class members of a user-defined base class are public in the data type derived class.
  • Whereas protected class members of the base class are protected in the data type derived class.
  • In this, private class members of the base class cannot be accessed in the data type derived class.

Example of public inheritance in C++.

#include <iostream>

class Root {

public:

int publicParametr;

Root() : publicParametr(0) {}

void preview() {

std::cout << “here user define Root class preview() function \n”;

}

};

class Derived : public Root {

Public:

void accessPublicData() {

publicParameter = 100; //here Public class member is accessible

std::cout << “here publicParametr in Derived class is – ” << publicParametr << std::endl;

}

};

int main() {

Derived d;

d.accessPublicData();

d.preview(); // here it Inherited public function member

return 0;

}

Explanation of public inheritance in C++.

  • Here in this example, publicParametr is a public class data type member from the base class, and it is accessible in the derived class, which is defined as a public inheritance. The preview() function is inherited from the base class, and is called in the derived object.

Leave a Reply