用C++语言设计一个虚数类,要求虚数类中重载运算符加减,主函数定义类对象并调用运算重载符。 10
2个回答
展开全部
代码如下:
/*
1.复数(实部运算 +虚部运算) 1+2i 1-3i 加:2-i 减 0-5i
1 重载实现复数一个输入和输出
普通写法实现输入
调用函数的形式实现输出
2. 类重载实现复数的加法和减法
加法:类重载
减法:友元重载
*/
#include<iostream>
using namespace std;
class A
{
private:
int x;
int y;
public:
A(int x=0, int y=0) :x(x), y(y){}
void Output(ostream &out)
{
out << "x+yi=" << x << "+" << y <<"i"<< endl;
}
friend ostream& operator<<(ostream &out, A &a);
//重载的声明方式 函数返回值 operator 运算符(参数表)
A operator+(A& a);//操作数等于参数-1
friend A operator-(A &a, A &b);//友元,操作数等于形参数
};
ostream& operator<<(ostream &out, A &a)
{
a.Output(out);
return out;
}
//C++学习
A A::operator+(A &a)
{
//打包你要实现的操作
A b;
b.x = x + a.x;
b.y = y + a.y;
return b;
}
//群
A operator-(A &a, A &b)
{
A ab;
ab.x = a.x - b.x;
ab.y = a.y - b.y;
return ab;
}
int main()
{
// 491994603
cout << "输入两个复数,实现复数的加减" << endl;
int x, y,xx,yy;
cout << "输入第一个复数";
cin >> x >> y;
cout << "输入第二个复数";
cin >> xx >> yy;
A X1(x, y);
A X2(xx,yy);
cout << "复数的加法" << endl;
A X3 = X1 + X2;
cout << X3 << endl;
cout << "复数的减法" << endl;
A X4 = X1 - X2;
cout << X4 << endl;
return 0;
}
2017-09-12
展开全部
#includeusingnamespacestd;constintR=3,C=4;classMatrix{doublea[R][C];public:Matrixoperator+(Matrix&d){Matrixtemp;for(inti=0;i>a[i][j];}}voiddisplay(){for(inti=0;i<R;i++){for(intj=0;j<C;j++)cout<<a[i][j]<<"";cout<<endl;}}};intmain(){Matrixm1,m2,m3;m1.Input();m2.Input();m3=m1+m2;cout<<"Matrixm1:\n";m1.display();cout<<"\nMatrixm2:\n";m2.display();cout<<"\nMatrixm3:\n";m3.display();return0;}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询