实验五 运算符重载 完成

更新时间:2023-12-20 08:55:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

实验五 运算符重载的应用

班级:B135A2学号: 201322688 姓名: 杨弘 成绩:

一. 实验目的

1、理解运算符重载的作用; 2、掌握运算符重载的两种方法; 3、掌握单目、双目运算符的重载;

二. 使用的设备和仪器

计算机+Windows XP +Visual C++6.0

三. 实验内容及要求

1、定义一个复数类Complex,重载运算符“-”,使之能用于复数与实数的减法运算。参加运算的两个操作数一个是类的对象,一个是实数,顺序任意。例如:i-c,c-i均合法(其中,c为复数类的对象,i为实数)。

减法规则:复数实部与实数相减,复数虚部不变。 2、定义点类Point,重载运算符“+”、“-”、“==”、“!=”、“++”、“--”、“>>”、“<<”,实现两个点的相加、相减、相等、不等、自增、自减、输入和输出运算。

3、定义一个矩阵类Matrix,均为M行N列,通过重载运算符“+”、“-”,“<<”,“>>”,“++”,“--”,“==”,“!=”来实现矩阵的相加、相减、输出、输入、自增、自减以及相等、不等的判断。

4、定义时间类Time,时间的表示采用24小时制。重载运算符“<<”和“>>”实现时间的输出和输入;重载运算符“+”和“-”实现时间推后和提前若干分钟;重载运算符“++”和“--”实现当前时间推后和提前1小时;重载“>”、“<”、“==”来判断两个时间之间大于、小于、等于以及不等于的关系。

注意:输入时需对数据合法性进行判断。 5、编写一个程序,处理一个复数与一个double型数相加的运算,结果存放在一个double型的变量d1中,输出d1的值,再以复数形式输出此值。定义复数(Complex)类,在成员函数中包含重载类型转换运算符:

Operator double(){ return real;}

6、定义一个教师类和一个学生类,二者有一部分数据成员是相同的,例如num,name,sex。请编程,将一个学生类对象转换为教师类,只将以上3个相同的数据成员移植过去。可以设想为:一位学生大学毕业了,留校担任教师,他原有的部分数据对现在的教师身份来说仍然是有用的,应当保留并成为其教师的数据的一部分。

四. 实验步骤

1.程序代码:/*1、定义一个复数类Complex,重载运算符\,使之能用于复数与实数的减

法运算。参加运算的两个操作数一个是类的对象,一个是实数,顺序任意。 例如:i-c,c-i均合法(其中,c为复数类的对象,i为实数)。 减法规则:复数实部与实数相减,复数虚部不变。*/

#include //#include using std::cin; using std::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 operator-(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<<\输入**********\ cout<<\请输入复数的虚部和实部:\ cin>>real>>imag; }

void Complex::Display() {

cout<<\显示**********\ cout<

int main() //主函数 {

Complex c(1,1),c1; double m; c.Display();

cout<<\请输入需要与复数相减的实数:\ cin>>m; c=c-m; c.Display(); return 0; }

运行结果:

2.程序代码:

/*2、定义点类Point,重载运算符\+\、\-\、\==\、\!=\、\、\、\、\, 实现两个点的相加、相减、相等、不等、自增、自减、输入和输出运算。*/ //#include #include

//using std::cin; //用#include加上Using std::cin;using // 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 Display();

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 operator--(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; \void Point::Input() {

cout<<\输入******\ cout<<\请输入点坐标(x,y)\ cin>>x>>y; count++; }

void Point::Display() {

cout<<\显示******\

cout<<\第\个坐标为:\}

Point Point::operator+(Point &b) //重载“+”为成员函数 {

Point p; p.x=x+b.x; p.y=y+b.y; return p; }

Point Point::operator-(Point &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&&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--(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; }

istream & operator>>(istream & input,Point & a) //重载流插入运算符 {

input>>a.x>>a.y; return input; }

ostream & operator<<(ostream & output,Point & a) //重载流插入运算符 {

output<<\ return output; }

int main() {

Point a,b; a.Input(); a.Display(); b.Input(); b.Display(); a=a+b;

cout<<\ a=a-b;

cout<<\ cout<<\重新用'<<','>>'输入输出:\ cout<<\输入a的坐标:\ cin>>a;

cout<<\显示<

cout<<\输入b的坐标:\ cin>>b;

cout<<\显示<

cout<<\ if(a==b) cout<<\ else if(a!=b) cout<<\ return 0; }

运行结果:

//主函数

程序代码:/*3、定义一个矩阵类Matrix,均为M行N列,通过重载运算符\、\,\,

\,\!=\来实现矩阵的相加、相减、输出、输入、自增、自减以及相等、不等的判断。*/

#include using namespace std; const int M=3; const int 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 operator++(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

for(j=0;j

c[i][j]=a[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

c[i][j]=a[i][j]-b[i][j]; } return c[M][N]; }

istream & operator>>(istream & input,Matrix *c) {

int i,j;

for(i=0;i>c[i][j]; }

ostream & operator<<(istream & output,Matrix *c) {

int i,j;

for(i=0;i

Matrix operator++(Matrix *c) {

int i,j;

for(i=0;i

Matrix operator--(Matrix *c) {

int i,j;

for(i=0;i

--c[i][j]; return c[M][N]; }

int operator==(Matrix *a,Matrix *b) {

int i,j;

for(i=0;i

int operator!=(Matrix *a,Matrix *b) {

int i,j;

for(i=0;i

int main() {

Matrix a[M][N],b[M][N],c[M][N]; cout<<\ cin>>a; cin>>b;

cout<<\ cout<<\

cout<<\ c[M][N]=a[M][N]+[M][N]; cout<<\ c[M][N]=a[M][N]-[M][N]; cout<<\

cout<<\ if(a[M][N]==b[M][N]) cout<<\ else cout<<\ if(a[M][N]!=b[M][N])

cout<<\ else cout<<\ return 0; }

4. 程序代码:/*4、定义时间类Time,时间的表示采用24小时制。重载运算符\和\实现时间的输出和输入;

重载运算符\和\实现时间推后和提前若干分钟;重载运算符\和\实现当前时间推后和提前1小时;

重载\、\、\来判断两个时间之间大于、小于、等于以及不等于的关系。 注意:输入时需对数据合法性进行判断。*/ #include //using namespace std;

class Time //Define class Time {

private: int hour,minute,second; public: Time operator--(); Time operator++(); Time operator+(int n); Time operator-(int n); friend istream & operator>>(istream & Input,Time & t); friend ostream & operator<<(ostream & Output,Time & t); friend int operator<(Time &a,Time &b); friend int operator>(Time &a,Time &b); friend int operator==(Time &a,Time &b); };

istream & operator>>(istream & Input,Time & t) //Overload \{ //Make sure hour,minute,second right. Input>>t.hour>>t.minute>>t.second; while(1) {

if(t.hour>24||t.hour<0||t.minute>60||t.minute<0||t.second>60||t.second<0) { cout<<\ Input>>t.hour>>t.minute>>t.second; } if(t.hour<24&&t.hour>0&&t.minute<60&&t.minute>0&&t.second<60||t.second>0) break; } return Input;

}

ostream & operator<<(ostream & Output,Time & t) //Overload \{

Output<

Time Time::operator+(int n) //Overload \{

Time t; t.hour=hour;

t.minute=minute+n; t.second=second; return t; }

Time Time::operator-(int n) { Time t;

t.minute=minute-n; t.hour=hour; t.second=second; return t; }

Time Time::operator++() { Time t;

t.minute=minute; t.second=second; ++hour; t.hour=hour; return t; }

Time Time::operator--() { Time t; t.minute=minute; t.second=second; --hour; t.hour=hour; return t; }

int operator<(Time &a,Time &b) { if(a.hour

//Overload \ //Overload \ //Overload \ //Overload \ else if(a.hour==b.hour&&a.minute

int operator>(Time &a,Time &b) //Overload \{ if(a.hour>b.hour) return 1; else if(a.hour==b.hour&&a.minute>b.minute) return 1; else if(a.hour==b.hour&&a.minute==b.minute&&a.second>b.second) return 1; else return 0; }

int operator==(Time &a,Time &b) //Overload \{ if(a.hour!=b.hour) return 0; else if(a.minute!=b.minute) return 0; else if(a.second!=b.second) return 0; else return 1; }

int main() { Time t,t0; cout<<\(hour,minute,second):\ cin>>t; cout<<\ cout<<\ \ cout<<\ cout<<\ \ t=t+20; cout<

cout<<\ \ t=t-10; cout<

cout<<\ \ ++t; cout<>t0; cout<<\ cout<<\ \ cout<<\ if(t==t0) cout<<\ else if(t>t0) cout<<\ else cout<<\ return 0; }

运行结果:

程序代码:/*5、编写一个程序,处理一个复数与一个double型数相加的运算,结果存放在一个double型的变量d1中,

输出d1的值,再以复数形式输出此值。定义复数(Complex)类,在成员函数中包含重载类型转换运算符:

Operator double(){ return real;}*/ #include //using namespace std;

class Complex //Define class Complex {

private: double real,imag;

public: double Complex::operator+(double a); friend ostream & operator<<(ostream & output,Complex & a); friend istream & operator>>(istream & input,Complex & a); Complex() //构造函数 { real=10; imag=10; } Complex(double a) //定义转换构造函数 { real=a;imag=0; } };

double Complex::operator+(double a) // 重载类型转换符“+” { double b; b=a+real; return b; }

ostream & operator<<(ostream & output,Complex &a) //重载“ <<” { output<

istream & operator>>(istream & input,Complex &a) //重载“ >>” { input>>a.real>>a.imag; return input; }

int main() // 主函数 { Complex s; double a,d1; cout<<\ \ cout<<\ cin>>a; d1=s+a; cout<<\ s=d1; cout<<\ return 0; }

运行结果:

程序代码:/*6、定义一个教师类和一个学生类,二者有一部分数据成员是相同的,例如num,name,sex。

请编程,将一个学生类对象转换为教师类,只将以上3个相同的数据成员移植过去。可以设想为:

一位学生大学毕业了,留校担任教师,他原有的部分数据对现在的教师身份来说仍然是有用的,

应当保留并成为其教师的数据的一部分。*/ #include #include #include using namespace std; class Teacher;

class Student //定义学生类 {

private:

string num,name; string sex; public: void Input(); void Display(); friend class Teacher; friend Teacher Teacher::operator=(Student & s); };

class Teacher //定义教师类 {

private:

string number,na; string s; public: void Input(); void Display(); Teacher operator=(Student & s); };

void Student::Input() { cout<<\对学生信息进行输入(学号、姓名、性别(f 或 m)):\ cin>>num>>name>>sex; }

void Student::Display() { cout<<\对学生信息进行输出:\ cout<

void Teacher::Input() { cout<<\对教师信息进行输入(学号、姓名、性别(f 或 m)):\ cin>>number>>na>>s; }

void Teacher::Display() { cout<<\对教师信息进行输出:\ cout<

Teacher Teacher::operator=(Student &s) {

number=s.num; na=s.name; s=s.sex; }

int main() { Student s; Teacher t; s.Input(); s.Display(); t=s; t.Display(); return 0; }

五. 实验总结

void Student::Display() { cout<<\对学生信息进行输出:\ cout<

void Teacher::Input() { cout<<\对教师信息进行输入(学号、姓名、性别(f 或 m)):\ cin>>number>>na>>s; }

void Teacher::Display() { cout<<\对教师信息进行输出:\ cout<

Teacher Teacher::operator=(Student &s) {

number=s.num; na=s.name; s=s.sex; }

int main() { Student s; Teacher t; s.Input(); s.Display(); t=s; t.Display(); return 0; }

五. 实验总结

本文来源:https://www.bwwdw.com/article/mhw5.html

Top