(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;
}
(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;
}
程序1:掌握以类的成员函数重载运算符的使用方法。
源代码:
#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;
}
运行结果截图:
程序2:掌握以非成员函数重载运算符的使用方法。
源代码:
运行结果截图:
自定义编程:
定义一个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;
}
运行结果截图:
自增:
相加:
来源:freebuf.com 2021-06-03 12:18:39 by: yggcwhat
请登录后发表评论
注册