用c++写一个简单的带有构造函数和析构函数的类
时间: 2024-02-20 08:57:47 浏览: 74
好的,以下是一个简单的C++类,该类包含一个私有成员变量和一个公有成员函数:
```c++
#include <iostream>
using namespace std;
class MyClass {
private:
int x;
public:
MyClass(int a) {
x = a;
cout << "对象已创建,x的值为" << x << endl;
}
~MyClass() {
cout << "对象已销毁" << endl;
}
void print() {
cout << "x的值为" << x << endl;
}
};
int main() {
MyClass obj(10);
obj.print();
return 0;
}
```
这个类叫做MyClass,包含一个整型私有成员变量x和一个公有成员函数print。构造函数用于初始化x的值,并输出一条消息,析构函数用于在对象销毁时输出一条消息。在main函数中,我们创建了一个MyClass对象并调用了它的print函数。当程序执行完毕,该对象会自动被销毁,析构函数也会被调用。
相关问题
用c++写一个简单的带有构造函数和析构函数的类,且具有两个私有成员的例子
好的,下面是一个简单的带有构造函数和析构函数的类,同时具有两个私有成员的例子:
```c++
#include <iostream>
using namespace std;
class MyClass {
private:
int num1;
double num2;
public:
// 构造函数
MyClass(int n, double d) {
num1 = n;
num2 = d;
cout << "对象已创建" << endl;
}
// 析构函数
~MyClass() {
cout << "对象已销毁" << endl;
}
// 其他成员函数
void print() {
cout << "num1 = " << num1 << ", num2 = " << num2 << endl;
}
};
int main() {
MyClass obj(10, 3.14);
obj.print();
return 0;
}
```
这个类叫做 `MyClass`,其中有两个私有成员 `num1` 和 `num2`。构造函数初始化这两个私有成员,析构函数用于在对象销毁时输出一段信息。还有一个公有成员函数 `print()` 用于输出私有成员的值。
在 `main()` 函数中创建了一个 `MyClass` 类的对象 `obj`,并调用了成员函数 `print()` 输出了 `num1` 和 `num2` 的值。在程序结束时,对象 `obj` 会被自动销毁,此时析构函数会被调用,输出一段信息。
使用c++编写一个带有构造函数的日期类(构造函数和析构函数)
好的,以下是一个简单的带有构造函数和析构函数的日期类的示例代码:
```c++
#include <iostream>
class Date {
private:
int year;
int month;
int day;
public:
// 构造函数
Date(int y, int m, int d) {
year = y;
month = m;
day = d;
std::cout << "日期对象已创建!" << std::endl;
}
// 析构函数
~Date() {
std::cout << "日期对象已销毁!" << std::endl;
}
// 获取年份
int getYear() {
return year;
}
// 获取月份
int getMonth() {
return month;
}
// 获取日期
int getDay() {
return day;
}
// 设置年份
void setYear(int y) {
year = y;
}
// 设置月份
void setMonth(int m) {
month = m;
}
// 设置日期
void setDay(int d) {
day = d;
}
};
int main() {
// 创建日期对象
Date date(2021, 10, 1);
// 获取日期信息
std::cout << "日期:" << date.getYear() << "-" << date.getMonth() << "-" << date.getDay() << std::endl;
// 修改日期信息
date.setYear(2022);
date.setMonth(1);
date.setDay(1);
// 获取修改后的日期信息
std::cout << "修改后的日期:" << date.getYear() << "-" << date.getMonth() << "-" << date.getDay() << std::endl;
// 程序结束,日期对象将会自动销毁
return 0;
}
```
在上面的示例代码中,我们定义了一个 Date 类,其中包括私有成员变量 year、month 和 day,以及公有成员函数和构造函数、析构函数。在构造函数中,我们将传入的年份、月份和日期分别赋值给 year、month 和 day 变量,并输出一条创建对象的信息。在析构函数中,我们只输出了一条销毁对象的信息。在主函数中,我们创建了一个 Date 对象,并对其进行了一些操作。在程序结束时,该对象将会自动销毁。
阅读全文
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044901.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)