多态性--运算符重载--实现复数类的加减运算
输入样例: 1 2 3 4
输出样例: (4,6)
(-2,-2)
#include<iostream> using namespace std; class complex{ private: double real; double imag; public: void display() { cout<<"("<<real<<","<<imag<<")"<<endl; } complex (double r=0.0,double i=0.0) { real=r; imag=i; } complex operator +(complex &c2); complex operator -(complex &c2); }; complex complex::operator +(complex &c2) { return complex(real+c2.real,imag+c2.imag); } complex complex::operator -(complex &c2) { return complex(real-c2.real,imag-c2.imag); } int main() { int x,y,m,n; cin>>x>>y>>m>>n; complex c1(x,y); complex c2(m,n); complex c3; complex c4; c3=c1+c2; c4=c1-c2; cout<<"c3="; c3.display(); cout<<"c4="; c4.display(); }
标签:cout,complex,2023,c3,c2,c1,c4 From: https://www.cnblogs.com/wangxinyuan1108/p/17378506.html