实验五 运算符重载的应用
班级: 学号: 序号 姓名: 成绩:
一. 实验目的
1.理解运算符重载的作用;
2.掌握运算符重载的两种方法;
3.掌握单目、双目运算符的重载;
二. 使用的设备和仪器
计算机+Windows XP +Visual C++6.0
三. 实验内容及要求
1.定义点类Point,重载运算符“+”、“-”、“==”、“!=”、“++”、“--”、“>>”、“<<”,实现两个点的相加、相减、相等、不等、自增、自减、输入和输出运算。
2.定义一个矩阵类Matrix,均为M行N列,通过重载运算符“+”、“-”,“<<”,“>>”,“++”,“--”,“==”,“!=”来实现矩阵的相加、相减、输出、输入、自增、自减以及相等、不等的判断。
3.定义时间类Time,时间的表示采用24小时制。重载运算符“<<”和“>>”实现时间的输出和输入;重载运算符“+”和“-”实现时间推后和提前若干分钟;重载运算符“++”和“--”实现当前时间推后和提前1小时;重载“>”、“<”、“==”来判断两个时间之间大于、小于、等于以及不等于的关系。
注意:输入时需对数据合法性进行判断。
扩展练习:
4.定义一个复数类Complex,重载运算符“-”,使之能用于复数与实数的减法运算。参加
运算的两个操作数一个是类的对象,一个是实数,顺序任意。例如:i-c,c-i均合法(其中,c为复数类的对象,i为实数)。
减法规则:复数实部与实数相减,复数虚部不变。
5.编写一个程序,处理一个复数与一个double型数相加的运算,结果存放在一个double型的变量d1中,输出d1的值,再以复数形式输出此值。定义复数(Complex)类,在成员函数中包含重载类型转换运算符:
Operator double(){ return real;}
6.定义一个教师类和一个学生类,二者有一部分数据成员是相同的,例如num,name,sex。请编程,将一个学生类对象转换为教师类,只将以上3个相同的数据成员移植过去。可以设想为:一位学生大学毕业了,留校担任教师,他原有的部分数据对现在的教师身份来说仍然是有用的,应当保留并成为其教师的数据的一部分。
四. 实验步骤
编写程序+编译+连接+运行+分析结果
1#include <iostream>
using namespace std;
class Point
{
private:
double hig; //k空间坐标
double lon;
double wid;
public:
Point()
{
lon=0;
wid=0;
hig=0;
}
Point(double l,double w,double h)
{
lon=l;
wid=w;
hig=h;
}
void Input();
void Show();
Point operator++(int);//重载
Point operator++();
Point operator--(int);
Point operator--();
Point operator+(Point &a);
Point operator-(Point &a);
friend bool operator==(Point &a,Point &b);
friend bool operator!=(Point &a,Point&b);
friend ostream& operator<<(ostream&,Point&);
friend istream& operator>>(istream&,Point&);
};
void Point::Input()
{
cout<<"输入点的坐标:"<<endl;
cout<<"输入x坐标:";
cin>>lon;
cout<<"输入y坐标:";
cin>>wid;
cout<<"输入z坐标:";
cin>>hig;
}
void Point::Show()
{
cout<<"点坐标为:"<<"("<<lon<<","<<wid<<","<<hig<<")"<<endl;
}
Point Point::operator++(int)
{
return Point(lon++,wid++,hig++);
}
Point Point::operator++()
{
return Point(++lon,++wid,++hig);
}
Point Point::operator--(int)
{
return Point(lon--,wid--,hig--);
}
Point Point::operator--()
{
return Point(--lon,--wid,--hig);
}
Point Point::operator+(Point &a)
{
return Point(lon+a.lon,wid+a.wid,hig+a.hig);
}
Point Point::operator-(Point&a)
{
return Point(lon-a.lon,wid-a.wid,hig-a.hig);
}
bool operator==(Point&a,Point&b)
{
if((b.lon==a.lon)&&(b.wid==a.wid)&&(b.hig==a.hig))
return true;
else return false;
}
bool operator!=(Point&a,Point&b)
{
if(!((b.lon==a.lon)&&(b.wid==a.wid)&&(b.hig==a.hig)))
return true;
else return false;
}
ostream& operator<<(ostream& output,Point&a)
{
output<<"("<<a.lon<<","<<a.wid<<","<<a.hig<<")"<<endl;
return output;
}
istream& operator>>(istream& input,Point &a)
{
cout<<"输入点的x,y,z坐标:";
input>>a.lon>>a.wid>>a.hig;
return input;
}
int main()
{
Point a,b(5,3,7),c,d,e;
a.Input();
a.Show();
b.Show();
c=a+b;
d=a-b;
cout<<c<<endl;
cout<<d<<endl;
cin>>e;
cout<<e<<endl;
if(a==e) cout<<"LOVE"<<endl;
if(a!=e) cout<<"HATE"<<endl;
b++;
cout<<b<<endl;
b--;
cout<<b<<endl;
return 0;
}
2.#include<iostream>
#include<iomanip>
using std::cin;
using std::cout;
using std::endl;
using std::istream;
using std::ostream;
using std::setw;
const int M=2;
const int N=2;
class Matrix
{
private:
double mat[M][N]; //M行N列矩阵
public:
Matrix() //gou构造函数
{
for(int i=0;i<M;i++)
{
for(int j=0;j<N;j++)
{
mat[i][j]=0;
}
cout<<endl;
}
}
void Input(); //输入函数
void Show(); //s输出函数
Matrix operator+(Matrix&); //ch重载
Matrix operator-(Matrix&);
Matrix operator++(int);
Matrix operator++();
Matrix operator--(int);
Matrix operator--();
friend bool operator==(Matrix &,Matrix&);
friend ostream& operator<<(ostream&,Matrix&);
friend istream& operator>>(istream&,Matrix&);
};
void Matrix::Input() //输入函数
{
int i,j;
cout<<"请输入"<<M<<"行"<<N<<"列数据"<<endl;
for(i=0;i<M;i++)
for(j=0;j<N;j++)
cin>>mat[i][j];
}
void Matrix::Show() //输出函数
{
int i,j;
for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
{
cout<<setw(5)<<mat[i][j];
}
cout<<endl;
}
cout<<endl;
}
Matrix Matrix::operator+(Matrix&b) //加号重载
{
int i,j;
Matrix c;
for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
{
c.mat[i][j]=mat[i][j]+b.mat[i][j];
}
}
return c;
}
Matrix Matrix::operator-(Matrix&b) //减号重载
{
int i,j;
Matrix c;
for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
{
c.mat[i][j]=mat[i][j]-b.mat[i][j];
}
}
return c;
}
Matrix Matrix::operator++(int) //h后置自加重载
{
int i,j;
Matrix temp(*this);
for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
{
mat[i][j]++;
}
}
return temp;
}
Matrix Matrix::operator++()
{
for(int i=0;i<M;i++)
{
for(int j=0;j<N;j++)
{
++mat[i][j];
}
}
return *this;
}
Matrix Matrix::operator--(int) //zijian
{
Matrix temp(*this);
int i,j;
for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
{
mat[i][j]--;
}
}
return temp;
}
Matrix Matrix::operator--()
{
int i,j;
for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
{
--mat[i][j];
}
}
return *this;
}
bool operator==(Matrix &a,Matrix&b) //判断相等
{
int i,j,m;
for(i=0;i<M;i++)
{
for(j=0;j<N;j--)
{
if(a.mat[i][j]==b.mat[i][j]) m++;
else break;
}
}
if(m==M*N) return true;
else return false;
}
ostream& operator <<(ostream &output,Matrix &a) //输出流重载
{
int i,j;
for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
{
output<<a.mat[i][j]<<" ";
}
output<<endl;
}
output<<endl;
return output;
}
istream& operator>>(istream&input,Matrix&a) //输入流重载
{
int i,j;
for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
{
input>>a.mat[i][j];
}
}
return input;
}
int main()
{
Matrix a,b,c;
cout<<a;
cin>>a;
cout<<a;
b=a++;
cout<<b;
c=++a;
cout<<c;
return 0;
}
3#include<iostream>
using std::cin;
using std::cout;
using std::endl;
using std::ostream;
using std::istream;
class Time
{
private:
int hour;
int min;
int sec;
public:
Time(int h=0,int m=0,int s=0):hour(h),min(m),sec(s){}
friend ostream& operator<<(ostream&,Time&);
friend istream& operator>>(istream&,Time&);
friend bool operator==(Time&,Time&);
friend bool operator>(Time&,Time&);
friend bool operator<(Time&,Time&);
Time operator++(int);
Time operator++();
Time operator--(int);
Time operator--();
Time operator+(int);
Time operator-(int);
};
Time Time::operator++(int) //后置自加
{
return Time(hour,min,sec);
hour=((hour++)%24);
}
Time Time::operator++() //前置自加
{
hour=((++hour)%24);
return Time(hour,min,sec);
}
Time Time::operator--(int) //后置自减
{
return Time(hour,min,sec);
if(hour==0)hour=23;
else hour=(hour--);
}
Time Time::operator--() //前置自减
{
if(hour==0)hour=23;
else --hour;
return Time(hour,min,sec);
}
Time Time::operator+(int a) //ji加号重载
{
hour=hour+((min+a)/60);
min=((min+a)%60);
return Time(hour,min,sec);
}
Time Time::operator-(int a) //jianhao减号重载
{
if(min>a)
{
min=min-a;
}
else
{
hour=hour-(a-min)/60;
min=60-(a-min)%60;
}
return Time(hour,min,sec);
}
ostream& operator<<(ostream&output,Time&a) //流插入 重载
{
output<<a.hour<<":"<<a.min<<":"<<a.sec<<endl;
}
istream& operator>>(istream&input,Time&a) //流提取重载
{
input>>a.hour>>a.min>>a.sec;
while(a.hour<0||a.hour>23||a.min<0||a.min>59||a.sec<0|a.sec>59)
{
cout<<"请重新输入:";
input>>a.hour>>a.min>>a.sec;
}
return input;
}
bool operator==(Time&a,Time&b) //判断相等重载
{
if(a.hour==b.hour&&a.min==b.min&&a.sec==b.sec)
return true;
else
return false;
}
bool operator>(Time&a,Time&b) //大于号重载
{
if(a.hour>b.hour||(a.hour==b.hour&&a.min>b.min)||(a.hour==b.hour&&a.min==b.min&&a.sec>b.sec))
return true;
else return false;
}
bool operator<(Time&a,Time&b) //小于号重载
{
if(a.hour<b.hour||(a.hour==b.hour&&a.min<b.min)||(a.hour==b.hour&&a.min==b.min&&a.sec<b.sec))
return true;
else return false;
}
int main()
{
Time a,b(4,45,30),c;
cout<<"请输入时间:"<<endl;
cin>>a;
c=a++;
cout<<a;
c=++a;
cout<<a;
c=b--;
cout<<c;
c=--b;
cout<<c;
a=a-7;
cout<<a;
b=b-50;
cout<<b;
if(a==b)
cout<<"LOVE"<<endl;
else
cout<<"HATE"<<endl;
if(a<b)
cout<<"LOVE"<<endl;
else
cout<<"HATE"<<endl;
if(a>b)
cout<<"LOVE"<<endl;
else
cout<<"HATE"<<endl;
return 0;
}
4.#include<iostream>
using namespace std;
class Complex
{
private:
double real;
double imag;
public:
Complex(double re=0,double im=0):real(re),imag(im){} //构造函数
friend ostream& operator<<(ostream&,Complex&);
friend istream& operator>>(istream&,Complex&);
friend Complex operator-(double ,Complex&);
Complex operator-(double );
Complex operator-(Complex&);
void Show();
};
void Complex::Show() //输出函数
{
if(imag>0)
cout<<"("<<real<<"+"<<imag<<"i"<<")"<<endl;
else
cout<<"("<<real<<imag<<"i)"<<endl;
}
ostream & operator <<(ostream & output,Complex&a) //流插入重载
{
output<<"("<<a.real<<"+"<<a.imag<<"i)"<<endl;
return output;
}
istream& operator>>(istream&input,Complex&a) //流提取重载
{
input>>a.real>>a.imag;
return input;
}
Complex Complex::operator-(double a) //重载为复数减实数格式
{
return Complex(real-a,imag);
}
Complex Complex::operator-(Complex&a) //重载为复数减复数格式
{
return Complex(real-a.real,imag-a.imag);
}
Complex operator-(double a,Complex&b) //重载为实数减复数格式
{
return Complex(a-b.real,-b.imag);
}
int main()
{
Complex a(4,6),b(2,5),c;
cout<<"请输入c:";
cin>>c;
cout<<a;
cout<<b;
c=a-b;
cout<<c;
c=5-a;
cout<<c;
c=a-2;
cout<<c<<endl;
return 0;
}
5.#include<iostream>
#include<string>
using namespace std;
class Complex
{
private:
double real; //实部
double imag; //虚部
public:
Complex(double re=0,double im=0):real(re),imag(im){
} //构造函数
void Input(); //输入函数
void Show(); //输出函数
operator double() //
{
return real;
}
friend Complex operator+(Complex &,double); //重载+
friend ostream& operator<<(ostream&out,Complex&);//流插入
friend istream& operator>>(istream&in,Complex&) ;//流提取重载
};
void Complex::Input()
{
cin>>real;
cin>>imag;
}
void Complex::Show()
{
cout<<"("<<real<<","<<imag<<"i)"<<endl;
}
Complex operator+(Complex &c,double a)
{
return Complex(c.real+a,c.imag);
}
ostream& operator<<(ostream&out,Complex&a)
{
out<<"("<<a.real<<"+"<<a.imag<<"i)"<<endl;
return out;
}
istream& operator>>(istream&in,Complex&a) //流提取重载
{
in>>a.real>>a.imag;
return in;
}
int main()
{
Complex c(5,6),b;
double a;
b=c+5.0;
cout<<b;
a=b;
cout<<a<<endl;
return 0;
}
6.#include<iostream>
#include<string>
using namespace std;
class Student
{
protected:
string name; //姓名
string num; //编号
char sec; //x性别
double score; //学生特有属性
public:
Student(string nam,string nu,char s,double sco):name(nam),num(nu),sec(s),score(sco){}
Student(string nam,string nu,char s):name(nam),num(nu),sec(s){} //构造函数重载
void display()
{
cout<<"name:"<<name<<endl;
cout<<"num:"<<num<<endl;
cout<<"sec:"<<sec<<endl;
cout<<"score:"<<score<<endl;
}
};
class Teacher:public Student //公有继承
{
protected:
double wage; //教师特有属性
public:
Teacher(string nam,string nu,char s,double wa):Student(nam,nu,s),wage(wa){
}
void Show()
{
cout<<"name:"<<name<<endl;
cout<<"num:"<<num<<endl;
cout<<"sec:"<<sec<<endl;
cout<<"wage:"<<wage<<endl;
}
};
int main()
{
Teacher t("lili","1001",'w',4000);
t.Show();
return 0;
}
五. 实验总结
1.编写的程序源代码
2.实验中遇到的问题及解决方法
3.收获
1.对运算符的重载掌握不够牢固,以至于编写代码还需要一边参考书,一边写。如:Point operator++(int);后置自增忘记加int。
friend ostream& operator<<(ostream&,Point&);
friend istream& operator>>(istream&,Point&);
提取云算符关键字istream,插入运算符关键字ostream;
2.对数组处理需要进行封装成类;对++,--重载时 必须重载两次,包括前置后置;
3.对转换构造函数不太理解,以至于在写程序中没想到而用了派生,例如lass Student
{
protected:
string name; //姓名
string num; //编号
char sec; //x性别
double score; //学生特有属性
public:
Student(string nam,string nu,char s,double sco):name(nam),num(nu),sec(s),score(sco){}
Student(string nam,string nu,char s):name(nam),num(nu),sec(s){} //构造函数重载
v
};
class Teacher:public Student //公有继承
{
protected:
double wage; //教师特有属性
public:
Teacher(string nam,string nu,char s,double wa):Student(nam,nu,s),wage(wa){
}
....
};