鱼C论坛

 找回密码
 立即注册
查看: 930|回复: 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()函数进行输出。

下面是完整的代码实现:
++
#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 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-12-31 15:05:14 | 显示全部楼层    本楼为最佳答案   
根据题目要求,我们需要定义一个学生类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 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-10-3 10:36

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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