题解 | #多态实现求面积体积#

多态实现求面积体积

https://www.nowcoder.com/practice/826ed8e0d67042c49e32a7293c873538

#include<bits/stdc++.h>
using namespace std;
class rectangle {
  private:
    int length, width;
  public:
    rectangle(int x, int y) {
        length = x;
        width = y;
    }
    void set(int x, int y) {
        length = x;
        width = y;
    }
    int getlength() {
        return length;
    }
    int getwidth() {
        return width;
    }
    // write your code here...
    virtual int getval() {
        return this->length * this->width;
    }

};
class cuboid: public rectangle {
  private:
    int height;
  public:
    cuboid(int x, int y, int z): rectangle(x, y) {
        height = z;
    }
    // write your code here...
    virtual int getval() {
        return rectangle::getval() * this->height;
    }


};
int main() {
    int x, y, z;
    cin >> x >> y >> z;
    cuboid a(x, y, z);
    rectangle b(x, y);

    rectangle* p = &b;
    cout << p->getval() << '\n';

    p = &a;
    cout << p->getval();
    return 0;
}

全部评论

相关推荐

02-11 17:47
已编辑
门头沟学院 Java
神哥不得了:神哥来啦~建议先在网上找一些高频的八股去背,然后再去广泛的背八股,这样的学习会更有效率一些,简历的这两个项目建议换掉,换成两个高质量的项目,这样的话获得面试的比例会更高一点,专业技能的话排版要注意一下,要加句号的话就都加,要不加就都不加,荣誉奖项的话写在教育经历里边吧,这个确实没有太多的含金量
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务