跪求做2道C++编程题 高分悬赏

来源:百度知道 编辑:UC知道 时间:2024/06/01 15:31:40
1 定义Point类,有数据成员X,Y,为其定义友元函数实现重载运算符"+"(完成两个对象相加;以及一个对象与整数相加.)
2 定义一个Shape抽象类,在此基础上派生出Rectangle(矩形)和Circle(圆形)类,二者都有getarea()函数计算对象的面积,getperim()函数计算对象周长.要编写主函数进行验证.

会的大虾帮帮忙啊...

第一题

#include <iostream.h>
class Point
{
public:
Point(int a=0,int b=0):x(a),y(b){};//构造函数
friend Point operator + (Point& a,Point& b);
friend Point operator+(Point& a,int b);
void Display(){cout<<"( "<<x<<" , "<<y<<" )"<<endl;}
private:
int x,y;
};

Point operator+(Point& a,Point& b)
{
return Point(a.x+b.x ,a.y+b.y) ;
};
Point operator+(Point& a,int b)
{
return Point(a.x+b,a.y ) ;
}
void main()
{
Point pa(10,20),pb(1,5),pc=pa+pb;
pc.Display();//测试两个对象相加
pa=pa+10;
pa.Display();//测试对象和函数加
}

第二题

#include <iostream.h>
class Shape
{
public:
void GetArea(){}
void GetPerim(){}
};
class Rectangle:public Shape
{
public:
Rectangle(float w=0,float h=0):width(w),height(h){}
void GetA