news 2026/6/8 21:42:31

建立普通的基类Building,用来存储一座楼房的层数、房间数以及它的总平方米数。建立派生类House,继承Building,并存储下面的内容:卧室与浴室的数量。另外,建立派生类Office,继承Bu

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
建立普通的基类Building,用来存储一座楼房的层数、房间数以及它的总平方米数。建立派生类House,继承Building,并存储下面的内容:卧室与浴室的数量。另外,建立派生类Office,继承Bu
class Builing { private: int cengshu;//层数 int fangjianshu;//房间数 double total_area;//总平方米 public: //构造函数 Builing(int ceng = 0, int fangjian = 0, double area =0.0) :cengshu(ceng), fangjianshu(fangjian), total_area(area) { //检验合法性 if (cengshu < 0) { cengshu = 0; } if (fangjianshu < 0) { fangjianshu = 0; } if (total_area < 0) { total_area = 0; } } //获得属性 int getcengshu()const { return cengshu; } int getfangjianshu()const { return fangjianshu; } double gettotal_area()const { return total_area; } //设置属性 int setcengshu(int ceng) { if (cengshu >= 0) { cengshu = ceng; } } int setfangjianshu(int fangjian) { if (fangjianshu >=0 ) { fangjianshu = fangjian; } } int settotal_area(double area) { if (total_area >=0) { total_area = area; } } //显示信息 virtual void show()const { cout << "楼房信息 " << endl; cout << "层数" << cengshu << endl; cout << "房间数量" << fangjianshu << endl; cout << "总平方米" << total_area << "平方米" << endl; } }; class House :public Builing { private: int bedroom_count;//卧室数量 int bathroom_count;//浴室数量 public: //构造函数 House(int ceng = 0, int fangjian = 0, double area = 0.0, int bedroom = 0, int bathroom = 0) : Builing(ceng, fangjian, area), bedroom_count(bedroom), bathroom_count(bathroom) { //检验合理性 if (bedroom_count < 0) { bedroom_count = 0; } if (bathroom_count < 0) { bathroom_count = 0; } } //显示信息 void show()const { //因为show是虚函数 Builing::show(); cout << "住宅信息" << endl; cout << "卧室数量" << bedroom_count << endl; cout << "浴室数量" << bathroom_count << endl; } }; class Office :public Builing { private: int fire_extinguisher;//灭火器数量 int phone_count;//电话数量 public: Office(int ceng = 0, int fangjian = 0, double area = 0.0, int fire = 0, int phone = 0) : Builing(ceng, fangjian, area), fire_extinguisher(fire), phone_count(phone) { if (fire_extinguisher < 0) { fire_extinguisher = 0; } if (phone_count < 0) { phone_count = 0; } } //显示信息 void show()const { Builing::show(); cout << "办公室信息" << endl; cout << "灭火器数量" << fire_extinguisher << endl; cout << "电话数量" << phone_count << endl; } }; int main() { Builing* b1 = nullptr, * b2 = nullptr; House house(3, 8, 120, 2, 4); b1 = &house; Office office(3, 8, 120, 8, 4); b2 = &office; b1->show(); cout << endl; b2->show(); return 0; }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/4 8:07:26

【信号调制】使用不同的分类器(逻辑回归分类器、决策树、随机森林、全连接密集层和CNN)来训练模型,以预测不同信噪比值下信号的调制类型附Python代码

✅作者简介&#xff1a;热爱科研的Matlab仿真开发者&#xff0c;擅长数据处理、建模仿真、程序设计、完整代码获取、论文复现及科研仿真。&#x1f34e; 往期回顾关注个人主页&#xff1a;Matlab科研工作室&#x1f34a;个人信条&#xff1a;格物致知,完整Matlab代码及仿真咨询…

作者头像 李华
网站建设 2026/5/23 15:34:42

【修正-高斯拉普拉斯滤波器-用于平滑和去噪】基于修正高斯滤波拉普拉斯地震到达时间自动检测研究附Matlab代码

✅作者简介&#xff1a;热爱科研的Matlab仿真开发者&#xff0c;擅长数据处理、建模仿真、程序设计、完整代码获取、论文复现及科研仿真。&#x1f34e; 往期回顾关注个人主页&#xff1a;Matlab科研工作室&#x1f34a;个人信条&#xff1a;格物致知,完整Matlab代码及仿真咨询…

作者头像 李华
网站建设 2026/6/8 14:34:56

数字图像处理篇---颜色空间

核心比喻&#xff1a;颜色的“语言和度量衡”想象一下&#xff0c;你要告诉远方的朋友“天空的颜色”。用词语描述&#xff1a;你说“天蓝色”。但每个人心中的“天蓝”可能略有不同&#xff0c;不精确。用标准色卡比对&#xff1a;你拿出潘通色卡&#xff0c;说“编号 15-4020…

作者头像 李华
网站建设 2026/6/8 18:49:16

数字图像处理篇---颜色空间的发展历程

我们来梳理一下颜色空间的发展历程。这不是枯燥的技术清单&#xff0c;而是一段人类如何从感知颜色&#xff0c;到测量颜色&#xff0c;再到数字时代精准控制颜色的认知进化史。 我将这段历史分为四个关键的“认知阶梯”。 第一阶梯&#xff1a;感知与定性&#xff08;工业革命…

作者头像 李华