题解 | #求长方体表面积#

多态实现求面积体积

http://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,父类声明,也可以实现(面积)
    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 this->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;
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-01 11:47
点赞 评论 收藏
分享
屌丝逆袭咸鱼计划:心态摆好,man,晚点找早点找到最后都是为了提升自己好进正职,努力提升自己才是最关键的😤难道说现在找不到找的太晚了就炸了可以鸡鸡了吗😤早实习晚实习不都是为了以后多积累,大四学长有的秋招进的也不妨碍有的春招进,人生就这样
点赞 评论 收藏
分享
06-07 19:59
门头沟学院 C++
补药卡我啊😭:都快15年前的了还在11新特性
你的简历改到第几版了
点赞 评论 收藏
分享
评论
5
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务