C++ 如何声明面积和体积函数 #包括 使用名称空间std; 阶级形态{ 受保护的: int_w,_h; 公众: 形状(intw,inth):w(w),h(h){} //面积和体积函数的声明 }; 类矩形:公共形状{ 公众: 矩形(intw,inth):形状(w,h){} }; 类立方体:公共形状{ 公众: int_b; 公众: 立方体(intw,inth,intb):形状(w,h),_b(b){} int area(){return 2*(_w*_h+_w*_b+_b*_h);} int volume(){return\u w*\u h*\u b;} }; int main(){ 形状*pt; int w,h,b,v; cin>>w>>h>>b; pt=新矩形(w,h); cout area()volume())==-1) cout

C++ 如何声明面积和体积函数 #包括 使用名称空间std; 阶级形态{ 受保护的: int_w,_h; 公众: 形状(intw,inth):w(w),h(h){} //面积和体积函数的声明 }; 类矩形:公共形状{ 公众: 矩形(intw,inth):形状(w,h){} }; 类立方体:公共形状{ 公众: int_b; 公众: 立方体(intw,inth,intb):形状(w,h),_b(b){} int area(){return 2*(_w*_h+_w*_b+_b*_h);} int volume(){return\u w*\u h*\u b;} }; int main(){ 形状*pt; int w,h,b,v; cin>>w>>h>>b; pt=新矩形(w,h); cout area()volume())==-1) cout,c++,C++,欢迎来到SO 我相信您正在寻找的是如何为两个继承类(矩形和立方体)声明这两个函数 您可以研究的一般主题称为“多态性”,父类可以通过其派生类采用多种形式 下面是一个示例,说明您可能倾向于做什么,但无法按预期工作: #include <iostream> using namespace std; class Shape { protected: int _w, _h; public: Shape(int w, int h)

欢迎来到SO

我相信您正在寻找的是如何为两个继承类(矩形和立方体)声明这两个函数

您可以研究的一般主题称为“多态性”,父类可以通过其派生类采用多种形式

下面是一个示例,说明您可能倾向于做什么,但无法按预期工作

#include <iostream>
using namespace std;

class Shape {
        protected:
        int _w, _h;
        public:
        Shape(int w, int h) : _w(w), _h(h) { }
//declaration of area and volume function
};

class Rectangle : public Shape {
        public:
        Rectangle(int w, int h) : Shape(w, h) { }
};

class Cube : public Shape {
        public:
        int _b;
        public:
        Cube(int w, int h, int b) : Shape(w, h), _b(b) { }
        int area() { return 2 * (_w * _h + _w * _b + _b * _h); }
        int volume() { return _w * _h * _b; }
};

int main() {
    Shape *pt;
    int w, h, b, v;
    cin >> w >> h >> b;
    pt = new Rectangle(w, h);
    cout << pt->area() << " ";
    if ((v = pt->volume()) == -1)
        cout << "Undefined ";
    else
        cout << v << " ";
    pt = new Cube(w, h, b);
    cout << pt->area() << " ";
    if ((v = pt->volume()) == -1)
        cout << "Undefined ";
    else
        cout << v << " ";
}
类形状{
受保护的:
int宽度、高度;
公众:
形状(inta=0,intb=0){
宽度=a;
高度=b;
}
内部区域(){

无法在此处发布说明问题的完整代码,而不是通过链接。请提供。您存在内存泄漏(而您不需要
new
)。发布完整代码不会提示人们发布“完整代码”尼尔。事情就是这样。我们期待一个。这似乎是一个评论,而不是一个答案。是的,我意识到,你还不能写评论,因此,相关阅读:@Enthus3d“请不要因为我试图引导他们解决问题而投我一票”,但这不是“答案”字段用于。答案用于答案,而不是指导。你可能希望看到更好地理解什么是正确的答案。不,链接也不是答案。好的答案应该是自包含的,并且应该是完整的,即使任何外部链接过期。指向某个非现场教程的链接不是答案。太好了,听起来很有希望!@Enthus3d你能详细说明一下吗…你建议的链接示例不清楚,调用类型也不同。你能建议我如何调用定义这些函数吗?顺便说一下,谢谢分享链接
class Shape {
   protected:
      int width, height;

   public:
      Shape( int a = 0, int b = 0){
         width = a;
         height = b;
      }
      int area() {
         cout << "Parent class area :" <<endl;
         return 0;
      }
      int volume() {
         cout << "Parent class volume:" <<endl;
         return 0;
      }
};
class Shape {
   protected:
      int width, height;

   public:
      Shape( int a = 0, int b = 0){
         width = a;
         height = b;
      }
      virtual int area() {
         cout << "Parent class area :" <<endl;
         return 0;
      }
      virtual int volume() {
         cout << "Parent class volume:" <<endl;
         return 0;
      }
};