编程基础(三十七):PTA运行时错误

PTA运行时错误

您的程序运行时发生错误,比如 C 语言数组越界访问或 Python 语言运行时抛出异常等,
例如:使用基类指针访问派生类对象时也会出错,如想使用,需要对基类指针地址引用
错误代码

    #include<iostream>
    using namespace std;
    const double PI=3.1415926;
    class Container
    {
        public:
        virtual double surface_area()=0;  //纯虚函数surface_area,计算图形的表面积 
        virtual double volume()=0;       //纯虚函数volume,计算图形的体积 
        virtual void display();
    };
    class Sphere:public Container{
        private:
        double radius;
        public:
        Sphere(double a = 0):radius(a){};
        double surface_area(){
            return 4*PI*radius*radius;
        }
        double volume(){
            return 4.0/3*PI*radius*radius*radius;
        }
        void display(){
            cout<<"球体表面积:"<<surface_area()<<",球体体积:"<<volume()<<endl;
        }
    };
    class Cylinder:public Container{
        private:
        double radius,height;
        public:
        Cylinder(double a=0, double b=0):radius(a),height(b){};
        double surface_area(){
            double temp;
            temp = 2*PI*radius*radius+2*PI*radius*height;
            return temp;
        }
        double volume(){
            double temp;
            temp = PI*radius*radius*height;
            return temp;
        }
        void display(){
            cout<<"圆柱体表面积:"<<surface_area()<<",圆柱体体积:"<<volume()<<endl;
        }
    };
    class Cube:public Container{
        private:
        double edgeLength;
        public:
        Cube(double a = 0):edgeLength(a){};
        double surface_area(){
            return 6*edgeLength*edgeLength;
        }
        double volume(){
            return edgeLength*edgeLength*edgeLength;
        }
        void display(){
            cout<<"正方体表面积:"<<surface_area()<<",正方体体积:"<<volume()<<endl;
        }
    };
    int main(){
        int n,type;
        double radius,height,sideLength;
        cin>>n;
        Container* p;
        for(int i=0;i<n;i++){
            cin>>type;
            if(type==1){
                cin>>radius;
                *p = Sphere(radius);
            }
            if(type==2){
                cin>>radius>>height;
                *p = Cylinder(radius,height);
            }
            if(type==3){
                cin>>sideLength;
                *p =Cube(sideLength);
            }
            p->display();
        }
        return 0;
    }

正确代码

#include<iostream>
using namespace std;
const double PI = 3.1415926;
class Container
{
public:
    virtual double surface_area() = 0;  //纯虚函数surface_area,计算图形的表面积 
    virtual double volume() = 0;       //纯虚函数volume,计算图形的体积 
};
class Sphere:public Container {
private:
    double r;
public: 
    Sphere(double x) {
        r = x;
    }
    virtual double surface_area() {
        double S;
        S = 4 * PI * r * r;
        return S;
    }
    virtual double volume() {
        double V;
        V =( 4  * PI * r * r * r)/3;
        return V;
    }
    
};
class Cylinder :public Container {
private:
    double h;
    double r;
public:
    Cylinder(double x, double y) {
        r = x;
        h = y;
    }
    virtual double surface_area() {
        double S;
        S = 2 * PI * r * r + 2 * PI * r * h;
        return S;
    }
    virtual double volume() {
        double S;
        S = PI * r * r * h;
        return S;
    }
};
class Cube :public Container {
private:
    double c;

public:
    Cube(double x) {
        c = x;
    }
    virtual double surface_area() {
        double S;
        S = 6 * c * c;
        return S;
    }
    virtual double volume() {
        double V;
        V = c * c * c;
        return V;
    }
};
int main() {
    int n,i=0;
    cin >> n;

    int ch;
    Container* q;
   
   
        while (i<n) {
            cin >> ch;
            if (ch == 1) {
                double r;
                cin >> r;
                Sphere p(r);
                q = &p;
                cout << "球体表面积:" << q->surface_area() << ",球体体积:" << q->volume()<<endl;
                
            }
            if (ch == 2) {
                double r, h;
                cin >> r >> h;
                Cylinder p(r, h);
                q = &p;
                cout << "圆柱体表面积:" << q->surface_area() << ",圆柱体体积:" << q->volume()<<endl;
              
            }
            if (ch == 3) {
                double c;
                cin >> c;
                Cube p(c);
                q = &p;
                cout << "正方体表面积:" << q->surface_area() << ",正方体体积:" << q->volume()<<endl;
                
            }
            i++;
        }
  
    return 0;
}


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值