利用基类、派生类和虚函数的概念编写一个程序计算三角形、矩形和圆形的面积.
来源:学生作业帮 编辑:拍题作业网作业帮 分类:综合作业 时间:2024/04/30 14:45:26
利用基类、派生类和虚函数的概念编写一个程序计算三角形、矩形和圆形的面积.
#include
#include
#define PI 3.14159
using namespace std;
class Shape{
public:
virtual double getArea() = 0;
protected:
double area;
};
class Triangle:public Shape{
public:
Triangle(double a,double b,double c){
this->a = a;
this->b = b;
this->c = c;
}
virtual double getArea(){
double p = (a+b+c)/2;
this->area = sqrt(p*(p-a)*(p-b)*(p-c));
return this->area;
}
private:
double a;
double b;
double c;
};
class Rectangle:public Shape{
public:
Rectangle(double width,double height){
this->width = width;
this->height = height;
}
virtual double getArea(){
this->area = this->width*this->height;
return this->area;
}
private:
double width;
double height;
};
class Circle:public Shape{
public:
Circle(double radius){
this->radius = radius;
}
virtual double getArea(){
this->area = PI*radius*radius;
return this->area;
}
private:
double radius;
};
int main(){
Shape *pTg = new Triangle(3.0,4.0,5.0);
Shape *pRtg = new Rectangle(4.0,5.0);
Shape *pCle = new Circle(5.0);
cout
#include
#define PI 3.14159
using namespace std;
class Shape{
public:
virtual double getArea() = 0;
protected:
double area;
};
class Triangle:public Shape{
public:
Triangle(double a,double b,double c){
this->a = a;
this->b = b;
this->c = c;
}
virtual double getArea(){
double p = (a+b+c)/2;
this->area = sqrt(p*(p-a)*(p-b)*(p-c));
return this->area;
}
private:
double a;
double b;
double c;
};
class Rectangle:public Shape{
public:
Rectangle(double width,double height){
this->width = width;
this->height = height;
}
virtual double getArea(){
this->area = this->width*this->height;
return this->area;
}
private:
double width;
double height;
};
class Circle:public Shape{
public:
Circle(double radius){
this->radius = radius;
}
virtual double getArea(){
this->area = PI*radius*radius;
return this->area;
}
private:
double radius;
};
int main(){
Shape *pTg = new Triangle(3.0,4.0,5.0);
Shape *pRtg = new Rectangle(4.0,5.0);
Shape *pCle = new Circle(5.0);
cout
利用基类、派生类和虚函数的概念编写一个程序计算三角形、矩形和圆形的面积.
编写一个程序计算三角形、正方形和圆形三种图形的面积.依题意,可以抽象出一个基类base,在其中说明一个虚函数,用来求面积
用c++编写程序,定义CShape类,其中包括两个纯虚函数,分别用于计算面积和周长,派生出两个派
应用纯虚函数的使用,定义几个派生类如三角形,矩形,圆形等,求出所有形状的总面积.
c语言,编写程序计算矩形的面积和周长,用两个函数分别计算面积和周长
C语言问题,编写一个程序计算矩形的面积和周长
定义一个Point类,派生出Rectangle类和Circle类,计算各 派生类对象的面积Area().
编写一个程序 ,采用方法重载分别实现圆、三角形、矩形、椭圆的面积计算
定义一个shape抽象类,在此基础上派生出rectangle和circle类,二者均有计算面积周长的函数.
vc++ 应用纯虚函数的使用,定义几个派生类如三角形,矩形,圆形等,求出所有形状的总面积.
定义一个抽象类CShape,包含纯虚函数Area(用来计算面积)和SetData(用来重设形状大小).然后派生出三角形C
用VF编写程序计算三角形面积,三角形底长X和三角形高Y的值由键盘输入.提示:掌握其他形状如长方形,圆形