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

  • Contructor and Destructor in C ++Photo of 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.
  • Copy constructor in C ++Photo of Copy constructor in C ++
    copy constructor is a constructor that creates an object by initializing it with an object of the same class, which was created earlier.
  • Friend function in C ++Photo of 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.
  • Inline function in C ++Photo of Inline function in C ++
    inline functions in c ++ are powerful concepts that are commonly used with classes. if a function is inline, the compiler places a copy of the code of that function at each location that the function is called at compile time.
  • Cursor this in C ++Photo of Cursor this in C ++
    each object in c ++ has access to its own location through an important pointer called this pointer. the pointer this in c ++ is a hidden parameter to all member functions. so inside a member function, this pointer can refer to the calling object.
  • Cursor to class in C ++Photo of Cursor to class in C ++
    a pointer to a class in c ++ is executed in the same way as a pointer to a structure; and to access members of a pointer to a class you use the member access operator in c ++ as the -> operator, as you do with pointers to the structure.