定义一个学生类,其中有3个数据成员有学号、姓名、年龄,以及若干成员函数。同时编写主函数使用这个类,实现对学生数据的赋值和输出。要求: (1) 使用成员函数实现输入、输出; (2) 使用构造函数和析构函数实现对数据的输入、输出; (3) 编写主函数,定义对象,完成相应功能。
时间: 2023-05-20 15:06:24 浏览: 78
好的,我可以回答这个问题。以下是一个示例学生类的代码:
```cpp
#include <iostream>
#include <string>
using namespace std;
class Student {
public:
Student(string id, string name, int age) {
this->id = id;
this->name = name;
this->age = age;
}
~Student() {}
void input() {
cout << "请输入学生的学号、姓名和年龄:" << endl;
cin >> id >> name >> age;
}
void output() {
cout << "学生的信息为:" << endl;
cout << "学号:" << id << endl;
cout << "姓名:" << name << endl;
cout << "年龄:" << age << endl;
}
private:
string id;
string name;
int age;
};
int main() {
Student stu("001", "张三", 18);
stu.output();
Student stu2("", "", 0);
stu2.input();
stu2.output();
return 0;
}
```
在这个示例中,我们定义了一个名为 Student 的类,它有三个数据成员:学号、姓名和年龄。我们使用构造函数和析构函数来初始化和销毁对象。我们还定义了两个成员函数 input() 和 output(),用于输入和输出学生的信息。在主函数中,我们定义了两个对象 stu 和 stu2,并使用它们来演示如何使用这个类。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](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)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)