0
点赞
收藏
分享

微信扫一扫

类成员函数public

上善若水的道 2022-02-25 阅读 43

类的成员函数是指那些把定义和原型写在类定义内部的函数,就像类定义中的其他变量一样。
类成员函数有两种定义方法:

  • 将函数定义在类的内部
  • 将函数定义在类的外部,单独使用范围解析运算符 :: 来定义

将函数定义在类的内部

在类定义中定义的成员函数是把函数声明为内联的,即便没有使用 inline 标识符。
例程如下:

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

函数定义在类的外部

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

然后在类外定义该函数:

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

调用public类成员函数

调用成员函数是在对象上使用点运算符(.),这样它就能操作与该对象相关的数据。

Box myBox;          // 创建一个对象
myBox.getVolume();  // 调用该对象的成员函数

使用示例

#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;
}
举报

相关推荐

0 条评论