1)
#include
using namespace std;
const double PI=3.14;
class Shape{
public:
double getArea()const;
double getPerimeter()const ;
};
double Shape::getArea()const{
return 0;
}
double Shape::getPerimeter()const{
return 0;
}
class Circle: public Shape{
public:
Circle(double r) : radius(r) {}
double getArea() const;
double getPerimeter() const;
private:
double radius;
};
double Circle::getArea( )const{
return PI*radius*radius;
}
double Circle::getPerimeter( )const{
return 2*PI*radius;
}
class Rectangle: public Shape{
public:
Rectangle( double w, double h): width(w), height(h) {}
double getArea()const;
double getPerimeter()const;
private:
double width;
double height;
};
double Rectangle::getArea()const{
return width*height;
}
double Rectangle::getPerimeter( )const{
return 2*(width+height);
}
int main(){
Shape *pShapes[3]; //定义基类指针数组
pShapes[0]=new Shape;
pShapes[1]=new Rectangle(3,4);
pShapes[2]=new Circle(1.0);
double totalArea=0, totalPerimeter=0;
for(int i=0;i<3;++i){
totalArea+=pShapes[i]->getArea();
totalPerimeter+=pShapes[i]->getPerimeter();
}
cout<<"Total Area: "<"Total Perimeter: "<for(int i=0;i<3;++i)
delete pShapes[i];
return 0;
}
2)修改 1)中的 Shape 类,将 getArea 和 getPerimeter 定义为虚函数,以实现多态
效果,并增加虚析构函数。重新运行程序,记录程序的运行效果。
3)修改 2)中的 Shape 类,将 getArea 和 getPerimeter 定义为纯虚函数,以实现多
态效果。修改主函数,重新运行程序,记录程序的运行效果。
class Shape{
public:
};
_double Shape::getArea()const{_
_return 0;_
}
_double Shape::getPerimeter()const{
return 0;_
}
int main(){
Shape *pShapes[3]; //定义基类指针数组
_pShapes[0]=new Shape;_
pShapes[0]=new Rectangle(3,4);
pShapes[1]=new Circle(1.0);
pShapes[2]=new Rectangle(2,1);
double totalArea=0, totalPerimeter=0;
for(int i=0;i<3;++i){
totalArea+=pShapes[i]->getArea();
totalPerimeter+=pShapes[i]->getPerimeter();
}
cout<<"Total Area: "<"Total Perimeter: "<for(int i=0;i<3;++i)
delete pShapes[i];
return 0;
}
运行结果:
class Triangle : public Shape{
public:
Triangle(double newA, double newB, double newC);
virtual double getArea( )const;
virtual double getPerimeter( )const;
private:
double a, b, c;
};
【提示】计算三角形面积,可以参考海伦公式。调用 sqrt 函数时需要包含头
文件:#include
测试主程序修改如下。
int main(){
Shape *pShapes[3]; //定义基类指针数组
pShapes[0]=new Rectangle(3,4);
pShapes[1]=new Circle(1.0);
pShapes[2]=new Triangle(3,4,5);
double totalArea=0, totalPerimeter=0;
for(int i=0;i<3;++i){
totalArea+=pShapes[i]->getArea();
totalPerimeter+=pShapes[i]->getPerimeter();
}
cout<<"Total Area: "<"Total Perimeter: "<for(int i=0;i<3;++i)
delete pShapes[i];
}
(2)
#include <iostream>
using namespace std;
const double PI = 3.14;
class Shape {
public:
virtual double getArea() const {
return 0;
}
virtual double getPerimeter() const {
return 0;
}
virtual ~Shape() {}
};
class Circle : public Shape {
public:
Circle(double r) : radius(r) {}
double getArea() const override {
return PI * radius * radius;
}
double getPerimeter() const override {
return 2 * PI * radius;
}
private:
double radius;
};
class Rectangle : public Shape {
public:
Rectangle(double w, double h) : width(w), height(h) {}
double getArea() const override {
return width * height;
}
double getPerimeter() const override {
return 2 * (width + height);
}
private:
double width;
double height;
};
int main() {
Shape* pShapes[3];
pShapes[0] = new Shape;
pShapes[1] = new Rectangle(3, 4);
pShapes[2] = new Circle(1.0);
double totalArea = 0, totalPerimeter = 0;
for (int i = 0; i < 3; ++i) {
totalArea += pShapes[i]->getArea();
totalPerimeter += pShapes[i]->getPerimeter();
}
cout << "Total Area: " << totalArea << endl;
cout << "Total Perimeter: " << totalPerimeter << endl;
for (int i = 0; i < 3; ++i)
delete pShapes[i];
return 0;
}
(3)
#include <iostream>
using namespace std;
const double PI = 3.14;
class Shape {
public:
virtual double getArea() const = 0;
virtual double getPerimeter() const = 0;
virtual ~Shape() {}
};
class Circle : public Shape {
public:
Circle(double r) : radius(r) {}
double getArea() const override {
return PI * radius * radius;
}
double getPerimeter() const override {
return 2 * PI * radius;
}
private:
double radius;
};
class Rectangle : public Shape {
public:
Rectangle(double w, double h) : width(w), height(h) {}
double getArea() const override {
return width * height;
}
double getPerimeter() const override {
return 2 * (width + height);
}
private:
double width;
double height;
};
int main() {
Shape* pShapes[3];
pShapes[0] = new Rectangle(3, 4);
pShapes[1] = new Circle(1.0);
double totalArea = 0, totalPerimeter = 0;
for (int i = 0; i < 2; ++i) {
totalArea += pShapes[i]->getArea();
totalPerimeter += pShapes[i]->getPerimeter();
}
cout << "Total Area: " << totalArea << endl;
cout << "Total Perimeter: " << totalPerimeter << endl;
for (int i = 0; i < 2; ++i)
delete pShapes[i];
return 0;
}