C++——类的多态性(一) – 作者:Johnson666

目的

掌握运算符重载的使用方法

内容

编程题

1.(1)以成员函数的方式,实现运算符“+”的重载,程序运行结果保持不变; (2)以友元函数的方式,实现运算符“+”的重载,程序运行结果保持不变。

代码:
(1)

#include<iostream.h>
class Box
{
public:
	Box(){}
	Box(int l,int b, int h)
	{
	length=l;
	breadth=b;
	height=h;
	}
	Box operator+(Box& b)
	{
	Box box;
	box.length=length+b.length;
   box.breadth=breadth+b.breadth;
	box.height=height+b.height;
	return box;
	}
   void print()
   {
   cout<<"length:"<<length<<endl;
   cout<<"breadth:"<<breadth<<endl;
   cout<<"height:"<<height<<endl;
   }
private:
	double length;
	double breadth;
	double height;
};
int main()
{
Box Box1(1,2,3);
Box Box2(4,5,6);
Box Box3;
Box3=Box1+Box2;
Box3.print();
return 0;
}

结果:
image.png
(2)

#include<iostream.h>
class Box
{
public:
	Box(){}
	Box(int l,int b, int h)
	{
	length=l;
	breadth=b;
	height=h;
	}
   friend Box operator+(Box& b,Box& b1);
   void print()
   {
   cout<<"length:"<<length<<endl;
   cout<<"breadth:"<<breadth<<endl;
   cout<<"height:"<<height<<endl;
   }
private:
	double length;
	double breadth;
	double height;
};
	Box operator+(Box& b,Box& b1)
	{
	Box box;
	box.length=b1.length+b.length;
    box.breadth=b1.breadth+b.breadth;
	box.height=b1.height+b.height;
	return box;
	}
int main()
{
Box Box1(1,2,3);
Box Box2(4,5,6);
Box Box3;
Box3=Box1+Box2;
Box3.print();
return 0;
}

结果:
image.png

2.掌握以类的成员函数重载运算符的使用方法

代码:

#include<iostream.h>
class Time
{
private:
	int hour;
	int minute;
public:
	Time(){hour=0; minute=0;}
	Time (int h,int m);
	Time operator+(Time &t2);
	void display();
};
Time::Time(int h,int m)
{
hour=h;
minute=m;
}
Time Time::operator +(Time &t2)
{
Time t;
t.hour=hour+t2.hour;
t.minute=minute+t2.minute;
if(t.minute>=60)
{
t.minute-=60;
t.hour++; 
}
return t;
}
void Time::display()
{
cout<<hour<<"小时"<<minute<<"分钟"<<endl;
}
int main()
{
Time t1(3,30),t2(2,40);
Time t;
t=t1+t2;
cout<<"t1+t2=";
t.display();
return 0;
}

image.png

3.掌握以非成员函数重载运算符的使用方法

代码:

#include<iomanip>
#include<iostream.h>
class Matrix
{
private:
int mat[2][3];
public:
Matrix();
friend Matrix operator+(Matrix &a,Matrix &b);
void input();
void display();
};
Matrix::Matrix()
	  {
	  for(int i=0;i<2;i++)
		  for(int j=0;j<3;j++)
			  mat[i][j]=0;
	   }
Matrix operator+(Matrix &a,Matrix &b)
{
Matrix c;
for(int i=0;i<2;i++)
		  for(int j=0;j<3;j++)
			 c.mat[i][j]=a.mat[i][j]+b.mat[i][j];
		  return c;
}
void Matrix::input()
{
for(int i=0;i<2;i++)
		  for(int j=0;j<3;j++)
			  cin>>mat[i][j];
}
void Matrix::display()
{
for(int i=0;i<2;i++)
{
for(int j=0;j<3;j++)
cout<<mat[i][j]<<"  ";
cout<<endl;
}
}
int main()
{
Matrix a,b,c;
cout<<"input a:\n";
a.input();
cout<<"input b:\n";
b.input();
c=a+b;
cout<<"output c:\n";
c.display();
return 0;
}

结果:
image.png

4.定义一个Point类,包含私有的数据成员x,y,按要求完成下面的编程

(1)重载运算符“<<”和“>>”,用于输出输入Point类的对象。
(2)重载运算符“++”和“–”,实现对Point类对象的自增和自减运算(x和y同时加1或减1),要求同时重载前缀和后缀的形式。
(3)重载运算符“+”和“-”,实现两个点坐标的相加与相减。

代码:

#include<iomanip>
#include<iostream.h>
class point
{
private:
int x,y;
public:
friend ostream & operator<<(ostream&,point&);
friend istream & operator>>(istream&,point&);
point operator++()
{
x++;
y++;
return *this;
}
point operator++(int)
{
x++;
y++;
return *this;
}
point operator--()
{
x--;
y--;
return *this;
}
point operator--(int)
{
x--;
y--;
return *this;
}
friend point operator+(point &,point &);
friend point operator-(point &,point &);
};
ostream & operator<<(ostream &out,point &a)
{
	out<<"x坐标为:"<<a.x<<"  y坐标为:"<<a.y<<endl;
	return out;
}
istream & operator>>(istream &in,point &b)
{
	in>>b.x;
	in>>b.y;
	return in;
}

point operator+(point &b,point &b1)
{
point bb;
bb.x=b1.x+b.x;
bb.y=b1.y+b.y;
return bb;
}
point operator-(point &b,point &b1)
{
point bb;
bb.x=b.x-b1.x;
bb.y=b.y-b1.y;
return bb;
}
void getint(int &f)
{
cin>>f;
int a;
a=f;
}
int main()
{
point a,b,f,z;
int c,d,e;
cout<<"请输入两个类对象的x和y坐标:"<<endl;
cout<<"请输入第一个的x和y坐标:"<<endl;
cin>>a;
cout<<a;
cout<<"请输入第二个的x和y坐标:"<<endl;
cin>>b;
cout<<b;
cout<<"请选择:1、两个坐标点自增或自减 2、两个坐标点相加或相减"<<endl;
getint(c);
if(c==1)
{
cout<<"1:自增,2:自减"<<endl;
getint(d);
if(d==1)
{
++a;
b++;
cout<<"自增后x和y坐标为:"<<endl;
cout<<a;
cout<<b;
}
else 
{
--a;
b--;cout<<"自减后x和y坐标为:"<<endl;
cout<<a;
cout<<b;
}
}
else
{
cout<<"1:相加 2:相减"<<endl;
getint(e);
if(e==1)
{
f=a+b;
cout<<"相加为:"<<endl;
cout<<f;
}
else
{
z=a-b;
cout<<"相减为:"<<endl;
cout<<z;
}
}
return 0;
}

结果:

自增:
image.png
自减:
image.png
相加:
image.png
相减:
image.png

来源:freebuf.com 2021-06-03 21:29:38 by: Johnson666

© 版权声明
THE END
喜欢就支持一下吧
点赞0
分享
评论 抢沙发

请登录后发表评论