概念:
类的成员函数是指那些把定义和原型写在类定义内部的函数,就像类定义中的其他变量一样。
注意点:
- class类声明大括号后面有
;
,与java不同; - 使用范围解析运算符
::
定义的类成员函数,必须在class中声明
;
#include <iostream>
using namespace std;
class Box
{
public:
double height;
double length;
double breath;
double getVolume();
// double getVolume()
// {
// return height * length * breath;
// }
};
double Box::getVolume()
{
return height * length * breath;
}
int main()
{
Box box1;
Box box2;
box1.breath = 2;
box1.height = 3;
box1.length = 3;
cout << "box1的体积为%f" << box1.getVolume()<<endl;
box2.breath = 1;
box2.height = 1;
box2.length = 1;
cout << "box2的体积为%f" << box2.getVolume();
}