栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

(面向对象程序设计c++)第十章定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算。参加运算的两个运算量可以都是类对象,也可以其中有一个是整数,顺序任意。例如: c1+c2,i+

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

(面向对象程序设计c++)第十章定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算。参加运算的两个运算量可以都是类对象,也可以其中有一个是整数,顺序任意。例如: c1+c2,i+

#include
using namespace std;
class Complex
{
public:
    Complex operator +(Complex& c2);
    //运算符的重载也可以进行函数重载
    Complex operator +(int a);
    void get_complex();
    void dispaly();
private:
    int real;
    int imag;
};
Complex Complex::operator+(Complex& c2)
{
    Complex c1;
    c1.real = real + c2.real;
    c1.imag = imag + c2.imag;
    return c1;
}
Complex Complex::operator+(int a)
{
    Complex c1;
    c1.real = real + a;
    c1.imag = imag;
    return c1;
}
void Complex::get_complex()
{
    cin >> real >> imag;
}
void Complex::dispaly()
{
    cout << "(" << real << "," << imag << "i)" << endl;
}
int main()
{
    Complex c1, c2, c3;
    int a = 0;
    c1.get_complex();
    c2.get_complex();
    c3 = c1 + c2;
    c3.dispaly();
    c2.get_complex();
    cin >> a;
    c3 = c2 + a;
    c3.dispaly();
}

2简答题)有两个矩阵a和b,均为2行3列。求两个矩阵之和。重载运算符“+”,使之能用于矩阵相加。如: c=a+b。

#include
using namespace std;
class Juzheng
{
public:

    friend Juzheng operator+(Juzheng a,Juzheng b);
    void set();
    void display();      

private:
int    arr[2][3];

};
Juzheng operator+(Juzheng a,Juzheng b)
{
    Juzheng c;
    for(int i=0;i<2;i++)
    {
        for (int j = 0; j < 3; j++)
        {
            c.arr[i][j] = a.arr[i][j] + b.arr[i][j];
        }
            
    }
    return c;
}
void Juzheng::set()
{
    for (int i = 0; i<2; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            cin >> arr[i][j];
        }
    }
}
void Juzheng::display()
{
    for (int i = 0; i<2; i++)
    {
        for (int j = 0; j <=3; j++)
        {
        cout<         }
    }

}
int main()
{
    Juzheng c,a,b;
    a.set();
    a.display();
    b.set();
    b.display();
    c = a + b;

c.display();
    return 0;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/917401.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号