18_1.cpp
资源名称:c.rar [点击查看]
上传用户:puke2000
上传日期:2022-07-25
资源大小:912k
文件大小:1k
源码类别:

C#编程

开发平台:

Visual C++

  1. #include <iostream.h>
  2. class Complex{
  3. public:
  4.   Complex(double r=0, double v=0):real(r),virt(v){}
  5.   friend Complex operator+(Complex a, Complex b);
  6. friend ostream& operator<<(ostream& out, Complex& a);
  7. private:
  8.   double real;
  9.   double virt;
  10. };
  11. ostream& operator<<(ostream& out, Complex& a){ return out <<a.real <<"+" <<a.virt <<"in"; }
  12. Complex operator+(Complex a, Complex b)
  13. {
  14.   return Complex(a.real+b.real, a.virt+b.virt);
  15. }
  16. void main()
  17. {
  18.   Complex a(2,5), b(7,8), c(0,0);
  19.   c = a + b;
  20.   cout <<c;
  21.   c = 4.1 + a;
  22.   cout <<c;
  23.   c = b + 5.6;
  24.   cout <<c;
  25. }
  26.