当不想从外部调用成员函数修改成员变量时,可以将其被调成员函数加上const每个成员函数都有个隐式 this 指针,指向调用对象
代码
1 // CBox类头文件
2 #pragma once
3 #include
4 using namespace std;
5
6 class CBox
7 {
8 public:
9 CBox(double lv = 1.0, double lw = 1.0, double lh = 1.0);
10
11 public:
12 double Volume( void ) const;
13 int Compare( CBox xBox ) const;
14
15 private:
16 double m_Length;
17 double m_Width;
18 double m_Height;
19 };
20
21 // CBox类源文件
22 #include "Box.h"
23
24 //构造函数
25 CBox::CBox(double lv, double lw, double lh) 26 : m_Length(lv),m_Width(lw),m_Height(lh)
27 {
28 cout << "构造函数" << endl;
29 }
30
编辑推荐: