:编写程序,满足以下 4 点要求:(1)设计一个学生类 Student,该 类拥有的数据成员是学号,姓名,电话号码;(2)定义相应的构造函数和析 构函数;(3)定义显示这些属性的成员函数;(4)在 main()中定义对象数
时间: 2023-06-09 16:05:11 浏览: 238
据,并输出其属性值。
回答:
好的,我理解了您的要求。以下是您要求的程序:
```c++
#include <iostream>
#include <string>
using namespace std;
class Student {
private:
string id; // 学号
string name; // 姓名
string phone; // 电话号码
public:
// 构造函数
Student(string _id, string _name, string _phone) {
id = _id;
name = _name;
phone = _phone;
}
// 析构函数
~Student() {}
// 显示属性的成员函数
void display() {
cout << "学号:" << id << endl;
cout << "姓名:" << name << endl;
cout << "电话号码:" << phone << endl;
}
};
int main() {
Student s1("20210001", "张三", "13800138001"); // 定义对象1
Student s2("20210002", "李四", "13900139001"); // 定义对象2
s1.display(); // 输出对象1的属性值
cout << endl;
s2.display(); // 输出对象2的属性值
return 0;
}
```
这个程序实现了您的要求,定义了一个学生类 `Student`,包含学号、姓名、电话号码三个属性。定义了构造函数、析构函数和显示属性的成员函数。在 `main()` 函数中定义了两个学生对象,并输出它们的属性值。
请问还有其他问题吗?
阅读全文
相关推荐



















