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

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

会的大虾帮帮忙啊...
展开
 我来答
在世贸天阶灌篮的高飞燕草
2008-06-04 · TA获得超过2380个赞
知道大有可为答主
回答量:2070
采纳率:0%
帮助的人:2187万
展开全部
第一题

#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 GetArea(){cout<<"长方形的面积为:"<<width*height<<endl;}
void GetPerim(){cout<<"长方形的周长为:"<<2*(width+height)<<endl;}
private:
float width,height;
};
class Circle:public Shape
{
public:
Circle(float r):radius(r){}
void GetArea(){cout<<"圆的面积为:"<<3.14*radius*radius<<endl;}
void GetPerim(){cout<<"圆的周长为:"<<2*3.14*radius<<endl;}
private:
float radius;
};

void main()
{
Rectangle a(10,20);
//测试长方形
a.GetArea();
a.GetPerim();
Circle c(80);
//测试圆
c.GetArea();
c.GetPerim();
}
SehDan_Lee
2008-06-04 · 超过17用户采纳过TA的回答
知道答主
回答量:27
采纳率:0%
帮助的人:45万
展开全部
第一题,Point类与整数相加不明白问的什么意思,比如,是X与整数相加还是Y与整数相加,或者是X,Y经过某种运算后和整数相加?希望把问题再描述清楚点。
第一题解法:
#include<iostream.h>
class Point{
double x,y;
public:
Point(double x0=0,double y0=0){ x=x0;y=y0;}
friend Point operator+(Point pt1,Point pt2);
void display();
};
Point operator+(Point pt1,Point pt2)
{
Point temp;
temp.x=pt1.x+pt2.x;
temp.y=pt1.y+pt2.y;
return temp;
}
void Point::display()
{
cout<<"("<<x<<","<<y<<")"<<endl;
}
void main(){
Point s0,s1(1.2,-3.55),s2(-1.5,6);
cout<<"s0=";s0.display();
cout<<"Now add s1+s2 to s0";
s0=s1+s2;
cout<<"s0=";s0.display();
}

第二题解法:

#include<iostream.h>
#define PI 3.14159
class Shape{
protected:
double area,perim;
};
class Rectangle:public Shape{
float w,h;
public:
Rectangle(float x=0,float y=0){w=x;h=y;}
double getarea()
{
area=w*h;
return area;
}
double getperim()
{
perim=2*(w+h);
return perim;
}
};
class Circle:public Shape{
float dia;
public:
Circle(float diameter=0){dia=diameter;}
double getarea()
{
area=PI*(dia/2.0)*(dia/2.0);
return area;
}
double getperim()
{
perim=PI*dia;
return perim;
}
};
void main(){
Rectangle a(2.5,4);
Circle b(6.2);
cout<<"The area of the Rectangle is:"<<a.getarea()<<endl;
cout<<"The perimeter of the Rectangle is:"<<a.getperim()<<endl;
cout<<"The area of the Circle is:"<<b.getarea()<<endl;
cout<<"The perimeter of the Circle is:"<<b.getperim()<<endl;
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
102wangyingli
2008-06-04 · TA获得超过100个赞
知道答主
回答量:112
采纳率:0%
帮助的人:108万
展开全部
1.
#include <iostream.h>

class Point
{
int x,y;
public:
Point()
{
x=0;
y=0;
}
Point(int x,int y)
{
Point::x=x;
Point::y=y;
}
friend Point operator+(Point &p1,Point &p2);
friend Point operator+(Point &p1,int n);
void DisPlay()
{
cout<<"x:"<<x<<" y:"<<y<<endl;
}
};

void main()
{
Point p1(2,4);
Point p2(4,2);
Point p3;
p3=p1+p2;
p3.DisPlay();
p3=p3+1;
p3.DisPlay();
}

Point operator+(Point &p1,Point &p2)
{
Point p;
p.x=p1.x+p2.x;
p.y=p1.y+p2.y;
return p;
}

Point operator+(Point &p1,int n)
{
Point p;
p.x=p1.x+n;
p.y=p1.y+n;
return p;
}
2.
#include<iostream.h>
#define PI 3.14159
class Shape{
protected:
double area,perim;
};
class Rectangle:public Shape{
float w,h;
public:
Rectangle(float x=0,float y=0){w=x;h=y;}
double getarea()
{
area=w*h;
return area;
}
double getperim()
{
perim=2*(w+h);
return perim;
}
};
class Circle:public Shape{
float dia;
public:
Circle(float diameter=0){dia=diameter;}
double getarea()
{
area=PI*(dia/2.0)*(dia/2.0);
return area;
}
double getperim()
{
perim=PI*dia;
return perim;
}
};
void main(){
Rectangle a(2.5,4);
Circle b(6.2);
cout<<"The area of the Rectangle is:"<<a.getarea()<<endl;
cout<<"The perimeter of the Rectangle is:"<<a.getperim()<<endl;
cout<<"The area of the Circle is:"<<b.getarea()<<endl;
cout<<"The perimeter of the Circle is:"<<b.getperim()<<endl;
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
jsjj123
2008-06-05
知道答主
回答量:1
采纳率:0%
帮助的人:0
展开全部
鄙视楼上102wangyingli 抄答案!!!!!!!!强烈鄙视!!!
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式