【C++】侯捷 C++面向对象高级开发P1-6

视频

侯捷《C++面向对象开发》——动手实现自己的复数类

一些tricks

P2-为了防止Include的先后顺序问题,我们要加入如下命令

#ifndef __COMPLEX__
#define __COMPLEX__
...
#endif

P4-构造函数放在private中

一般情况下,构造函数是要被外界调用的,因此不能放在private中,但是有一种设计模式叫做Singleton,这个模式将够着函数放在private中,只允许获得一个类实例

class A {
public:
    static A& getInstance();
    setup() (...)
private:
	A();
	A(const A& rhs);
	...
};
A& A::getInstance(){
	static A a;
	return a;
}

不改变数据的内置函数,一定要加const


相同的class的各个objects互为friends(友元),可以直接取得数据

class complex
{
    public:
	    complex(double r=0,double i=0):re(r),im(i)
	    {}
	    int func(const complex& param)
	    {return param.re+param.im}
    private:
	    double re,im;
};
...
{
    complex c1(2,3);
    complex c2;
	c2.func(c1);
}

tricks
  1. 数据在private
  2. 传入数据和返回值reference
  3. 类的本体中的这些函数应该要加const的就要加
  4. 尽量用构造函数

P5 操作符重载

临时对象

下面这些函数一定不能以reference作为返回值,因为是全局函数,而且是生成一个新的对象,函数结束后对象删除,若以reference传递会找不到这个对象。所以只能以value传递,慢点就慢点吧~

inline complex
operator + (const complex& x,const complex& y)
{
    return complex(real(x)+real(y),imag(x)+imag(y));

代码

complex.h

#ifndef __MYCOMPLEX__
#define __MYCOMPLEX__
 
#include<iostream>
using namespace std;
class complex; 
// complex&
//   __doapl (complex* ths, const complex& r);
// complex&
//   __doami (complex* ths, const complex& r);
// complex&
//   __doaml (complex* ths, const complex& r);
// ostream&
//   operator << (ostream& os, const complex& x);
 
class complex
{
public:
  complex (double r = 0, double i = 0): re (r), im (i) { }
  complex& operator += (const complex&);
  complex& operator -= (const complex&);
  complex& operator *= (const complex&);
  complex& operator /= (const complex&);
  double real () const { return re; }
  double imag () const { return im; }
private:
  double re, im;
 
  friend complex& __doapl (complex *, const complex&);
  friend complex& __doami (complex *, const complex&);
  friend complex& __doaml (complex *, const complex&);


  friend  ostream&
  operator << (ostream& os, const complex& x);
};
 
 
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);
}
 
inline complex&
__doami (complex* ths, const complex& r)
{
  ths->re -= r.re;
  ths->im -= r.im;
  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
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));
}
 
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));
}
 
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 //reference传递
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;
}
 
#include <cmath>
 
inline complex
polar (double r, double t)
{
  return complex (r * cos (t), r * sin (t));
}
 
inline complex
conj (const complex& x) 
{
  return complex (real (x), -imag (x));
}
 
inline double
norm (const complex& x)
{
  return real (x) * real (x) + imag (x) * imag (x);
}


inline ostream&
operator << (ostream& os, const complex& x)
{
	// return os << '(' << real (x) << ',' << imag (x) << ')';
  return os << '(' << x.re<< ',' << x.im << ')';
}
 
#endif   //__MYCOMPLEX__

main.cpp

#include <iostream>
#include "complex.h"
 
using namespace std;
 
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 << conj(c1) << endl;
  cout << norm(c1) << endl;
  cout << polar(10,4) << endl;
  
  cout << (c1 += c2) << endl;
  
  cout << (c1 == c2) << endl;
  cout << (c1 != c2) << endl;
  cout << +c2 << endl;
  cout << -c2 << endl;
  
  cout << (c2 - 2) << endl;
  cout << (5 + c2) << endl;
  
  return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值