本文是针对侯捷老师C++课程面向对象高级编程的前六节的学习笔记。
首先将一些要点进行总结
总结:
- 数据要封装在private中
- 参数要尽可能地以reference形式传输,要考虑是否可行:如果参数要变化就不以reference形式传递;要不要加const视情况而定
- 返回值尽可能用reference来传输。如果返回值已经有确定的内存可以存储,那么可以用reference; 如果没有,则只能以local object形式返回。
- 类中的function,如果能加constant的一定要加,否则编译时可能会出错
- 构造函数的标准特殊语法:initization list
对第4点的解释
对第5点的解释
实现代码如下:
complex.h
#ifndef __MYCOMPLEX__ //防卫式声明
#define __MYCOMPLEX__ //保证该头文件只被编译一次
//如果下次又用到这个类要运行这个头文件,由于已经定义了那个宏,重复的代码不会被再次执行
//=============================前置声明=============================
class complex;//声明在这个类中会用到类complex
complex&
__doapl(complex* ths, const complex& r);//全局函数 ,可以直接用
complex&
__doami(complex* ths, const complex& r);
complex&
__doaml (complex* ths, const complex& r);
//=============================类-声明=============================
class complex
{
public:
complex(double r = 0, double i = 0)
: re(r), im(i)
{ } //构造函数
//内联函数 inline
double real() const { return re; } //不能return by reference,因为事先没有给定的memory
double imag() const { return im; }
complex& operator += (const complex&);//函数定义在body外面 //成员函数有隐藏参数this
complex& operator -= (const complex&);
complex& operator *= (const complex&);
private:
double re, im;
//若全局函数使用类中的private数据需要将该函数设定为友元函数
friend complex& __doapl(complex*, const complex&);//函数定义在body外面的函数在body内的声明写出参数类型即可
friend complex& __doami(complex*, const complex&);
friend complex& __doaml(complex*, const complex&);
};
//=============================类-定义=============================
inline complex&
__doapl(complex* ths, const complex& r)
{
ths->re += r.re;
ths->im += r.im;
return *ths;
}
inline complex&
complex::operator += (const complex& r)
{
return __doapl(this, r);
}//谁调用了这个函数谁就是this
inline complex&
__doami(complex* ths, const complex& r)
{
ths->im -= r.im;
ths->re -= r.re;
return *ths;
}
inline complex&
complex::operator -= (const complex& r)
{
return __doami(this, r);
}
inline complex&
__doaml(complex* ths, const complex &r)
{
double f = ths->re * r.re - ths->im * r.im;
ths->im = ths->re * r.im + ths->im * r.re;
ths->re = f;
return *ths;
}
inline complex&
complex::operator *= (const complex& r)
{
return __doaml(this, r);
}
//函数重载
//成员函数重载
inline double//由于返回值没有已经分配好的内存,所以这里不能return by reference
imag(const complex& x)
{
return x.imag;
}
inline double
real(const complex& x)
{
return x.real;
}
//函数重载
//操作符(非成员函数)重载
//加号
inline complex
operator + (const complex& x, const complex& y)
{
return complex(real(x) + real(y), imag(x) + imag(y));
}
inline complex
operator + (const complex& x, double y)
{
return complex(real(x) + y, imag(x));
//也可以写成return complex(x.real() + y, x.imag());
}
inline complex
operator + (double x, const complex& y)
//也可以写成(const double& x, const complex& y)
{
return complex(x + real(y), imag(y));
}
//减号
inline complex
operator - (const complex& x, const complex& y)
{
return complex(real(x) - real(y), imag(x) - imag(y));
}
inline complex
operator - (const complex& x, double y)
{
return complex(real(x) - y, imag(x));
}
inline complex
operator - (double x, const complex& y)
{
return complex(x - real(y), -imag(y));
}
//乘号
inline complex
operator * (const complex& x, const complex& y)
{
return complex(real(x) * real(y) - imag(x) * imag(y),
real(x) * imag(y) + imag(x) * real(y));
}
inline complex
operator * (const complex& x, double y)
{
return complex(real(x) * y, imag(x) * y);
}
inline complex
operator * (double x, const complex& y)
{
return complex(x * real(y), x * imag(y));
}
//除号
complex
operator / (const complex& x, double y)
{
return complex(real(x) / y, imag(x) / y);
}
//正号
inline complex
operator + (const complex& x)
{
return x;
}
//负号
inline complex
operator - (const complex& x)
{
return complex(-real(x), -imag(x));
}
//是否相等
inline bool
operator == (const complex& x, const complex& y)
{
return real(x) == real(y) && imag(x) == imag(y);
}
inline bool
operator == (const complex& x, double y)
{
return real(x) == y && imag(x) == 0;
}
inline bool
operator == (double x, const complex& y)
{
return x == real(y) && imag(y) == 0;
}
//是否不等
inline bool
operator != (const complex& x, const complex& y) {
return real(x) != real(y) || imag(x) != imag(y);
}
inline bool
operator != (const complex& x, double y)
{
return real(x) != y || imag(x) != 0;
}
inline bool
operator != (double x, const complex& y)
{
return x != real(y) || imag(y) != 0;
}
#endif //__MYCOMPLEX__
complex_text.cpp
#include<iostream>
#include "complex.h"
using namespace std;
//操作符重载
ostream&
operator << (ostream& os, const complex& x)
{
return os << '(' << real(x) << ',' << imag(x) << ')';
}
int main()
{
complex c1(2, 1);
complex c2(4, 0);
cout << c1 << endl;
cout << c2 << endl;
cout << c1 + c2 << endl;
cout << c1 - c2 << endl;
cout << c1 * c2 << endl;
cout << c1 / 2 << endl;
cout << +c2 << endl;
cout << -c2 << endl;
}