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
- 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 ++
May be interested
- 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.
- 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 ++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 ++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 ++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 ++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.