C++实验报告3.doc
文本预览下载声明
重 庆 交 通 大 学
学 生 实 验 报 告
实验课程名称 C++程序设计
开课实验室 数学实验室
学 院 理学院 年级 09级信息 专业班 2班
学 生 姓 名 周后飞 学号
开 课 时 间 2009 至 2010 学年 第 2 学期
评分细则
内容 分数 实验过程设计 (40%) 实验结果分析(30%) 实验体会(20%) 排版格式(10%) 总 成 绩 教师签名:韩逢庆
实验5 运算符重载
5.1 实验目的
1 . 进一步理解运算符重载的概念和使用方法;
2 . 掌握几种常用的运算符重载的方法;
3 . 了解转换构造函数的使用方法
5.2 实验内容
5.2.1 理解部分
阅读理解:教材8.4和8.5小节,tc\example\下的CIRCLE.PRJ、MCIRCLE.PRJ、DYNPOINT.PRJ、FIGDEMO.PRJ、LISTDEMO.PRJ这5个工程。
5.2.2 程序设计部分
1 .定义一个复数类,通过重载运算符: + , - , * , / ,= =,++,--,和。能够实现二个复数之间或者复数与实数之间的四则运算、判断二个复数是否相等、复数的自加、自减、复数的输入和输出等等,其中“++”的语义是实部和虚部都自加。编写一个完整的程序,测试各种运算符的正确性,如:(4+i)+(102-47i),3.5*(5-2i),…等等。
2 .定义一个矩阵类(4行4列)和一个向量类(4行1列),通过重载运算符: + , - , * ,和。能够实现二个矩阵的加、减、乘法,二个向量的加、减法,矩阵和向量乘法,以及各自的输入/输出。
实验结果分析
1 .定义一个复数类,通过重载运算符: + , - , * , / ,= =,++,--,和。能够实现二个复数之间或者复数与实数之间的四则运算、判断二个复数是否相等、复数的自加、自减、复数的输入和输出等等,其中“++”的语义是实部和虚部都自加。编写一个完整的程序,测试各种运算符的正确性,如:(4+i)+(102-47i),3.5*(5-2i),…等等。
(一)、程序理解
该程序考察运算符重载问题,从而实现了复数之间的各种运算!
(二)、程序设计
见代码。
实验体会
改程序让我了解熟悉了运算符重载的具体操作,最主要是理解了运算符重载的规则与方法,为以后解决实际的各类运算问题奠定了基础。
附录:(源代码)
#includeiostream.h
#includemath.h
class complex//定义一个复数类,通过重载运算符: + , - , * , / ,= =,++,--
{
private:
double real;
double image;
public:
complex(void):real(0),image(0)
{}
complex(double rea):real(rea),image(0)
{}
complex(double rea,double ima):real(rea),image(ima)
{}
~complex()
{}
complex operator+(const complex x)const;
complex operator-(const complex x)const;
complex operator*(const complex x)const;
complex operator/(const complex x)const;
int operator==(const complex x);
complex operator++(void);
complex operator--(void);
void print(void)const;
};
inline complex complex::operator+(const complex x)const
{
return complex(real+x.real,image+x.image);
}
inline complex complex::operator-(const complex x)const
{
return complex(real-x.real,image-x.image);
}
inline complex complex::operator*(const complex x)const
{
r
显示全部