C++题目:定义一个圆类数据成员有颜色、圆心坐标、半径.
来源:学生作业帮助网 编辑:六六作业网 时间:2025/02/02 00:12:03
C++题目:定义一个圆类数据成员有颜色、圆心坐标、半径.
C++题目:定义一个圆类数据成员有颜色、圆心坐标、半径.
C++题目:定义一个圆类数据成员有颜色、圆心坐标、半径.
在此基础上派生出矩形类CRectangle和圆类CCircle.
//矩形类包括左上角坐标、长和宽等数据成员及相关的成员函数(如计算面积、周长、显示矩形的属性值等).
//圆类包括圆心坐标、半径等数据成员及相关的成员函数(如计算面积、周长、显示圆形的属性值等).
//编写一个主函数,对设计的类进行测试.
#include<iostream.h>
#include<string.h>
class CShape//定义父类CShape
{
public:
char color[10];
public:
CShape(){}
CShape(char color[])//构造函数
{
strcpy(this->color,color);
}
};
class CRectangle:CShape//定义子类CRectangle继承CShape
{
private:
int x;
int y;
int height;
int width;
public:
CRectangle(){};
CRectangle(int x,int y,int heigth,int width,char color[]):CShape(color)//构造函数
{
this->x=x;
this->y=y;
this->height=heigth;
this->width=width;
}
void mianJi()//求并显示面积
{
cout<<"面积"<<height*width<<endl;
}
void display()//显示矩形的所有属性
{
cout<<"左上角坐标:("<<x<<","<<y<<")"<<endl;
cout<<"长:"<<height<<endl;
cout<<"宽:"<<width<<endl;
cout<<"颜色为:"<<color<<endl;
}
void zhouChang()//求并显示周长
{
cout<<"周长"<<2*(height+width)<<endl;
}
};
class CCircle:CShape//定义子类CCircle继承CShape
{
private:
int r;
int x;
int y;
public:
CCircle(){};
CCircle(int x,int y,int r,char color[]):CShape(color)//构造函数
{
this->x=x;
this->y=y;
this->r=r;
}
void mianJi()//求并显示面积
{
cout<<"面积"<<3.1415926*r*r<<endl;
}
void display()//显示圆的所有属性
{
cout<<"圆心坐标:("<<x<<","<<y<<")"<<endl;
cout<<"半径:"<<r<<endl;
cout<<"颜色为:"<<color<<endl;
}
void zhouChang()//求并显示周长
{
cout<<"周长"<<2*3.1415926*r<<endl;
}
};
void main()
{
CCircle circle(3,4,5,"red");//定义一个圆对象
CRectangle rectangle(3,4,6,8,"yellow");//定义一个矩形对象
circle.mianJi();
circle.zhouChang();
circle.display();
rectangle.mianJi();
rectangle.zhouChang();
rectangle.display();
}