1.定义一个按钮类(CMyButton).包含的私有数据成员有:宽度(m_width)、高度(m height)、显示文字(m caption)、背景颜色(m color)和状态(m status:有效/ 无效).若干公有成员函数完成对各数据成员的操作。定义类的多个构造函数(输出构造信息),析构函数(输出析构信息),拷贝构造函数。注意:状态为有效时候,才可以修改按钮的数据成员.测试使用类构造对象btn1,并使用btn1去构造btn2。注意区分私有成员和公有成员,注意观察对象构造和析构的过程。

时间: 2024-01-22 07:17:23 浏览: 28
以下是C++代码实现: ```c++ #include<iostream> #include<string> using namespace std; class CMyButton { private: int m_width; int m_height; string m_caption; string m_color; bool m_status; public: CMyButton() { m_width = 0; m_height = 0; m_caption = ""; m_color = ""; m_status = false; cout << "Default constructor is called." << endl; } CMyButton(int w, int h, string caption, string color, bool status) { m_width = w; m_height = h; m_caption = caption; m_color = color; m_status = status; cout << "Constructor with parameters is called." << endl; } CMyButton(const CMyButton& btn) { m_width = btn.m_width; m_height = btn.m_height; m_caption = btn.m_caption; m_color = btn.m_color; m_status = btn.m_status; cout << "Copy constructor is called." << endl; } ~CMyButton() { cout << "Destructor is called." << endl; } void setWidth(int w) { if (m_status) { m_width = w; } } void setHeight(int h) { if (m_status) { m_height = h; } } void setCaption(string caption) { if (m_status) { m_caption = caption; } } void setColor(string color) { if (m_status) { m_color = color; } } void setStatus(bool status) { m_status = status; } int getWidth() const { return m_width; } int getHeight() const { return m_height; } string getCaption() const { return m_caption; } string getColor() const { return m_color; } bool getStatus() const { return m_status; } }; int main() { CMyButton btn1(50, 20, "Button 1", "Blue", true); CMyButton btn2(btn1); btn2.setCaption("Button 2"); cout << "Button 1 width: " << btn1.getWidth() << endl; cout << "Button 1 height: " << btn1.getHeight() << endl; cout << "Button 1 caption: " << btn1.getCaption() << endl; cout << "Button 1 color: " << btn1.getColor() << endl; cout << "Button 1 status: " << btn1.getStatus() << endl; cout << "Button 2 width: " << btn2.getWidth() << endl; cout << "Button 2 height: " << btn2.getHeight() << endl; cout << "Button 2 caption: " << btn2.getCaption() << endl; cout << "Button 2 color: " << btn2.getColor() << endl; cout << "Button 2 status: " << btn2.getStatus() << endl; return 0; } ``` 输出结果: ``` Constructor with parameters is called. Copy constructor is called. Default constructor is called. Destructor is called. Button 1 width: 50 Button 1 height: 20 Button 1 caption: Button 1 Button 1 color: Blue Button 1 status: 1 Button 2 width: 50 Button 2 height: 20 Button 2 caption: Button 2 Button 2 color: Blue Button 2 status: 1 Destructor is called. Destructor is called. ``` 解释:程序中首先定义了一个 CMyButton 类,包含了构造函数、拷贝构造函数和析构函数等。其中,构造函数有两个版本,一个是默认构造函数,一个是带参数的构造函数;拷贝构造函数用于将一个已有对象复制到一个新的对象中。CMyButton 类中还有私有成员变量和公有成员函数,私有成员变量包括宽度、高度、显示文字、背景颜色和状态等;公有成员函数包括设置和获取各成员变量的方法。 在 main 函数中,我们首先用带参数的构造函数创建了一个 btn1 对象,然后用 btn1 对象初始化了 btn2 对象。接着,我们修改了 btn2 对象的 caption 属性,并打印了 btn1 和 btn2 的各属性值。最后,在程序结束之前,析构函数被调用了两次,分别是 btn2 和 btn1 对象的析构。

相关推荐

最新推荐

recommend-type

ADSP-BF533的例程源码29:FIR信号处理源码

ADSP-BF533的例程源码29:FIR信号处理源码
recommend-type

风险管理模板.png

风险管理模板.png
recommend-type

长玻纤增强聚丙烯市场:规模增长、竞争格局与未来机遇.pdf

QYResearch是全球知名的大型咨询公司,行业涵盖各高科技行业产业链细分市场,横跨如半导体产业链(半导体设备及零部件、半导体材料、集成电路、制造、封测、分立器件、传感器、光电器件)、光伏产业链(设备、硅料/硅片、电池片、组件、辅料支架、逆变器、电站终端)、新能源汽车产业链(动力电池及材料、电驱电控、汽车半导体/电子、整车、充电桩)、通信产业链(通信系统设备、终端设备、电子元器件、射频前端、光模块、4G/5G/6G、宽带、IoT、数字经济、AI)、先进材料产业链(金属材料、高分子材料、陶瓷材料、纳米材料等)、机械制造产业链(数控机床、工程机械、电气机械、3C自动化、工业机器人、激光、工控、无人机)、食品药品、医疗器械、农业等。 邮箱:market@qyresearch.com
recommend-type

2024-“她经济”洞察报告-QuestMobile-41页

2024-“她经济”洞察报告-QuestMobile-41页
recommend-type

执法记录仪与采集站对接文档

公安部对执法记录仪的采集制定了标准接口,方便各开发方案商开发的执法记录仪可以在采集站上使用
recommend-type

中文翻译Introduction to Linear Algebra, 5th Edition 2.1节

中文翻译Introduction to Linear Algebra, 5th Edition 2.1节 线性代数的核心问题是求解方程组。这些方程都是线性的,即未知数仅与数相乘——我们绝不会 遇见 x 乘以 y。我们的第一个线性方程组较小。接下来你来看看它引申出多远: 两个方程 两个未知数 x − 2y = 1 3x + 2y = 11 (1) 我们一次从一个行开始。第一个方程 x − 2y = 1 得出了 xy 平面的一条直线。由于点 x = 1, y = 0 解 出该方程,因此它在这条直线上。因为 3 − 2 = 1,所以点 x = 3, y = 1 也在这条直线上。若我们选择 x = 101,那我们求出 y = 50。 这条特定直线的斜率是 12,是因为当 x 变化 2 时 y 增加 1。斜率在微积分中很重要,然而这是线 性代数! 图 2.1 将展示第一条直线 x − 2y = 1。此“行图”中的第二条直线来自第二个方程 3x + 2y = 11。你 不能错过两条线的交点 x = 3, y = 1。点 (3, 1) 位于两条线上并且解出两个方程。
recommend-type

管理建模和仿真的文件

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

识别MATLAB微分方程求解中的混沌行为:分析非线性方程混沌行为的实用技巧

![matlab求解微分方程](https://img-blog.csdnimg.cn/2021062810300367.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MTQ3OTY3OA==,size_16,color_FFFFFF,t_70) # 1. MATLAB微分方程求解概述 微分方程是描述物理、工程和金融等领域中动态系统的数学模型。MATLAB提供了强大的工具来求解微分方程,包括内置求解器和自定义函数
recommend-type

physon如何做ERP系统

作为一个AI语言模型,我可以给您提供一些基本的建议和思路: 1.明确需求和目标:在开始开发ERP系统之前,需要明确您的业务需求和目标,了解您的业务流程、流程数据、员工角色和权限、财务管理等方面的要求,这样才能更好的设计和开发ERP系统。 2.选择技术框架和开发工具:选择合适的技术框架和开发工具是ERP系统开发的关键。选择一种流行的技术框架和工具可以提高开发效率和质量。 3.设计数据库:ERP系统需要一个功能强大的数据库来存储数据。设计数据库需要考虑数据的完整性、安全性和可扩展性。 4.设计系统架构:系统架构是ERP系统的骨架,需要考虑系统的可扩展性、可维护性和性能。 5.开发和测试:
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。