基类的数据成员在派生类中使用时提供垃圾值

问题描述

在此程序中,每当我尝试访问派生类中基类“ radius”的数据成员时,radius的值都会变为垃圾值。我究竟做错了什么?我该如何解决这个问题?

#include<iostream>
#include<cmath>
using namespace std;
class circle{
    public:
        int radius,circrea;
        void getRadius(){
            cout<<"Enter radius: "<<endl;
            cin>>radius;
            }
        void area(){
            circrea = 3.14*radius*radius;
            }
        void display(){
            cout<<"Area of circle is: "<<circrea<<endl;
            }

};
class cylinder: public circle{
    public:
        int height,cylrea;
        void getHeight(){
            cout<<"Enter height: "<<endl;
            cin>>height;
            }
        void area(){
            cylrea = 2 * 3.14 * radius * (radius + height);
            }
        void display(){
            cout<<"Area of cylinder is: "<<cylrea<<endl;
            }
};
int main(){
    circle c;
    cylinder c1;
    c.getRadius();
    c.area();
    c.display();
    c1.getHeight();
    c1.area();
    c1.display();
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...