class VirtualNumber{
private:
int real_number;
int vitual_number;
public:
VirtualNumber(int a,int b);
void show();
VirtualNumberoperator+(VirtualNumber a);
VirtualNumberoperator-(VirtualNumber a);
staticVirtualNumber mul(VirtualNumber a,VirtualNumber b);
VirtualNumberoperator+(int a);
VirtualNumberoperator-(int a);
int getRealNumber();
int getVitualNumber();
};
intVirtualNumber::getRealNumber(){
returnthis->real_number;
}
intVirtualNumber::getVitualNumber(){
returnthis->vitual_number;
}
VirtualNumber::VirtualNumber(int a,int b){
this -> real_number = a;
this -> vitual_number = b;
}
voidVirtualNumber::show(){
cout << "{" <<this->real_number << "+" << this->vitual_number << "i" << "}";
}
VirtualNumberVirtualNumber::operator+(VirtualNumber a){
int vitual = this->vitual_number + agetVitualNumber();
int real = this->real_number + agetRealNumber();
VirtualNumber result = VirtualNumber(vitual,real);
return result;
}
VirtualNumberVirtualNumber::operator-(VirtualNumber a){
int vitual = this->vitual_number - agetVitualNumber();
int real = this->real_number - agetRealNumber();
VirtualNumber result = VirtualNumber(vitual,real);
return result;
}
VirtualNumberVirtualNumber::operator+(int a){
int vitual = this->vitual_number;
int real = this->real_number + a;
VirtualNumber result = VirtualNumber(vitual,real);
return result;
}
VirtualNumberVirtualNumber::operator-(int a){
int vitual = this->vitual_number;
int real = this->real_number - a;
VirtualNumber result = VirtualNumber(vitual,real);
return result;
}
VirtualNumberVirtualNumber::mul(VirtualNumber a, VirtualNumber b){
int i = 0,j = 0,k = 0,l = 0;
i = agetRealNumber()bgetVitualNumber();
j = agetVitualNumber()bgetRealNumber();
k = agetRealNumber()bgetRealNumber();
l = agetVitualNumber()bgetVitualNumber()-1;
VirtualNumber result = VirtualNumber(k+l,i+j);
return result;
}
int main(){
VirtualNumber a = VirtualNumber(3, 2);
VirtualNumber b = VirtualNumber(2 ,5);
ashow();
cout << "";//根据需要改+- 除法没做实在是忘记了
bshow();
cout << "=";
(VirtualNumber::mul(a, b))show();
//乘法没用重载,这里可以写(a+b)show(); 或者 (a-b)show()
return0;
}
//试试看
以上就是关于定义一个复数类,通过重载运算符: +,-,*,/,直接实现二个复数之间的乘除运算。全部的内容,包括:定义一个复数类,通过重载运算符: +,-,*,/,直接实现二个复数之间的乘除运算。、、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)