为什么这段代码的运行结果total Area和total Perimeter都是0啊?
为什么不是三个相加得到的结果
是这两行代码决定的吗?
totalArea+=pShapes[i]->getArea();
totalPerimeter+=pShapes[i]->getPerimeter();
这是完整的代码
#include <iostream>
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: "<<totalArea<<endl;
cout<<"Total Perimeter: "<<totalPerimeter<<endl;
for(int i=0;i<3;++i)
delete pShapes[i];
return 0;
}
如果我的答案对你有所帮助,麻烦帮忙给个解决,或者关注一下,谢谢
从你代码看,运行结果total Area和total Perimeter都是0,是因为在Shape基类中的getArea()和getPerimeter()方法都返回了0,而在子类Circle和Rectangle中重写getArea()和getPerimeter()方法后,返回的是正确的面积和周长,但是在main函数中,使用基类指针数组pShapes来存储Circle和Rectangle对象,因此调用getArea()和getPerimeter()方法时,实际上调用的是Shape基类中的方法,而不是子类中重写的方法。
你可以将Shape基类中的getArea()和getPerimeter()方法声明为纯虚函数,在子类中必须实现这两个方法。在这种情况下,如果你试图创建Shape对象,编译器将会产生一个错误,因为Shape类是抽象类,不能直接创建对象。修改后的代码如下:
#include <iostream>
using namespace std;
const double PI=3.14;
class Shape{
public:
virtual double getArea()const = 0;
virtual double getPerimeter()const = 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; //Error! Shape is an abstract class
pShapes[0] = nullptr;
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(inti = 1; i < 3; ++i) {
delete pShapes[i];
}
return 0;
}
在修改后的代码中,Shape类的getArea()和getPerimeter()方法被声明为纯虚函数,因此Shape类成为了抽象类,不能直接创建对象。在main函数中,我们使用nullptr来初始化pShapes[0],表示这个元素不需要创建对象,以避免编译错误。在循环中,我们调用pShapes[i]->getArea()和pShapes[i]->getPerimeter()方法时,实际上调用的是子类中重写的方法,而不是Shape类中的方法。这样就可以正确计算出总面积和总周长了。
多态的静态联编问题 static binding,
这两个函数 前加 virtual 即可
class Shape{
public:
virtual double getArea()const;
virtual double getPerimeter()const
}