c++编程题
虚函数:编写一个程序,计算Circle(圆形)、Square(正方形)、Rectangle(矩形)、Triangle(三角形)、Trapezoid(梯形)的面积与周长。(...
虚函数:编写一个程序,计算Circle(圆形)、Square(正方形)、Rectangle(矩形)、Triangle(三角形)、Trapezoid(梯形)的面积与周长。
(1)设计一个图形抽象基类Shape类,以及由它派生出5个类Circle(圆形)类、Square(正方形)类、Rectangle(矩形)类、Triangle(三角形)类、Trapezoid(梯形)类;
(2)在Shape类中包括纯虚函数area和girth;
(3)分别在Circle(圆形)类、Square(正方形)类、Rectangle(矩形)类、Triangle(三角形)类、Trapezoid(梯形)类中定义这两个函数,用于计算面积和周长。
(4)主函数中要求用基类指针数组,使它的每一个元素指向一个派生类对象,分别计算Circle(圆形)类、Square(正方形)类、Rectangle(矩形)类、Triangle(三角形)类、Trapezoid(梯形)类对象的面积和周长。 展开
(1)设计一个图形抽象基类Shape类,以及由它派生出5个类Circle(圆形)类、Square(正方形)类、Rectangle(矩形)类、Triangle(三角形)类、Trapezoid(梯形)类;
(2)在Shape类中包括纯虚函数area和girth;
(3)分别在Circle(圆形)类、Square(正方形)类、Rectangle(矩形)类、Triangle(三角形)类、Trapezoid(梯形)类中定义这两个函数,用于计算面积和周长。
(4)主函数中要求用基类指针数组,使它的每一个元素指向一个派生类对象,分别计算Circle(圆形)类、Square(正方形)类、Rectangle(矩形)类、Triangle(三角形)类、Trapezoid(梯形)类对象的面积和周长。 展开
展开全部
// 梯形的没写睁哪,不孝咐记得面积公式了,但是写法和其他形状一样
#include <iostream>
#include <cmath>
using namespace std;
const double PI = 3.14;
class Shape {
public:
virtual double area() = 0;
virtual double girth() = 0;
};
class Circle : public Shape {
public:
Circle(double r) : radius(r) {}
double area() { return PI * radius * radius; }
double girth() { return 2 * PI * radius; }
private:
double radius;
};
class Square : public Shape {
public:
Square(double l) : length(l) {}
double area() { return length * length; }
double girth() { return 4 * length; }
private:
double length;
};
class Rectangle : public Shape {
public:
Rectangle(double h, double w) : height(h), width(w) {}
double area() { return height * width; }
double girth() { return 2 * (height + width); }
private:
double height, width;
};
class Triangle : public Shape {
public:
Triangle(double l1, double l2, double l3) : length_1(l1), length_2(l2), length_3(l3) {}
double area() {
return sqrt((length_1 + length_2 + length_3)
* (length_1 + length_2 - length_3)
* (length_1 + length_3 - length_2)
* (length_2 + length_3 - length_1)) / 4;
}
double girth() { return length_1 + length_2 + length_3; }
private:
double length_1, length_2, length_3;
};
int main() {
Shape* ShapeArray[4];
ShapeArray[0] = new Circle(10.0);
ShapeArray[1] = new Square(10.0);
ShapeArray[2] = new Rectangle(10.0, 15.0);
ShapeArray[3] = new Triangle(0.5, 0.6, 0.7);
//print area and girth for each shape;
for (int i = 0; i < 4; ++i) {
cout << ShapeArray[i]->area() << '\t' << ShapeArray[i]->巧早纯girth() << endl;
}
return 0;
}
#include <iostream>
#include <cmath>
using namespace std;
const double PI = 3.14;
class Shape {
public:
virtual double area() = 0;
virtual double girth() = 0;
};
class Circle : public Shape {
public:
Circle(double r) : radius(r) {}
double area() { return PI * radius * radius; }
double girth() { return 2 * PI * radius; }
private:
double radius;
};
class Square : public Shape {
public:
Square(double l) : length(l) {}
double area() { return length * length; }
double girth() { return 4 * length; }
private:
double length;
};
class Rectangle : public Shape {
public:
Rectangle(double h, double w) : height(h), width(w) {}
double area() { return height * width; }
double girth() { return 2 * (height + width); }
private:
double height, width;
};
class Triangle : public Shape {
public:
Triangle(double l1, double l2, double l3) : length_1(l1), length_2(l2), length_3(l3) {}
double area() {
return sqrt((length_1 + length_2 + length_3)
* (length_1 + length_2 - length_3)
* (length_1 + length_3 - length_2)
* (length_2 + length_3 - length_1)) / 4;
}
double girth() { return length_1 + length_2 + length_3; }
private:
double length_1, length_2, length_3;
};
int main() {
Shape* ShapeArray[4];
ShapeArray[0] = new Circle(10.0);
ShapeArray[1] = new Square(10.0);
ShapeArray[2] = new Rectangle(10.0, 15.0);
ShapeArray[3] = new Triangle(0.5, 0.6, 0.7);
//print area and girth for each shape;
for (int i = 0; i < 4; ++i) {
cout << ShapeArray[i]->area() << '\t' << ShapeArray[i]->巧早纯girth() << endl;
}
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询