Access Modifier for class in C ++

Data Hiding is one of the important features of object-oriented programming that allows to prevent the function of a program from directly accessing the internal representation of a class type. Limiting access to class members defined by public, private and protected areas is labeled inside the class body. The public, private and protected keywords are called Access Specifier.

A class can have multiple public, private and protected areas labeled. Each area remains effective until it meets: or another regional label or closing parenthesis of the class body. The default Access Specifier for members and classes is private.

 class Base { public : // tai day la cac thanh vien public protected : // tai day la cac thanh vien protected private : // tai day la cac thanh vien private }; 

Members are public in C / C ++

Public members are accessible from anywhere outside the class but are inside a program. You can set and retrieve the values ​​of public variables without member functions, as in the following example:

 #include using namespace std ; class Line { public : double chieudai ; void setChieuDai ( double dai ); double layChieuDai ( void ); }; // phan dinh nghia cac ham thanh vien double Line :: layChieuDai ( void ) { return chieudai ; } void Line :: setChieuDai ( double dai ) { chieudai = dai ; } // ham main cua chuong trinh int main ( ) { Line line ; // thiet lap chieu dai cua line line . setChieuDai ( 50.2 ); cout << "Do dai cua duong la: " << line . layChieuDai () << endl ; // thiet lap chieu dai cua line ma khong su dung ham thanh vien line . chieudai = 24.7 ; // Dieu nay la OK: boi vi chieudai la public cout << "Do dai cua duong la: " << line . chieudai << endl ; return 0 ; } 

Compiling and running the above C / C ++ program will produce the following results:

Access Modifier for class in C ++ Picture 1

Private member in C / C ++

A variable or a private member function in C / C ++ cannot be accessed, or viewed from outside the class. Only classes and friend functions can access private members in C / C ++.

By default, all members of a class will be private , for example, chieurong is a private member, that is, when you label a member, it will be treated as a member. private .

 class Box { double chieurong ; public : double chieudai ; void setChieuRong ( double rong ); double layChieuRong ( void ); }; 

In fact, we define data in the private area and related public functions so that they can be called from outside the class, like the following program:

 #include using namespace std ; class Box { public : double chieudai ; void setChieuRong ( double wid ); double layChieuRong ( void ); private : double chieurong ; }; // phan dinh nghia cac ham thanh vien double Box :: layChieuRong ( void ) { return chieurong ; } void Box :: setChieuRong ( double rong ) { chieurong = rong ; } // ham main cua chuong trinh int main ( ) { Box box ; // thiet lap chieu dai cua Box, khong su dung ham thanh vien box . chieudai = 50.2 ; // Dieu nay la OK: boi vi chieudai la public cout << "Chieu dai cua Box la: " << box . chieudai << endl ; // thiet lap chieu rong cua Box, khong su dung ham thanh vien // box.chieurong = 22.4; // Tao ra mot Error: boi vi chieurong la private box . setChieuRong ( 22.4 ); // Ban su dung ham thanh vien de thiet lap chieurong. cout << "Chieu rong cua Box la: " << box . layChieuRong () << endl ; return 0 ; } 

Compiling and running the above C / C ++ program will produce the following results:

Access Modifier for class in C ++ Picture 2

Protected member in C / C ++

A protected variable or a member function is quite similar to a private member, but it provides an additional benefit that they can be accessed in subclasses, which are called inheritance classes.

You will learn about inheritance and inheritance in the next chapter. Now, you can check the following example, here, I have inherited a SmallBox subclass from a Box class called Box .

The following example is similar to the above example and here the chieurong member will be accessible by any member function of the SmallBox inheritance class.

 #include using namespace std ; class Box { protected : double chieurong ; }; class SmallBox : Box // SmallBox la mot lop ke thua cua Box. { public : void setChieuRongCon ( double rong ); double layChieuRongCon ( void ); }; // phan dinh nghia cac ham thanh vien cua lop con double SmallBox :: layChieuRongCon ( void ) { return chieurong ; } void SmallBox :: setChieuRongCon ( double rong ) { chieurong = rong ; } // ham main cua chuong trinh int main ( ) { SmallBox box ; // thiet lap chieu rong cua Box boi su dung ham thanh vien box . setChieuRongCon ( 15.3 ); cout << "Do rong cua Box la: " << box . layChieuRongCon () << endl ; return 0 ; } 

Compiling and running the above C / C ++ program will produce the following results:

 Do rong cua Box la : 15.3 

According to Tutorialspoint

Previous article: Function of class members in C ++

Next lesson: Contructor and Destructor in C ++

5 ★ | 2 Vote

May be interested

  • How to Crash a Class in CollegeHow to Crash a Class in College
    in many universities, classes can be impacted. colleges often do not have enough funding to supply enough professors to meet the demand. here are some ways to get into a class when all the spots are full. waitlist the class, if possible....
  • Storage class in C programmingStorage class in C programming
    a storage class defines the scope (visibility) and the lifetime of a variable or / and functions in the c program. the preceding type specifications can be changed. here are the storage classes that can be used in the c program.
  • Interface in C ++ (Abstract class)Interface in C ++ (Abstract class)
    an interface describes the behavior or capabilities of a class in c ++ without signing to a specific implementation of that class.
  • Friend function in C ++Friend function in C ++
    the friend function in c ++ of a class is defined outside of that class scope, but it has access to all private and protected members of that class. even if the prototypes for friend functions appear in the class definition, friend functions are not member functions.
  • How to Write a Class in PythonHow to Write a Class in Python
    in python, classes can help to wrap up data and functionality at the same time. several classes have already been written for us in python 3, called builtins. here are a few: int (integer class), str (string class), list (list class). this...
  • Overload class member access operator (->) in C ++Overload class member access operator (->) in C ++
    the class member access operator (->) can be overloaded, but it's quite complicated.
  • Contructor and Destructor in C ++Contructor and Destructor in C ++
    a contructor class is a special member function of a class that is executed whenever we create new objects of that class.
  • Learn Class and Object in PythonLearn Class and Object in Python
    python is an object-oriented programming language. unlike procedural programming, which emphasizes functions, object-oriented programming focuses on objects. this article quantrimang will explore class and object in depth, inviting readers to follow.
  • Class properties in HTMLClass properties in HTML
    in html, class properties are used to identify one or more class names for html elements.
  • Managing Windows networks using Script - Part 13: The script returns all valuesManaging Windows networks using Script - Part 13: The script returns all values
    in the previous article of this series we came up with a script called displayclassproperties.vbs, which shows the names of the properties of the wmi class. this is what the script content is, by using win32_bootconfiguration as a class, we are connecting to the wmi nickname.