#include "pch.h"
#include <iostream>
#include<cmath>
using namespace std; class Complex
{
public:
Complex(double aa = 0.0, double bb = 0.0)//构造函数赋予初始值
{
a = aa; b = bb;
}
Complex(Complex &p);//赋值构造函数
void add(Complex x);//复数相加
void show();//复数输出
double mod();//复数取模
private:
float a;
float b;
}; Complex::Complex(Complex &p) {
a = p.a;
b = p.b;
} void Complex::add(Complex x)
{
a += x.a;
b += x.b;
} void Complex::show()
{
cout << a << "+" << b << "i" << endl;
} double Complex::mod()
{
return sqrt(a*a + b * b);
} int main()
{
Complex c1(,);
cout << "c1:";
c1.show();
Complex c2(4.5);
Complex c3(c1);
c1.add(c2);
cout << endl << "c2:";
c2.show();
cout <<endl<< "c1+c2:";
c1.show();
cout << endl<< "mod(c3):";
cout << c3.mod() << endl;
return ;
}
运行结果展示:
总结体会:
通过编写这样一个简单的类,对于构造函数这一块理解更加深刻了。同时,对于OO的理解应该也有提升吧。
实验二评论地址:
1:https://www.cnblogs.com/zxz2425405395/p/10558646.html
本周事情太多,只能先评论一篇,见谅。