声明一个Shape(形状)基类,它有两个派生类:Circle(圆)和Square(正方形),
来源:学生作业帮 编辑:大师作文网作业帮 分类:综合作业 时间:2024/09/25 02:28:01
声明一个Shape(形状)基类,它有两个派生类:Circle(圆)和Square(正方形),
要求利用多态性的概念,分别以虚函数的形式完成对圆和正方形的周长及面积的计算.
要求:Shape类的数据成员包括中心点的坐标,Circle类和Square类初始值分别给出:圆的圆心和半径;正方形的中心和一个顶点.
要求利用多态性的概念,分别以虚函数的形式完成对圆和正方形的周长及面积的计算.
要求:Shape类的数据成员包括中心点的坐标,Circle类和Square类初始值分别给出:圆的圆心和半径;正方形的中心和一个顶点.
#include
#include
#define PI 3.14
class Point
{
public:
Point(){x=0;y=0;}
Point(float x,float y){this->x=x;this->y=y;}
Point(){}
float getX(){ return x;}
float getY(){ return y;}
void setX(float x){this->x=x;}
void setY(float y){this->y=y;}
private:
float x;
float y;
};
class Shape
{
public:
Shape(){this->center.setX(0);this->center.setY(0);}
Shape(Point center){
this->center.setX(center.getX());
this->center.setY(center.getY());
}
Shape(){}
virtual float getArea(){return 0;}
virtual float getCirc(){return 0;}
protected:
Point center;
};
class Circle:public Shape
{
public:
Circle(){}
Circle(Point center,float radius)
{
this->center.setX(center.getX());
this->center.setY(center.getY());
this->radius=radius;
}
Circle(){}
float getArea(){return radius*radius*PI;}
float getCirc(){return 2*PI*radius;}
private:
float radius;
};
class Square:public Shape
{
public:
Square(){}
Square(Point center,Point top){
this->center.setX(center.getX());
this->center.setY(center.getY());
this->top.setX(top.getX());
this->top.setY(top.getY());
}
Square(){}
float getArea(){
float a=top.getX()-center.getX();
float b=top.getY()-center.getY();
return 2*(a*a+b*b);
}
float getCirc(){
float a=top.getX()-center.getX();
float b=top.getY()-center.getY();
return 4*sqrt(2)*sqrt(a*a+b*b);
}
private:
Point top;
};
void main(){
Point p1(0,0),p2(4,4);
Circle c1=Circle(p1,4);
Square s1=Square(p1,p2);
Shape s=c1;
cout
#include
#define PI 3.14
class Point
{
public:
Point(){x=0;y=0;}
Point(float x,float y){this->x=x;this->y=y;}
Point(){}
float getX(){ return x;}
float getY(){ return y;}
void setX(float x){this->x=x;}
void setY(float y){this->y=y;}
private:
float x;
float y;
};
class Shape
{
public:
Shape(){this->center.setX(0);this->center.setY(0);}
Shape(Point center){
this->center.setX(center.getX());
this->center.setY(center.getY());
}
Shape(){}
virtual float getArea(){return 0;}
virtual float getCirc(){return 0;}
protected:
Point center;
};
class Circle:public Shape
{
public:
Circle(){}
Circle(Point center,float radius)
{
this->center.setX(center.getX());
this->center.setY(center.getY());
this->radius=radius;
}
Circle(){}
float getArea(){return radius*radius*PI;}
float getCirc(){return 2*PI*radius;}
private:
float radius;
};
class Square:public Shape
{
public:
Square(){}
Square(Point center,Point top){
this->center.setX(center.getX());
this->center.setY(center.getY());
this->top.setX(top.getX());
this->top.setY(top.getY());
}
Square(){}
float getArea(){
float a=top.getX()-center.getX();
float b=top.getY()-center.getY();
return 2*(a*a+b*b);
}
float getCirc(){
float a=top.getX()-center.getX();
float b=top.getY()-center.getY();
return 4*sqrt(2)*sqrt(a*a+b*b);
}
private:
Point top;
};
void main(){
Point p1(0,0),p2(4,4);
Circle c1=Circle(p1,4);
Square s1=Square(p1,p2);
Shape s=c1;
cout
声明一个Shape(形状)基类,它有两个派生类:Circle(圆)和Square(正方形),
C++实现:5.定义一个具有多态性的基类Shape,派生出以下一些类,圆Circle类(坐标点和半径),长方形Recta
//已定义一个Shape抽象类,在此基础上派生出矩形Rectangle和圆形Circle类,二者都有
定义一个shape抽象类,在此基础上派生出rectangle和circle类,二者均有计算面积周长的函数.
定义一个Point类,派生出Rectangle类和Circle类,计算各 派生类对象的面积Area().
.写一个C++程序,定义抽象基类Shape,由它派生出一个基类Point,再由Point派生出2个派生类:Circle(
下面Shape类是一个表示形状的抽象类,Area ( )为求图形面积的函数.请从Shape类派生梯形类(Trapezoi
用C++定义一个shape类,它派生出circle和Rectangle类,用show()显示各类图形的相关信息,最后还要
声明一个形状(Shape)基类,具有size、position、color等成员变量,move、draw等成员函数,由此
设计三个类:几何形状类(Shape)、圆类(Circle)、矩形类(Rectangle),要求:
1. 编一个使用派生类的程序求圆柱体(Cylinder)的体积.设计一个圆类(Circle)和一个矩形类(Rectang
设计三个类:几何形状类(Shape)、圆类(Circle)、矩形类(Rectangle),要求:(1)Shape类设计成