二、编写一个规范的类

  书写一个规范的类,一般包含以下四点。其实在许多教科书上都有提及相关内容,为了保证内容完整性,这里再次强调。

 

1、防止头文件重复包含,请在头文件中加入ifndef...;

2、成员变量设为私有;

3、请使用常函数(公有接口)来访问私有数据成员;

4、对于有参数构造函数,请使用初始化列表(字段)的方式来初始化私有数据成员;

 

1、防止头文件重复包含,这个是最基本要求,不多说

#ifndef __COMPLEX__
#define __COMPLEX__

// 类、函数声明
#endif // !COMPLEX_H

2、见complex.h文件 line 24、25

3、常函数见complex.h文件line17、18

4、使用初始化字段来初始化成员变量是一个好习惯,具体见我这篇博客。https://www.cnblogs.com/winslam/articles/9078161.html

 

complex.h文件

 1 
 2 #ifndef __COMPLEX__
 3 #define __COMPLEX__
 4 
 5 #include
 6 using namespace std;
 7 
 8 // 初始化列表,const & 传值, 常函数, return by value or reference
 9 
10 class complex
11 {
12 public:
13     complex();
14     complex::complex(const double real,const double imag)//
15         : real_(real), imag_(imag)
16     {}
17     double real() const;//
18     double imag() const;
19     complex& operator +=(const complex& cmplx);// 成员函数重载
20 
21     ~complex();
22 
23 private://
24     double real_;
25     double imag_;
26 
27friend ostream& operator << (ostream& out, const complex& cmplx); // const
28 };
29 
30 #endif // !COMPLEX_H

注意到27行,是一个友元函数(与上述关键字private无关),当你想要在外部访问对象的私有数据成员的时候,friend函数是一个方法,但是会破坏代码的封装,这时候friend的参数一般都是const类型。

参考我的博客:https://www.cnblogs.com/winslam/articles/9088976.html

 

complex.cpp文件

 1 #include"complex.h"
 2 
 3 complex::complex()
 4 {
 5     this->real_ = 0.;
 6     this->imag_ = 0.;
 7 }
 8 
 9 double complex::real() const
10 {
11     return this->real_;
12 }
13 
14 double complex::imag() const
15 {
16     return this->imag_;
17 }
18 
19 // 成员函数重载
20 complex& complex::operator +=(const complex& cmplx)
21 {
22     this->real_ += cmplx.real_;
23     this->imag_ += cmplx.imag_;
24     return *this;
25 }
26 
27 
28 complex::~complex()
29 {
30 }
31 
32 ostream& operator<<(ostream & out, const complex & cmplx)
33 {
34     out << "(" << cmplx.real_ << "," << cmplx.imag_ << ")";
35     return out ;
36 }

mian.cpp文件

 1 #include
 2 using namespace std;
 3 
 4 #include"complex.h"
 5 
 6 // 非成员函数重载
 7 inline complex
 8 operator +(double real, const complex& cmplx) // 复数左+ 常实数
 9 {
10     return complex(real + cmplx.real(), cmplx.imag());
11 }
12 
13 inline complex
14 operator +(const complex& cmplx, double real)// 复数右+ 常实数
15 {
16     return complex(real + cmplx.real(), cmplx.imag());
17 }
18 
19 inline complex
20 operator +(const complex& cmplx1, const complex& cmplx2)// 复数加法
21 {
22     return complex(cmplx1.real() + cmplx2.real(), cmplx2.imag() + cmplx2.imag());
23 }
24 
25 inline complex
26 operator - (const complex& cmplx)//复数乘以(-1)
27 {
28     return complex(-cmplx.real(), -cmplx.imag());
29 }
30 
31 inline complex
32 operator + (const complex& cmplx)//复数乘以(+1)
33 {
34     return cmplx;
35 }
36 
37 inline bool
38 operator == (const complex& cmplx1, const complex& cmplx2)//...
39 {
40     return cmplx1.real() == cmplx2.real() && cmplx1.imag() == cmplx2.imag();
41 }
42 
43 int main()
44 {
45     complex cp_1st(1.0, 2.0);
46     complex cp_2st(3.0, 4.0);
47     cp_2st += cp_1st;
48     cout << cp_2st << endl;
49     complex cp_3st = 4 + cp_2st;
50     cout << cp_3st << endl;
51     complex cp_4st = cp_3st + cp_2st;
52     cout << cp_4st << endl;
53     cout << -cp_4st << endl;
54     cout << +cp_4st << cp_2st <<  endl;
55     complex cp_5st(1, 2);
56     if (cp_1st == cp_5st)
57     {
58         cout << "==" << endl;
59     }
60     return 1;
61 }

二、编写一个规范的类_第1张图片

 

 以上是结果。注意到主函数中出现非成员函数的操作符重载,这个问题留到后面说。这一篇博客写得很简洁,也很基础,但是很重要。

你可能感兴趣的:(二、编写一个规范的类)