Latest web development tutorials

C ++ class member functions

C ++ Class & Objects C ++ Class & Objects

Class member functions are those written to define and prototype a function inside a class definition, like the definition of the class of the other variables. Class member function is a member of a class, it can be any type of object manipulation, you can access all the members of the object.

Let's look at previously defined class Box, and now we want to use to access the class member functions member, rather than direct access to the members of these classes:

class Box
{
   public:
      double length;         // 长度
      double breadth;        // 宽度
      double height;         // 高度
      double getVolume(void);// 返回体积
};

Member function can be defined inside a class definition, or used alone to definethe scope resolution operator ::.Member functions defined in the class definition of the function is declaredinline, even without the use of inline identifier.So you can define theVolume () function as follows:

class Box
{
   public:
      double length;      // 长度
      double breadth;     // 宽度
      double height;      // 高度
   
      double getVolume(void)
      {
         return length * breadth * height;
      }
};

You can alsoresolve operator outside the scopeof the definition of class::function as follows:

double Box::getVolume(void)
{
    return length * breadth * height;
}

Here, the need to emphasize that you must use the class name before the :: operator. Call to a member function on the object using the dot operator, so that it can manipulate the data associated with the object, as follows():

Box myBox;          // 创建一个对象

myBox.getVolume();  // 调用该对象的成员函数

Let us use the concept mentioned above to set and get the value of the different classes of members:

#include <iostream>

using namespace std;

class Box
{
   public:
      double length;         // 长度
      double breadth;        // 宽度
      double height;         // 高度

      // 成员函数声明
      double getVolume(void);
      void setLength( double len );
      void setBreadth( double bre );
      void setHeight( double hei );
};

// 成员函数定义
double Box::getVolume(void)
{
    return length * breadth * height;
}

void Box::setLength( double len )
{
    length = len;
}

void Box::setBreadth( double bre )
{
    breadth = bre;
}

void Box::setHeight( double hei )
{
    height = hei;
}

// 程序的主函数
int main( )
{
   Box Box1;                // 声明 Box1,类型为 Box
   Box Box2;                // 声明 Box2,类型为 Box
   double volume = 0.0;     // 用于存储体积
 
   // box 1 详述
   Box1.setLength(6.0); 
   Box1.setBreadth(7.0); 
   Box1.setHeight(5.0);

   // box 2 详述
   Box2.setLength(12.0); 
   Box2.setBreadth(13.0); 
   Box2.setHeight(10.0);

   // box 1 的体积
   volume = Box1.getVolume();
   cout << "Box1 的体积:" << volume <<endl;

   // box 2 的体积
   volume = Box2.getVolume();
   cout << "Box2 的体积:" << volume <<endl;
   return 0;
}

When the above code is compiled and executed, it produces the following results:

Box1 的体积: 210
Box2 的体积: 1560

C ++ Class & Objects C ++ Class & Objects