根据题目要求,我们需要定义一个学生类Student,并在main()函数中创建两个学生对象并调用display()函数进行输出。
首先,我们需要在类的定义中添加构造函数和析构函数。构造函数用于初始化对象的属性,而析构函数用于释放对象所占用的资源。由于输出样例中提到了构造函数和析构函数的调用,我们可以在这两个函数中添加一些输出语句。
接下来,我们还需要定义一个display()函数,用于输出学生对象的信息。display()函数中需要输出学生的学号、姓名和性别。
最后,在main()函数中创建两个学生对象,分别赋予不同的值,并调用display()函数进行输出。
下面是完整的代码实现:
- ++
- #include <iostream>
- #include <string>
- using namespace std;
- class Student {
- private:
- int num;
- string name;
- char sex;
- public:
- Student(int n, string na, char s) {
- cout << "Constructor called." << endl;
- num = n;
- name = na;
- sex = s;
- }
-
- ~Student() {
- cout << "Destructor called." << endl;
- }
-
- void display() {
- cout << "num:" << num << endl;
- cout << "name:" << name << endl;
- cout << "sex:" << sex << endl;
- }
- };
- int main()
- {
- Student stud1(10010,"Wang_li",'f');
- stud1.display();
- Student stud2(10011,"Zhang_fun",'m');
- stud2.display();
- return 0;
- }
复制代码
执行上述代码,就能得到题目要求的输出结果。
希望你能顺利完成这个练习,如果还有其他问题,欢迎随时提问!
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。