|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 红莲教主 于 2016-5-12 09:57 编辑
- #include <iostream>
- //using namespace std ;
- using std::cout;
- using std::endl;
- using std::ostream;
- class Complex
- {
- public:
- Complex(int a , int b);
- ~Complex();
- Complex operator+(Complex com);
- private:
- int a ;
- int b ;
- friend ostream& operator<<(ostream& out ,Complex com );
- friend Complex operator-(Complex c1 ,Complex c2);
- };
- ostream& operator<<(ostream& out ,Complex com );
- Complex operator-(Complex c1 ,Complex c2);
- // 构造函数
- Complex::Complex(int a , int b)
- {
- this->a = a ;
- this->b = b ;
- }
- //析构函数
- Complex::~Complex()
- {
- }
- //重载加号运算符
- Complex Complex::operator+(Complex com)
- {
- return Complex(a+com.a , b+com.b);
- }
- //重载减号预算福
- Complex operator-(Complex c1 ,Complex c2)
- {
- return Complex(c1.a-c2.a , c1.b-c2.b);
- }
- //重载<<运算符
- ostream& operator<<(ostream& out ,Complex com )
- {
- cout<<"("<<com.a<<","<<com.b<<")"<<endl;
- return out ;
- }
- int main()
- {
- Complex A(100,200);
- Complex B(500,600);
- cout<<(A+B);
- Complex* C = new Complex(800,600);
- Complex* D = new Complex(400,200);
- cout<<(*C-*D);
- delete C ;
- delete D ;
- return 0;
- }
复制代码 |
-
|