编写程序,以点point类为基类,派生出矩形类Rectangle和圆类Circle。矩形由左上角的顶点和长 5
设有一个点类Point的定义如下:Point{public:Point(){x=0;y=0;}Point(doublexv,doubleyv){x=xv;y=yv;}Po...
设有一个点类Point的定义如下:
Point {
public:
Point() {x = 0; y = 0; }
Point(double xv,double yv) {x = xv;y = yv;}
Point(Point& pt) { x = pt.x; y = pt.y; }
double getx() { return x; }
double gety() { return y; }
double Area() { return 0; }
void Show() { cout<<"x="<<x<<' '<<"y="<<y<<endl; }
private:
double x,y;
};
编写程序,以点point类为基类,派生出矩形类Rectangle和圆类Circle。矩形由左上角的顶点和长、宽定义。圆由圆心和半径定义。派生类中新增的成员函数position(Point &pt)用于判断任一坐标点是在图形内、还是在图形的边缘上,还是在图形外。 展开
Point {
public:
Point() {x = 0; y = 0; }
Point(double xv,double yv) {x = xv;y = yv;}
Point(Point& pt) { x = pt.x; y = pt.y; }
double getx() { return x; }
double gety() { return y; }
double Area() { return 0; }
void Show() { cout<<"x="<<x<<' '<<"y="<<y<<endl; }
private:
double x,y;
};
编写程序,以点point类为基类,派生出矩形类Rectangle和圆类Circle。矩形由左上角的顶点和长、宽定义。圆由圆心和半径定义。派生类中新增的成员函数position(Point &pt)用于判断任一坐标点是在图形内、还是在图形的边缘上,还是在图形外。 展开
1个回答
展开全部
#include<iostream>
#include<math.h>
using namespace std;
class Point {
public:
Point() {x = 0; y = 0; }
Point(double xv,double yv) {x = xv;y = yv;}
Point(Point& pt) { x = pt.x; y = pt.y; 拆段}
double getx() { return 余锋x; }
double gety() { return y; }
double Area() { return 0; }
void Show() { cout<<"x="<<x<<' '<<"y="<<y<<endl; }
private:
double x,y;
};
class Rectangle:public Point{
double a,b;
double l;
double w;
public:
Rectangle(double aa,double bb,double ll,double ww):Point(aa,bb),a(aa),b(bb),l(ll),w(ww){
}
void position(Point& pt){
if(pt.getx()>a && pt.getx()<a+l && pt.gety()>b-w && pt.gety()<b) cout<<"The point is in the rectangle!"<<endl;
else if((pt.getx()==a &&(pt.gety()>=b-w && pt.gety()<=b)) || (pt.getx()==a+l &&(pt.gety()>=b-w && pt.gety()<=b)) || (pt.gety()==b && (pt.getx()>=a && pt.getx()<=a+l)) || (pt.gety()==b-w && (pt.getx()>=a && pt.getx()<=a+l))){
cout<<"The point is at the edge of the rectangle!"<<endl;
}
else cout<<"The point is out of the rectangle!"<<endl;
}
double Area(){
return l*w;
}
};
class Circle:public Point{
double a,b;
double r;
public:
Circle(double aa,double bb,double rr):Point(aa,bb),a(aa),b(bb),r(rr){
}
void position(Point& pt){
double d=sqrt((pt.getx()-a)*(pt.getx()-a)+(pt.gety()-b)*(pt.gety()-b));
if(d<r) cout<<"The point is in the circle!"<<endl;
else if (d==r) cout<<"The point is on the circle!"<<endl;
else cout<<"The point is out of the circle!"<<endl;
}
double Area(){
return 3.14*r*r;
}
};
//TEST!!!
/*******
int main(){
Point p(1,2);
Rectangle r(0,2,4,2);
旅毁誉cout<<"The area of rectangle: "<<r.Area()<<endl;
r.position(p);
Circle c(0,2,1);
cout<<"The area of circle: "<<c.Area()<<endl;
c.position(p);
}
*/
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询