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:

Class member functions in C ++ Picture 1

According to Tutorialspoint

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

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

3.5 ★ | 2 Vote

May be interested

  • Learn Class and Object in PythonLearn Class and Object in Python
    python is an object-oriented programming language. unlike procedural programming, which emphasizes functions, object-oriented programming focuses on objects. this article quantrimang will explore class and object in depth, inviting readers to follow.
  • Cursor to class in C ++Cursor to class in C ++
    a pointer to a class in c ++ is executed in the same way as a pointer to a structure; and to access members of a pointer to a class you use the member access operator in c ++ as the -> operator, as you do with pointers to the structure.
  • Class selector in CSSClass selector in CSS
    in css, class is used to style the element with the specified class.
  • Pseudo-Class in CSSPseudo-Class in CSS
    pseudo-class in css is used to add special effects to some selector.
  • Class (Class) in C #Class (Class) in C #
    when you define a class in c #, you define a blueprint for a data type. this does not really define any data, but it defines the meaning of that class name. that is, what an object of that class consists of, what activities can be performed on that object.
  • Property (Property) in C #Property (Property) in C #
    properties - property is the member named for the layer, structure, and interface. member variables or methods in a class or structure are called fields. attribute is an inheritance of fields and is accessed using the same syntax. they use accessor through the values ​​of private fields that can be read, written, and manipulated.
  • How to Crash a Class in CollegeHow to Crash a Class in College
    in many universities, classes can be impacted. colleges often do not have enough funding to supply enough professors to meet the demand. here are some ways to get into a class when all the spots are full. waitlist the class, if possible....
  • Interface in C ++ (Abstract class)Interface in C ++ (Abstract class)
    an interface describes the behavior or capabilities of a class in c ++ without signing to a specific implementation of that class.
  • How to Write a Class in PythonHow to Write a Class in Python
    in python, classes can help to wrap up data and functionality at the same time. several classes have already been written for us in python 3, called builtins. here are a few: int (integer class), str (string class), list (list class). this...
  • Multiple Inheritance in PythonMultiple Inheritance in Python
    inheritance allows us to declare the new class to re-use the parent class's functions and attributes and extra functions. in this article, quantrimang will continue the lesson with the theme inheritance but at a deeper level, which is the inheritance and the order of method access of the parent classes (method resolution order).