Boolan C++(一)

BoolanC++

1.基本概念

(1)C++ (c with class) ——> 面向对象程序设计

(2)基于对象:类之间没有关联面向对象:类之间有关联

(3)涉及指针、不涉及指针

(4)最常用版本c++98

(5)语言+标准库2.头文件防卫式声明

#ifndef __MYCOMPLEX__

#define __MYCOMPLEX__

前置声明类:

class 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:(public的函数是可以被外界使用的)

构造函数: 

complex (double r = 0, double i = 0): re (r), im (i) { }

构造函数的函数名要和类名相同,是用来创建对象的。

参数要有默认值,叫做默认实参,否则外界调用创建对象的时候,比如complex c就没有默认值了。

构造函数不需要有返回类型,因为构造函数就是用来构造这个类的。

将传进来的参数给private的数据,采用构造函数特有的一种语法,即initialization list,用来设初值,这里是把r设到re,i设到im。不直接赋值的原因是,先要初始化再赋值,这两个步骤才是完整的,如果省掉这一步直接赋值,结论相同,但是过程不同。

外界创建对象的时候可能会有很多种写法,这样就需要不同的构造函数,c++允许构造函数重载,但是参数个数或者类型要不同以区分。常常会有构造函数重载的情况。

函数的声明: 

 complex& operator += (const complex&); 

 complex& operator -= (const complex&); 

 complex& operator *= (const complex&);  

complex& operator /= (const complex&);

操作符重载参数complex后面有&符号,是pass by reference,是传的引用,比传value好,更快。原则是能穿引用的时候要传引用。构造函数中的double也可以传引用。函数在本体里定义:写在这里是inline函数,至于能不能实现inline看编译器。

 double real () const { return re; }  

double imag () const { return im; }

函数是取得复数的实部和虚部,并不会改变对象的数据,因此函数要加const。

数据封装:

private:(private的数据是class私有的,不能被外界直接使用) 

double re, im;

friend complex& __doapl (complex *, const complex&);

 friend complex& __doami (complex *, const complex&);

 friend complex& __doaml (complex *, const complex&);

这些函数都是友元,可以直接取class里面的数据。

};

(public和private的顺序不定。)

类定义不在本体中定义:

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 doubleimag (const complex& x)

 return x.imag ();

}

inline doublereal (const complex& x)

{

 return x.real ();

}

inline complexoperator + (const complex& x, const complex& y)

 return complex (real (x) + real (y),

 imag (x) + imag (y));

}

inline complexoperator + (const complex& x, double y)

{

return complex (real (x) + y, imag (x));

}

inline complexoperator + (double x, const complex& y)

{

return complex (x + real (y), imag (y));

}

inline complexoperator - (const complex& x, const complex& y)

{

 return complex (real (x) - real (y), imag (x) - imag (y));

}

inline complexoperator - (const complex& x, double y)

{

 return complex (real (x) - y, imag (x));

}

inline complexoperator - (double x, const complex& y)

{

 return complex (x - real (y), - imag (y));

}

inline complexoperator * (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 complexoperator * (const complex& x, double y)

 return complex (real (x) * y, imag (x) * y);

}

inline complexoperator * (double x, const complex& y)

{

 return complex (x * real (y), x * imag (y));

}

complexoperator / (const complex& x, double y)

{

 return complex (real (x) / y, imag (x) / y);

}

inline complexoperator + (const complex& x)

{

 return x;

}

inline complexoperator - (const complex& x)

 return complex (-real (x), -imag (x));

}

inline booloperator == (const complex& x, const complex& y)

{

 return real (x) == real (y) && imag (x) == imag (y);

}

inline booloperator == (const complex& x, double y)

{

 return real (x) == y && imag (x) == 0;

}

inline booloperator == (double x, const complex& y)

{

 return x == real (y) && imag (y) == 0;

}

inline booloperator != (const complex& x, const complex& y)

 return real (x) != real (y) || imag (x) != imag (y);

}

inline booloperator != (const complex& x, double y)

 return real (x) != y || imag (x) != 0;

}

inline booloperator != (double x, const complex& y)

{

 return x != real (y) || imag (y) != 0;

}

#includeinline complexpolar (double r, double t)

{

 return complex (r * cos (t), r * sin (t));

}

inline complexconj (const complex& x)

{

 return complex (real (x), -imag (x));

}

inline doublenorm (const complex& x)

{

 return real (x) * real (x) + imag (x) * imag (x);

}

结束防卫式声明#endif //__MYCOMPLEX__

3.主函数

#include

#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 << 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;

}

你可能感兴趣的:(Boolan C++(一))