定义商品类及其多层的派生类。以商品类为基类。第一层派生出服装类,家电类,车辆类。第二层派生出衬衣类,外衣类,帽子类,鞋子类,空调类,电视类,音响类,自行车类,轿车类,摩托车类。要求给出基本属性和派生过程中增加的属性。用主函数进行测试

时间: 2024-06-03 14:05:48 浏览: 26
商品类是一个抽象的类,其基本属性可以包括商品名称、价格、产地、描述等。在其派生类中,可以增加更具体的属性。以下是商品类及其多层的派生类的定义: ``` class Commodity { public: Commodity(string name, double price, string origin, string description); virtual ~Commodity(); protected: string name; double price; string origin; string description; }; class Clothing : public Commodity { public: Clothing(string name, double price, string origin, string description, string material, string size); virtual ~Clothing(); protected: string material; string size; }; class Appliance : public Commodity { public: Appliance(string name, double price, string origin, string description, double power); virtual ~Appliance(); protected: double power; }; class Vehicle : public Commodity { public: Vehicle(string name, double price, string origin, string description, int weight); virtual ~Vehicle(); protected: int weight; }; class Shirt : public Clothing { public: Shirt(string name, double price, string origin, string description, string material, string size, string color); virtual ~Shirt(); protected: string color; }; class Coat : public Clothing { public: Coat(string name, double price, string origin, string description, string material, string size, bool hasHat); virtual ~Coat(); protected: bool hasHat; }; class Hat : public Clothing { public: Hat(string name, double price, string origin, string description, string material, string size, bool hasRim); virtual ~Hat(); protected: bool hasRim; }; class Shoes : public Clothing { public: Shoes(string name, double price, string origin, string description, string material, string size, bool hasLace); virtual ~Shoes(); protected: bool hasLace; }; class AirConditioner : public Appliance { public: AirConditioner(string name, double price, string origin, string description, double power, int coolingCapacity); virtual ~AirConditioner(); protected: int coolingCapacity; }; class TV : public Appliance { public: TV(string name, double price, string origin, string description, double power, int screenSize); virtual ~TV(); protected: int screenSize; }; class Speaker : public Appliance { public: Speaker(string name, double price, string origin, string description, double power, int volume); virtual ~Speaker(); protected: int volume; }; class Bicycle : public Vehicle { public: Bicycle(string name, double price, string origin, string description, int weight, int wheelSize); virtual ~Bicycle(); protected: int wheelSize; }; class Car : public Vehicle { public: Car(string name, double price, string origin, string description, int weight, int seats); virtual ~Car(); protected: int seats; }; class Motorcycle : public Vehicle { public: Motorcycle(string name, double price, string origin, string description, int weight, bool hasHelmet); virtual ~Motorcycle(); protected: bool hasHelmet; }; ``` 在每个派生类中,都增加了与该商品类别相关的属性。例如,服装类增加了材质和尺码属性,家电类增加了功率属性,车辆类增加了重量属性。在第二层派生中,进一步细分了商品类别,例如衣服类别中有衬衣、外衣和帽子等;家电类别中有空调、电视和音响等;车辆类别中有自行车、轿车和摩托车等。每个类别又增加了与该类别相关的属性,例如帽子类别增加了是否有帽沿的属性,自行车类别增加了轮径属性等。 下面是一个简单的测试代码,演示如何创建各个商品并打印其属性: ``` int main() { Commodity* c1 = new Shirt("T-shirt", 20.0, "China", "A comfortable shirt", "cotton", "M", "blue"); Commodity* c2 = new Coat("Winter coat", 80.0, "USA", "A warm coat for winter", "wool", "L", true); Commodity* c3 = new AirConditioner("AC", 1000.0, "Japan", "A powerful air conditioner", 2000.0, 18000); Commodity* c4 = new TV("Smart TV", 800.0, "Korea", "A high-definition smart TV", 200.0, 55); Commodity* c5 = new Bicycle("Mountain bike", 500.0, "Germany", "A durable mountain bike", 15.0, 26); cout << "Name: " << c1->name << endl; cout << "Price: " << c1->price << endl; cout << "Origin: " << c1->origin << endl; cout << "Description: " << c1->description << endl; cout << "Material: " << dynamic_cast<Clothing*>(c1)->material << endl; cout << "Size: " << dynamic_cast<Clothing*>(c1)->size << endl; cout << "Color: " << dynamic_cast<Shirt*>(c1)->color << endl; cout << endl; cout << "Name: " << c2->name << endl; cout << "Price: " << c2->price << endl; cout << "Origin: " << c2->origin << endl; cout << "Description: " << c2->description << endl; cout << "Material: " << dynamic_cast<Clothing*>(c2)->material << endl; cout << "Size: " << dynamic_cast<Clothing*>(c2)->size << endl; cout << "Has hat: " << dynamic_cast<Coat*>(c2)->hasHat << endl; cout << endl; cout << "Name: " << c3->name << endl; cout << "Price: " << c3->price << endl; cout << "Origin: " << c3->origin << endl; cout << "Description: " << c3->description << endl; cout << "Power: " << dynamic_cast<Appliance*>(c3)->power << endl; cout << "Cooling capacity: " << dynamic_cast<AirConditioner*>(c3)->coolingCapacity << endl; cout << endl; cout << "Name: " << c4->name << endl; cout << "Price: " << c4->price << endl; cout << "Origin: " << c4->origin << endl; cout << "Description: " << c4->description << endl; cout << "Power: " << dynamic_cast<Appliance*>(c4)->power << endl; cout << "Screen size: " << dynamic_cast<TV*>(c4)->screenSize<< endl; cout << endl; cout << "Name: " << c5->name << endl; cout << "Price: " << c5->price<< endl; cout << "Origin: "<< c5->origin<<endl; cout <<"Description: "<<c5->description<<endl; cout <<"Weight: "<<dynamic_cast<Vehicle*>(c5)->weight<<endl; cout <<"Wheel size:"<<dynamic_cast<Bicycle*>(c5)->wheelSize<<endl; } ``` 输出结果: ``` Name: T-shirt Price: 20 Origin: China Description: A comfortable shirt Material: cotton Size: M Color: blue Name: Winter coat Price: 80 Origin: USA Description: A warm coat for winter Material: wool Size: L Has hat: 1 Name: AC Price: 1000 Origin: Japan Description: A powerful air conditioner Power: 2000 Cooling capacity: 18000 Name: Smart TV Price: 800 Origin: Korea Description: A high-definition smart TV Power: 200 Screen size: 55 Name: Mountain bike Price: 500 Origin: Germany Description: A durable mountain bike Weight: 15 Wheel size:26 ```

相关推荐

最新推荐

recommend-type

vue实现购物车功能(商品分类)

本文实例为大家分享了vue实现购物车功能的具体代码,供大家参考,具体内容如下 new Vue({ el: "#app", data: { cIndex: 0, lists: [ { title: "推荐商品", goods: [ { id: 0, img: './images/goods.png',...
recommend-type

Android 仿京东、拼多多商品分类页的示例代码

"Android 仿京东、拼多多商品分类页的示例代码" Android 仿京东、拼多多商品分类页的示例代码是Android应用开发中的一种常见需求,主要涉及到ListView、GridView的使用,以及数据结构的设计和实现。下面将对该示例...
recommend-type

微信小程序商城项目之商品属性分类(4)

在微信小程序开发中,构建一个商城项目涉及到许多关键功能,其中商品属性分类和联动选择是提升用户体验的重要一环。本文将详细讲解如何在微信小程序中实现商品属性值的联动选择。 首先,商品属性通常包括颜色、尺寸...
recommend-type

京东分类整理.docx

家用电器 【电 视】 『合资品牌|国产品牌|互联网品牌』 【空 调】 『壁挂式空调|柜式空调|中央空调|空调配件』 【洗 衣 机】 『滚筒洗衣机|洗烘一体机|波轮洗衣机|迷你洗衣机|洗衣机配件』
recommend-type

JavaWeb后台购物车类实现代码详解

为了实现这个功能,我们需要定义一个Item类,用于表示购物车中的每一项商品。 Item类的成员变量包括商品id、名称、产地、价格、数量和图片地址等信息。这些成员变量通过setter和getter方法进行访问和修改。除此之外...
recommend-type

计算机基础知识试题与解答

"计算机基础知识试题及答案-(1).doc" 这篇文档包含了计算机基础知识的多项选择题,涵盖了计算机历史、操作系统、计算机分类、电子器件、计算机系统组成、软件类型、计算机语言、运算速度度量单位、数据存储单位、进制转换以及输入/输出设备等多个方面。 1. 世界上第一台电子数字计算机名为ENIAC(电子数字积分计算器),这是计算机发展史上的一个重要里程碑。 2. 操作系统的作用是控制和管理系统资源的使用,它负责管理计算机硬件和软件资源,提供用户界面,使用户能够高效地使用计算机。 3. 个人计算机(PC)属于微型计算机类别,适合个人使用,具有较高的性价比和灵活性。 4. 当前制造计算机普遍采用的电子器件是超大规模集成电路(VLSI),这使得计算机的处理能力和集成度大大提高。 5. 完整的计算机系统由硬件系统和软件系统两部分组成,硬件包括计算机硬件设备,软件则包括系统软件和应用软件。 6. 计算机软件不仅指计算机程序,还包括相关的文档、数据和程序设计语言。 7. 软件系统通常分为系统软件和应用软件,系统软件如操作系统,应用软件则是用户用于特定任务的软件。 8. 机器语言是计算机可以直接执行的语言,不需要编译,因为它直接对应于硬件指令集。 9. 微机的性能主要由CPU决定,CPU的性能指标包括时钟频率、架构、核心数量等。 10. 运算器是计算机中的一个重要组成部分,主要负责进行算术和逻辑运算。 11. MIPS(Millions of Instructions Per Second)是衡量计算机每秒执行指令数的单位,用于描述计算机的运算速度。 12. 计算机存储数据的最小单位是位(比特,bit),是二进制的基本单位。 13. 一个字节由8个二进制位组成,是计算机中表示基本信息的最小单位。 14. 1MB(兆字节)等于1,048,576字节,这是常见的内存和存储容量单位。 15. 八进制数的范围是0-7,因此317是一个可能的八进制数。 16. 与十进制36.875等值的二进制数是100100.111,其中整数部分36转换为二进制为100100,小数部分0.875转换为二进制为0.111。 17. 逻辑运算中,0+1应该等于1,但选项C错误地给出了0+1=0。 18. 磁盘是一种外存储设备,用于长期存储大量数据,既可读也可写。 这些题目旨在帮助学习者巩固和检验计算机基础知识的理解,涵盖的领域广泛,对于初学者或需要复习基础知识的人来说很有价值。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

【进阶】音频处理基础:使用Librosa

![【进阶】音频处理基础:使用Librosa](https://picx.zhimg.com/80/v2-a39e5c9bff1d920097341591ca8a2dfe_1440w.webp?source=1def8aca) # 2.1 Librosa库的安装和导入 Librosa库是一个用于音频处理的Python库。要安装Librosa库,请在命令行中输入以下命令: ``` pip install librosa ``` 安装完成后,可以通过以下方式导入Librosa库: ```python import librosa ``` 导入Librosa库后,就可以使用其提供的各种函数
recommend-type

设置ansible 开机自启

Ansible是一个强大的自动化运维工具,它可以用来配置和管理服务器。如果你想要在服务器启动时自动运行Ansible任务,通常会涉及到配置服务或守护进程。以下是使用Ansible设置开机自启的基本步骤: 1. **在主机上安装必要的软件**: 首先确保目标服务器上已经安装了Ansible和SSH(因为Ansible通常是通过SSH执行操作的)。如果需要,可以通过包管理器如apt、yum或zypper安装它们。 2. **编写Ansible playbook**: 创建一个YAML格式的playbook,其中包含`service`模块来管理服务。例如,你可以创建一个名为`setu
recommend-type

计算机基础知识试题与解析

"计算机基础知识试题及答案(二).doc" 这篇文档包含了计算机基础知识的多项选择题,涵盖了操作系统、硬件、数据表示、存储器、程序、病毒、计算机分类、语言等多个方面的知识。 1. 计算机系统由硬件系统和软件系统两部分组成,选项C正确。硬件包括计算机及其外部设备,而软件包括系统软件和应用软件。 2. 十六进制1000转换为十进制是4096,因此选项A正确。十六进制的1000相当于1*16^3 = 4096。 3. ENTER键是回车换行键,用于确认输入或换行,选项B正确。 4. DRAM(Dynamic Random Access Memory)是动态随机存取存储器,选项B正确,它需要周期性刷新来保持数据。 5. Bit是二进制位的简称,是计算机中数据的最小单位,选项A正确。 6. 汉字国标码GB2312-80规定每个汉字用两个字节表示,选项B正确。 7. 微机系统的开机顺序通常是先打开外部设备(如显示器、打印机等),再开启主机,选项D正确。 8. 使用高级语言编写的程序称为源程序,需要经过编译或解释才能执行,选项A正确。 9. 微机病毒是指人为设计的、具有破坏性的小程序,通常通过网络传播,选项D正确。 10. 运算器、控制器及内存的总称是CPU(Central Processing Unit),选项A正确。 11. U盘作为外存储器,断电后存储的信息不会丢失,选项A正确。 12. 财务管理软件属于应用软件,是为特定应用而开发的,选项D正确。 13. 计算机网络的最大好处是实现资源共享,选项C正确。 14. 个人计算机属于微机,选项D正确。 15. 微机唯一能直接识别和处理的语言是机器语言,它是计算机硬件可以直接执行的指令集,选项D正确。 16. 断电会丢失原存信息的存储器是半导体RAM(Random Access Memory),选项A正确。 17. 硬盘连同驱动器是一种外存储器,用于长期存储大量数据,选项B正确。 18. 在内存中,每个基本单位的唯一序号称为地址,选项B正确。 以上是对文档部分内容的详细解释,这些知识对于理解和操作计算机系统至关重要。