软件技术基础与开发 PPT - Class03 - 作业答案
作者:互联网
复数类complex的实现——答案
- 首先是头文件ClassComplexNumber.h,其给出了复数类complex的定义。
// 复数类头文件_示例程序
#ifndef complex_H
#define complex_H
class complex
{
private:
double m_real; // 实数部分
double m_imag; // 虚数部分
public:
complex(double r=0.0,double i=0.0); //构造函数
complex(const complex & cp); // 构造函数
~complex(); // 析构函数,啥也不做
// 设置实数部分、虚数部分、整体复数的函数
void setReal(double real);
void setImag(double imag);
void setValue(double real, double imag);
// 提取实数部分、虚数部分的函数
double getReal() const;
double getImag() const;
// 判断两个复数是否相等
friend bool operator==(const complex & lhs, const complex & rhs);
// 此复数与另一个复数相加
void addAnotherComplex(const complex & cp);
//输出复数
void display();
};
#endif
- 接下来是类的ClassComplexNumber.cpp实现文件,其给出了类的成员函数实现。
#include "ClassComplexNumber.h"
#include <iostream>
using namespace std;
complex::complex(double r,double i) //构造函数
{
m_real = r;
m_imag = i;
cout << "Construct complex number with real and image part: "
<< "( " << r << ", " << i << " )" << endl;
}
complex::complex(const complex & cp) // 构造函数
{
m_real = cp.m_real;
m_imag = cp.m_imag;
cout << "Construct complex number with another complex nubmer : "
<< "( " << cp.m_real << ", " << cp.m_imag << " )" << endl;
}
complex::~complex() // 析构函数,啥也不做
{
cout << "Destructor, do nothing! " << endl;
}
// 设置实数部分、虚数部分、整体复数的函数
void complex::setReal(double real)
{
cout << "Set real part to " << real << endl;
m_real = real;
}
void complex::setImag(double imag)
{
cout << "Set imag part to " << imag << endl;
m_imag = imag;
}
void complex::setValue(double real, double imag)
{
m_real = real;
m_imag = imag;
cout << "Set complex number value to : "
<< "( " << real << ", " << imag << " )" << endl;
}
// 提取实数部分、虚数部分的函数
double complex::getReal() const
{
return m_real;
}
double complex::getImag() const
{
return m_imag;
}
// 判断两个复数是否相等
bool operator==(const complex & lhs, const complex & rhs)
{
return ((lhs.m_real == rhs.m_real) && (lhs.m_imag == rhs.m_imag));
}
// 此复数与另一个复数相加
void complex::addAnotherComplex(const complex & cp)
{
cout << "( " << m_real << ", " << m_imag << " ) + "
<< "( " << cp.getReal() << ", " << cp.getImag() << " ) ";
m_real += cp.m_real;
m_imag += cp.m_imag;
cout << "= "
<< "( " << m_real << ", " << m_imag << " )" << endl;
}
void complex::display() //输出复数
{
/*if (m_imag < 0)
{
cout << m_real << m_imag<< "i" << endl;
}
else
{
cout << m_real << "+" << m_imag << "i" << endl;
}*/
cout << "Complex number value is : "
<< "( " << m_real << ", " << m_imag << " )" << endl;
}
- 最后是主函数main.cpp,用来测试complex的定义和实现是否正确。
#include <iostream>
#include "ClassComplexNumber.h"
using namespace std;
int main() //主函数
{
complex c1(5, 4);
complex c2(c1);
c1.display();
c2.display();
c2.setReal(10);
c2.display();
c2.setImag(13);
c2.display();
c2.setValue(7, 9);
c2.display();
c1.addAnotherComplex(c2);
c1.display();
cout << c1.getReal() << endl;
cout << c1.getImag() << endl;
cout << (c1 == c2) << endl;
return 0;
}
运行结果截图如下,可见该复数类complex成功实现了复数的定义和一些简单操作
标签:const,double,complex,复数,PPT,软件技术,c2,Class03,display 来源: https://blog.csdn.net/meiguanhua/article/details/100885289