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.

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:

Picture 1 of Access Modifier for class in C ++

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:

Picture 2 of Access Modifier for class in C ++

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 ++

« PREV POST
READ NEXT »