程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程解疑 >> c++-C++程序,公有繼承之後成員的值變成了隨機值,怎麼回事兒

c++-C++程序,公有繼承之後成員的值變成了隨機值,怎麼回事兒

編輯:編程解疑
C++程序,公有繼承之後成員的值變成了隨機值,怎麼回事兒

#include
#include
using namespace std;

const float Pi = 3.1415;

class C {
public:
C(float r, float h) :Radius(r), high(h) {}
protected:
float Radius;
float high;
};

class Round :public C {
public:
void supArea();
void volume();
};

void Round::supArea() {
float s;
s = 4 * Pi*pow(C::Radius, 2);
cout << "\n球的表面積為:" << s << endl;
}

void Round::volume() {
float v;
v = 4 * Pi*pow(C::Radius, 3) / 3;
cout << "\n球的體積為:" << v << endl;
}

class Cylinder :public C {
public:
void supArea();
void volume();
};

void Cylinder::supArea() {
float s;
s = 2 * Pi*pow(C::Radius, 2) + C::high * 2 * Pi*C::Radius;
cout << "\n圓柱的表面積為:" << s << endl;
}

void Cylinder::volume() {
float v;
v = Pi*pow(C::Radius, 2)*C::high;
cout << "\n圓柱的體積為:" << v << endl;
}

class Cone :public C {
public:
void supArea();
void volume();
};

void Cone::supArea() {
float s;
s = Pi*pow(C::Radius, 2) + Pi*C::Radius*C::high;
cout << "\n圓錐的表面積為:" << s << endl;
}

void Cone::volume() {
float v;
v = 1 / 3 * Pi*pow(C::Radius, 2)*sqrt(pow(C::high, 2) - pow(C::Radius, 2));
cout << "\n圓錐的體積為:" << v << endl;
}

int main() {
C q(1, 2);
Round r;
r.supArea();
r.volume();
Cylinder c1;
c1.supArea();
c1.volume();
Cone c2;
c2.supArea();
c2.volume();
return 0;
}

最佳回答:


#include
using namespace std;
const float Pi = 3.1415;
class C {
public:
C(float r, float h) :Radius(r), high(h) {}
protected:
float Radius;
float high;
};
class Round :public C {
public:
Round(float r,float h):C(r,h){}
void supArea();
void volume();
};
void Round::supArea() {
float s;

s = 4 * Pi*pow(C::Radius, 2);
cout << "\n球的表面積為:" << s << endl;

}
void Round::volume() {
float v;
v = 4 * Pi*pow(C::Radius, 3) / 3;
cout << "\n球的體積為:" << v << endl;
}
class Cylinder :public C {
public:
Cylinder(float r,float h):C(r,h){}
void supArea();
void volume();
};
void Cylinder::supArea() {
float s;
s = 2 * Pi*pow(C::Radius, 2) + C::high * 2 * Pi*C::Radius;
cout << "\n圓柱的表面積為:" << s << endl;
}
void Cylinder::volume() {
float v;
v = Pi*pow(C::Radius, 2)*C::high;
cout << "\n圓柱的體積為:" << v << endl;
}
class Cone :public C {
public:
Cone(float r,float h):C(r,h){}
void supArea();
void volume();
};
void Cone::supArea() {
float s;
s = Pi*pow(C::Radius, 2) + Pi*C::Radius*C::high;
cout << "\n圓錐的表面積為:" << s << endl;
}
void Cone::volume() {
float v;
v = 1 / 3 * Pi*pow(C::Radius, 2)*sqrt(pow(C::high, 2) - pow(C::Radius, 2));
cout << "\n圓錐的體積為:" << v << endl;
}
int main() {

Round r(1,2);
r.supArea();
r.volume();
Cylinder c1(1,2);
c1.supArea();
c1.volume();
Cone c2(1,2);
c2.supArea();
c2.volume();

return 0;

}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved