Table of Contents
Function of Class Members in C ++: Class Member Functions in C ++ is easier to understand when the core ideas are paired with practical examples. The sections below explain the topic clearly, highlight useful steps, and point out details that can prevent common errors.
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 ++
Frequently Asked Questions
What is Function of Class Members in C ++: 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.
Why is Function of Class Members in C ++: Class Member Functions in C ++ important?
A clear understanding of Function of Class Members in C ++: Class Member Functions in C ++ helps you make informed decisions, avoid common mistakes, and use the relevant tools or techniques more effectively.
How should beginners approach Function of Class Members in C ++: Class Member Functions in C ++?
Start with the fundamental concepts, follow the examples step by step, and test each change in a safe environment before applying it to important systems or data.
Was this article helpful?
Your feedback helps us improve.
Reader Comments 0
Sign in with email or Google to join the discussion.