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.
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:
According to Tutorialspoint
Previous lesson: Class (class) and Object in C ++
Next lesson: Access Modifier for class in C / C ++
You should read it
- Operator overload and Load overlap in C ++
- The '+' operator in SQL Server
- Friend function in C ++
- Operator in programming C
- Overload subscript operator [] in C ++
- Overload class member access operator (->) in C ++
- Overload the one-seat operator in C ++
- Load the operator stack to call the function () in C ++
- Load operator ++ and - in C ++
- Load the Input / Output operator stack in C ++
- Cursor to class in C ++
- Multiplication function (PRODUCT function) in Excel
Maybe you are interested
Instructions to fix double click error on computer mouse - Click once becomes twice
6 mobile apps to help create double exposure effects
Ripple CEO: Cryptocurrency market capitalization to double by the end of 2024
Fix double typing error on Macbook super fast
5 quick and effective ways to fix mouse double click errors
Double clicking does not open the file, what do you need to do to fix the error?