鱼C论坛

 找回密码
 立即注册
查看: 1378|回复: 1

[已解决]定义一个学生类Student,使得main()函数能够得到指定的输出结果

[复制链接]
发表于 2023-12-31 15:05:00 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
类定义:
定义一个学生类Student,使得main()函数能够得到指定的输出结果

main()函数如下:
/* 请在这里填写答案 */
int main()
  {Student stud1(10010,"Wang_li",'f');
   stud1.display();
   Student stud2(10011,"Zhang_fun",'m');
   stud2.display();
   return 0;
}

输入样例:

输出样例:
在这里给出相应的输出。例如:

Constructor called.
num:10010
name:Wang_li
sex:f

Constructor called.
num:10011
name:Zhang_fun
sex:m

Destructor called.
Destructor called.

这题怎么写
最佳答案
2023-12-31 15:05:14
根据题目要求,我们需要定义一个学生类Student,并在main()函数中创建两个学生对象并调用display()函数进行输出。

首先,我们需要在类的定义中添加构造函数和析构函数。构造函数用于初始化对象的属性,而析构函数用于释放对象所占用的资源。由于输出样例中提到了构造函数和析构函数的调用,我们可以在这两个函数中添加一些输出语句。

接下来,我们还需要定义一个display()函数,用于输出学生对象的信息。display()函数中需要输出学生的学号、姓名和性别。

最后,在main()函数中创建两个学生对象,分别赋予不同的值,并调用display()函数进行输出。

下面是完整的代码实现:

  1. ++
  2. #include <iostream>
  3. #include <string>

  4. using namespace std;

  5. class Student {
  6. private:
  7.     int num;
  8.     string name;
  9.     char sex;
  10. public:
  11.     Student(int n, string na, char s) {
  12.         cout << "Constructor called." << endl;
  13.         num = n;
  14.         name = na;
  15.         sex = s;
  16.     }
  17.    
  18.     ~Student() {
  19.         cout << "Destructor called." << endl;
  20.     }
  21.    
  22.     void display() {
  23.         cout << "num:" << num << endl;
  24.         cout << "name:" << name << endl;
  25.         cout << "sex:" << sex << endl;
  26.     }
  27. };

  28. int main()
  29. {
  30.     Student stud1(10010,"Wang_li",'f');
  31.     stud1.display();
  32.     Student stud2(10011,"Zhang_fun",'m');
  33.     stud2.display();
  34.     return 0;
  35. }
复制代码


执行上述代码,就能得到题目要求的输出结果。

希望你能顺利完成这个练习,如果还有其他问题,欢迎随时提问!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-12-31 15:05:14 | 显示全部楼层    本楼为最佳答案   
根据题目要求,我们需要定义一个学生类Student,并在main()函数中创建两个学生对象并调用display()函数进行输出。

首先,我们需要在类的定义中添加构造函数和析构函数。构造函数用于初始化对象的属性,而析构函数用于释放对象所占用的资源。由于输出样例中提到了构造函数和析构函数的调用,我们可以在这两个函数中添加一些输出语句。

接下来,我们还需要定义一个display()函数,用于输出学生对象的信息。display()函数中需要输出学生的学号、姓名和性别。

最后,在main()函数中创建两个学生对象,分别赋予不同的值,并调用display()函数进行输出。

下面是完整的代码实现:

  1. ++
  2. #include <iostream>
  3. #include <string>

  4. using namespace std;

  5. class Student {
  6. private:
  7.     int num;
  8.     string name;
  9.     char sex;
  10. public:
  11.     Student(int n, string na, char s) {
  12.         cout << "Constructor called." << endl;
  13.         num = n;
  14.         name = na;
  15.         sex = s;
  16.     }
  17.    
  18.     ~Student() {
  19.         cout << "Destructor called." << endl;
  20.     }
  21.    
  22.     void display() {
  23.         cout << "num:" << num << endl;
  24.         cout << "name:" << name << endl;
  25.         cout << "sex:" << sex << endl;
  26.     }
  27. };

  28. int main()
  29. {
  30.     Student stud1(10010,"Wang_li",'f');
  31.     stud1.display();
  32.     Student stud2(10011,"Zhang_fun",'m');
  33.     stud2.display();
  34.     return 0;
  35. }
复制代码


执行上述代码,就能得到题目要求的输出结果。

希望你能顺利完成这个练习,如果还有其他问题,欢迎随时提问!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-17 08:16

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表