我是靠谱客的博主 体贴大碗,这篇文章主要介绍复数类Complex,现在分享给大家,希望可以做个参考。

本题自定义一个复数类,类名Complex,包含实部real和虚部imag,定义如下。
本题3个空,请填写完整。

#include<iostream>
using namespace std;

class Complex
{
public:
	Complex() { Set(0, 0); }                    //无参构造函数声明及定义
	Complex(double r, double i);                   //有参构造函数声明
	void Set(double r, double i);
	void Output();
	double GetReal() { return real; }
	double GetImag() { return imag; }
private:
	double real, imag;
};


Complex::Complex(double r, double i)
{
	Set(r, i);
}

void Complex::Set(double r, double i)
{
    real = r;
    imag = i;
}

void Complex::Output()
{
    cout << real << "+" << imag << "i" << endl;
}

int main()
{
    double m, n;
    cin >> m >> n;
    Complex p1;
    p1.Output();
    p1.Set(m, n);
    p1.Output();

    Complex p2(1, 2);
    cout << p2.GetReal() << "+" << p2.GetImag() << "i" << endl;
    return 0;
}

输入样例:

3 5

输出样例:

0+0i
3+5i
1+2i

最后

以上就是体贴大碗最近收集整理的关于复数类Complex的全部内容,更多相关复数类Complex内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(167)

评论列表共有 0 条评论

立即
投稿
返回
顶部