1、 实验五 运算符重载的应用 班级:B135A2学号: 201322688 姓名: 杨弘 成绩: 一. 实验目的 1、理解运算符重载的作用; 2、掌握运算符重载的两种方法; 3、掌握单目、双目运算符的重载; 二. 使用的设备和仪器 计算机+Windows XP +Visual C++6.0 三. 实验内容及要求 1、定义一个复数类Complex,重载运算符“-”,使之能用于复数与实数的减法运算。参加运算的两个操作数一个是类的对象,一个是实数,顺序任意。例如:i-c,c-i均合法(其中,c为复数类的对象,i为实数)。 减法规则:复数实部与实数相减,复数虚部不
2、变。 2、定义点类Point,重载运算符“+”、“-”、“==”、“!=”、“++”、“--”、“>>”、“<<”,实现两个点的相加、相减、相等、不等、自增、自减、输入和输出运算。 3、定义一个矩阵类Matrix,均为M行N列,通过重载运算符“+”、“-”,“<<”,“>>”,“++”,“--”,“==”,“!=”来实现矩阵的相加、相减、输出、输入、自增、自减以及相等、不等的判断。 4、定义时间类Time,时间的表示采用24小时制。重载运算符“<<”和“>>”实现时间的输出和输入;重载运算符“+”和“-”实现时间推后和提前若干分钟;重载运算符“++”和“--”实现当前时间推后和提前1小时
3、重载“>”、“<”、“==”来判断两个时间之间大于、小于、等于以及不等于的关系。 注意:输入时需对数据合法性进行判断。 5、编写一个程序,处理一个复数与一个double型数相加的运算,结果存放在一个double型的变量d1中,输出d1的值,再以复数形式输出此值。定义复数(Complex)类,在成员函数中包含重载类型转换运算符: Operator double(){ return real;} 6、定义一个教师类和一个学生类,二者有一部分数据成员是相同的,例如num,name,sex。请编程,将一个学生类对象转换为教师类,只将以上3个相同的数据成员移植过去。可以设想为:一位学生大学毕业
4、了,留校担任教师,他原有的部分数据对现在的教师身份来说仍然是有用的,应当保留并成为其教师的数据的一部分。
四. 实验步骤
1.程序代码:/*1、定义一个复数类Complex,重载运算符"-",使之能用于复数与实数的减法运算。参加运算的两个操作数一个是类的对象,一个是实数,顺序任意。
例如:i-c,c-i均合法(其中,c为复数类的对象,i为实数)。
减法规则:复数实部与实数相减,复数虚部不变。*/
#include
5、cout; using std::endl; //using namespace std; class Complex //定义复数类 { private: double real,imag; public: Complex() { real=0; imag=0; } Complex(double a,double b) { real=a; imag=b; } void Display(); void Input(); friend Complex operat
6、or-(Complex &a,double b); friend Complex operator-(double b,Complex &a); }; Complex operator-(Complex &a,double b) //定义“-”重载函数 { return Complex(a.real-=b,a.imag); } Complex operator-(double b,Complex &a) { return Complex(a.real-=b,a.imag); } void Complex::Input() { cout<<"******
7、输入**********"< 8、实数:";
cin>>m;
c=c-m;
c.Display();
return 0;
}
运行结果:
2.程序代码:
/*2、定义点类Point,重载运算符"+"、"-"、"=="、"!="、"++"、"--"、">>"、"<<",
实现两个点的相加、相减、相等、不等、自增、自减、输入和输出运算。*/
//#include 9、g
// std::cout;using std::endl;对重载流插入、流提取运算符错误
//using std::cout;
//using std::endl;
//using namespace std;
class Point //定义point类
{
private:
double x;
double y;
static int count;
public:
Point()
{
x=0;y=0;
}
void Input();
void D 10、isplay();
Point operator+(Point &b);
Point operator-(Point &b);
friend int operator==(Point &a,Point &b);
friend int operator!=(Point &a,Point &b);
friend Point operator++(Point &a);
friend Point operator++(Point &a,int);
friend Point operator--(Point &a);
friend Point opera 11、tor--(Point &a,int);
friend istream & operator>>(istream & input,Point & a);
friend ostream & operator<<(ostream & output,Point & a);
};
int Point::count=0; //注意赋值时没有static," int static Point::count=0; " wrong!!
void Point::Input()
{
cout<<"******输入******"< 12、out<<"请输入点坐标(x,y)";
cin>>x>>y;
count++;
}
void Point::Display()
{
cout<<"******显示******"< 13、int &b) //重载“-”为成员函数
{
Point p;
p.x=x-b.x;
p.y=y-b.y;
return p;
}
int operator==(Point &a,Point &b) //重载“==”为友员函数
{
if(a.x!=b.x)
return 0;
else if(a.y!=b.y)
return 0;
else
return 1;
}
int operator!=(Point &a,Point &b) //重载“!=”为友员函数
{
if(a.x==b.x& 14、a.y==b.y)
return 0;
else return 1;
}
Point operator++(Point &a) //重载“++”为友员函数
{
Point p;
p.x=++a.x;
p.y=++a.y;
return p;
}
Point operator++(Point &a,int) //重载“++”为友员函数
{
Point c;
c.x=a.x;
c.y=a.y;
a.x++;
a.y++;
return c;
}
Point operator--(P 15、oint &a) //重载“--”为友员函数
{
Point p;
p.x=-a.x;
p.y=--a.y;
return p;
}
Point operator--(Point &a,int) //重载“--”为友员函数
{
Point c;
c.x=a.x;
c.y=a.y;
a.x--;
a.y--;
return c;
}
istream & operator>>(istream & input,Point & a) //重载流插入运算符
{
input>>a 16、x>>a.y;
return input;
}
ostream & operator<<(ostream & output,Point & a) //重载流插入运算符
{
output<<"("< 17、
cout<<"Overload'+'----a=a+b:"<>'输入输出:"< 18、 and '!='"< 19、N=3;
class Matrix
{
private:
int X[M][N];
public:
friend Matrix operator+(Matrix a[][N],Matrix b[][N]);
friend Matrix operator-(Matrix &a,Matrix &b);
friend istream & operator>>(istream & input,Matrix &c);
friend ostream & operator<<(istream & output,Matrix &c);
friend Matrix operato 20、r++(Matrix &c);
friend Matrix operator--(Matrix &c);
friend int operator==(Matrix &a,Matrix &b);
friend int operator!=(Matrix &a,Matrix &b);
};
Matrix operator+(Matrix a[][N],Matrix b[][N])
{
int i,j;
Matrix c[M][N];
for(i=0;i 21、i][j]+b[i][j];
}
return c[M][N];
}
Matrix operator-(Matrix *a,Matrix *b)
{
int i,j;
Matrix c[M][N];
for(i=0;i






