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 对象的析构。