猿问

C++ 虚函数使用以及继承的问题 ?

C++ 虚函数使用以及继承的问题 


精慕HU
浏览 831回答 2
2回答

收到一只叮咚

#include <iostream>#define PI 3.14using namespace std;class container{public:container(int radius=0){this->radius = radius;/*赋值给类的成员变量*/}virtual void Square()=0;virtual void Volume()=0;int GetRadius(){return radius;} /*给私有成员变量提供一个访问的函数*//*要想修改就提供一个void SetRadius(int radius)的函数*/private:int radius;};class cube:public container{public:cube(int radius1):container(radius1){}virtual void Square(){/*基类的私有成员在这里无法之间反问,可通过基类的成员函数访问,下面凡事访问radius都通过GetRadius()来访问*/square1=4*PI*GetRadius()*GetRadius();cout<<"the cube's square is:"<<square1<<endl;}virtual void Volume(){volume1=(4.0/3)*PI*GetRadius()*GetRadius()*GetRadius();/*4/3等于1了,要写成4.0/3才对 */cout<<"the cube's volume is:"<<volume1<<endl; /*这里应该是volume1*/}private:double square1,volume1;};class sphere:public container{public:sphere(int radius1):container(radius1){}virtual void Square(){square2=6*GetRadius()*GetRadius();cout<<"the sphere's square is:"<<square2<<endl;}virtual void Volume(){volume2=GetRadius()*GetRadius()*GetRadius();cout<<"the sphere's volume is:"<<volume2<<endl;}private:double square2,volume2;};class cylinder:public container{public:cylinder(int radius1):container(radius1){}virtual void Square(){cout<<"please input the cylinder's height=";cin>>height;square3=2*PI*GetRadius()*GetRadius()+2*PI*GetRadius()*height;cout<<"the cylinder's square is:"<<square3<<endl;}virtual void Volume(){volume3=PI*GetRadius()*GetRadius()*height;cout<<"the cylinder's volume is:"<<volume3<<endl;}private:double square3,volume3;int height;};int main(){//container container1;int a;cout<<"Please input a radius=";cin>>a;cube cube1(a);sphere sphere1(a);cylinder cylinder1(a);cube1.Volume();cube1.Square();sphere1.Volume();sphere1.Square();cylinder1.Square();cylinder1.Volume();return 0;}

慕勒3428872

#include <iostream>#define PI 3.14using namespace std;class container{public:container(int radius=0){}virtual void Square()=0;virtual void Volume()=0;//private: //因为这个是私有的,不能被继承类访问,为了简单,我将它改成了publicint radius;};class cube:public container{public:cube(int radius1):container(radius1){} //这里int必须去掉virtual void Square(){square1=4*PI*radius*radius; //////////////////基类的成员是radius,没有1,你写成了radius1cout<<"the cube's square is:"<<square1<<endl;}virtual void Volume(){volume1=4/3*PI*radius*radius*radius; //////////////////基类的成员是radius,没有1,你写成了radius1cout<<"the cube's volume is:"<<square1<<endl;}private:double square1,volume1;};class sphere:public container{public:sphere(int radius1):container(radius1){} //这里int必须去掉virtual void Square(){square2=6*radius*radius; //////////////////基类的成员是radius,没有1,你写成了radius1cout<<"the sphere's square is:"<<square2<<endl;}virtual void Volume(){volume2=radius*radius*radius; //////////////////基类的成员是radius,没有1,你写成了radius1cout<<"the sphere's volume is:"<<volume2<<endl;}private:double square2,volume2;};class cylinder:public container{public:cylinder(int radius1):container(radius1){} //这里int必须去掉virtual void Square(){cout<<"please input the cylinder's height=";cin>>height;square3=2*PI*radius*radius+2*PI*radius*height; //////////////////基类的成员是radius,没有1,你写成了radius1cout<<"the cylinder's square is:"<<square3<<endl;}virtual void Volume(){volume3=PI*radius*radius*height; //////////////////基类的成员是radius,没有1,你写成了radius1cout<<"the cylinder's volume is:"<<volume3<<endl;}private:double square3,volume3;int height;};int main(){//container container1;int a;cout<<"Please input a radius=";cin>>a;cube cube1(a);sphere sphere1(a);cylinder cylinder1(a);cube1.Volume();cube1.Square();sphere1.Volume();sphere1.Square();cylinder1.Square();cylinder1.Volume();return 0;}
随时随地看视频慕课网APP
我要回答