Class member functions in C ++

A member function of a class is a function that has its definition or prototype inside the class definition like any other variable. It works on any object of the class that is a member, and has access to all members of a class for that object.

A member function of a class is a function that has its definition or prototype inside the class definition like any other variable. It works on any object of the class that is a member, and has access to all members of a class for that object.

We access members of the class by using a member function instead of directly accessing them:

 class Box { public : double chieudai ; // chieu dai cua hop double chieurong ; // chieu rong cua hop double chieucao ; // chieu cao cua hop double thetich ( void ); // tra ve the tich cua hop }; 

Member functions can be defined inside the class definition or use the scope resolution operator :: . Defining a member function inside the class definition will declare the inline function, even if you don't use the inline specifier . Therefore, you can define the Volume () function as follows:

 class Box { public : double chieudai ; // chieu dai cua hop double chieurong ; // chieu rong cua hop double chieucao ; // chieu cao cua hop double thetich ( void ) { return chieudai * chieurong * chieucao ; } }; 

If you like, you can define the same function outside the class by using the scope resolution operator :: as follows:

 double Box :: thetich ( void ) { return chieudai * chieurong * chieucao ; } 

Here, the only important point is that you will have to use the class name immediately before the operator ::. A member function will be called using a dot (.) Operator on an object, where it will manipulate data related only to that object, as follows:

 Box hopQua ; // tao mot doi tuong hopQua . thetich (); // goi ham thanh vien cho doi tuong nay 

Here, we use the above concept to establish and take the value of different members in a class:

 #include using namespace std ; class Box { public : double chieudai ; // chieu dai cua hop double chieurong ; // chieu rong cua hop double chieucao ; // chieu cao cua hop // Khai bao ham thanh vien double thetich ( void ); void layDoDai ( double dai ); void layDoRong ( double rong ); void layChieuCao ( double cao ); }; // phan dinh nghia cac ham thanh vien double Box :: thetich ( void ) { return chieudai * chieurong * chieucao ; } void Box :: layDoDai ( double dai ) { chieudai = dai ; } void Box :: layDoRong ( double rong ) { chieurong = rong ; } void Box :: layChieuCao ( double cao ) { chieucao = cao ; } // ham main cua chuong trinh int main ( ) { Box Box1 ; // Khai bao Box1 la cua kieu Box Box Box2 ; // Khai bao Box2 la cua kieu Box double thetich = 0.0 ; // Luu giu the tich cua Box vao bien thetich // thong tin chi tiet ve box1 Box1 . layDoDai ( 2.3 ); Box1 . layDoRong ( 5.6 ); Box1 . layChieuCao ( 4.5 ); // thong tin chi tiet ve box2 Box2 . layDoDai ( 7.4 ); Box2 . layDoRong ( 4.2 ); Box2 . layChieuCao ( 3.8 ); // the tich cua box1 thetich = Box1 . thetich (); cout << "The tich cua box1 la: " << thetich << endl ; // the tich cua box2 thetich = Box2 . thetich (); cout << "The tich cua box2 la: " << thetich << endl ; return 0 ; } 

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

Picture 1 of Class member functions in C ++

According to Tutorialspoint

Previous lesson: Class (class) and Object in C ++

Next lesson: Access Modifier for class in C / C ++

Update 25 May 2019
Category

System

Mac OS X

Hardware

Game

Tech info

Technology

Science

Life

Application

Electric

Program

Mobile