鱼C论坛

 找回密码
 立即注册
查看: 498|回复: 2

[已解决]C++实验题求教!!

[复制链接]
发表于 2020-6-21 18:54:58 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 ttyule1 于 2020-6-21 19:15 编辑

定义一个学生类,它包含学号(int)、姓名(string)、性别(sex)、分数(float)等数据成员,定义一个对象,重载赋值运算符“=”,使“=”能够实现int、string、float型的数据为该对象赋值;重载运算符“<<”“>>”,使之能够直接输入、输出学生类的对象。
如题,是一道实验题,关键就是重载运算符“=”这一块,没太明白是什么意思,请大家帮忙解决一下!!!

下面是除了“重载赋值运算符“=”,使“=”能够实现int、string、float型的数据为该对象赋值”的代码,请大家帮忙完善一下“重载赋值运算符“=”,使“=”能够实现int、string、float型的数据为该对象赋值”
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;

  4. class Student
  5. {
  6.     private:
  7.         int num;
  8.         string name;
  9.         char sex;
  10.         float score;
  11.     public:
  12.                 Student(){}
  13.         Student(int nu,string na,char se='M',float sc=0)
  14.         {
  15.             num=nu;
  16.             name=na;
  17.             sex=se;
  18.             score = sc;
  19.         }
  20.         friend istream & operator >>(istream&,Student &);
  21.                 friend ostream & operator <<(ostream&,Student &);  
  22. };

  23. istream &operator >>(istream & is ,Student &s )
  24. {
  25.         is>>s.num>>s.name>>s.sex>>s.score;
  26.         return is;
  27. }
  28. ostream &operator <<(ostream & os ,Student &s)
  29. {
  30.         os<<"【学生信息】"<<"学号:"<<s.num<<","<<"姓名:"<<s.name<<","<<"性别:"<<s.sex<<","<<"成绩:"<<s.score<<endl;
  31.         return os;
  32. }

  33. int main( )
  34. {
  35.     Student stu;
  36.         cout<<"请分别输入该学生的学号、姓名、性别和成绩(中间用空格隔开):";
  37.     cin >> stu;
  38.         cout << stu;
  39.         system("pause");
  40.     return 0;
  41. }
复制代码
最佳答案
2020-6-21 20:01:43
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;

  4. class Student
  5. {
  6.     private:
  7.         int num;
  8.         string name;
  9.         char sex;
  10.         float score;
  11.     public:
  12.         Student(){}
  13.         Student(int nu,string na,char se='M',float sc=0)
  14.         {
  15.             num=nu;
  16.             name=na;
  17.             sex=se;
  18.             score = sc;
  19.         }
  20.         friend istream & operator >>(istream&,Student &);
  21.         friend ostream & operator <<(ostream&,Student &);  
  22.         Student & operator =(const Student &);
  23. };

  24. istream &operator >>(istream & is ,Student &s )
  25. {
  26.     is>>s.num>>s.name>>s.sex>>s.score;
  27.     return is;
  28. }
  29. ostream &operator <<(ostream & os ,Student &s)
  30. {
  31.     os<<"【学生信息】"<<"学号:"<<s.num<<","<<"姓名:"<<s.name<<","<<"性别:"<<s.sex<<","<<"成绩:"<<s.score<<endl;
  32.     return os;
  33. }
  34. Student& Student:: operator =(const Student &stu)
  35. {
  36.     if(this!=&stu)
  37.     {
  38.         this->num=stu.num;
  39.         this->name=stu.name;
  40.         this->sex=stu.sex;
  41.         this->score=stu.score;
  42.     }
  43.     return *this;
  44. }

  45. int main()
  46. {
  47.     Student stu;
  48.     cout<<"请分别输入该学生的学号、姓名、性别和成绩(中间用空格隔开):";
  49.     cin >> stu;
  50.     cout << stu;
  51.     Student stu2;
  52.     stu2=stu;
  53.     cout<<stu2;
  54.     system("pause");
  55.     return 0;
  56. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-6-21 19:12:45 | 显示全部楼层
各位大佬帮帮忙!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-21 20:01:43 | 显示全部楼层    本楼为最佳答案   
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;

  4. class Student
  5. {
  6.     private:
  7.         int num;
  8.         string name;
  9.         char sex;
  10.         float score;
  11.     public:
  12.         Student(){}
  13.         Student(int nu,string na,char se='M',float sc=0)
  14.         {
  15.             num=nu;
  16.             name=na;
  17.             sex=se;
  18.             score = sc;
  19.         }
  20.         friend istream & operator >>(istream&,Student &);
  21.         friend ostream & operator <<(ostream&,Student &);  
  22.         Student & operator =(const Student &);
  23. };

  24. istream &operator >>(istream & is ,Student &s )
  25. {
  26.     is>>s.num>>s.name>>s.sex>>s.score;
  27.     return is;
  28. }
  29. ostream &operator <<(ostream & os ,Student &s)
  30. {
  31.     os<<"【学生信息】"<<"学号:"<<s.num<<","<<"姓名:"<<s.name<<","<<"性别:"<<s.sex<<","<<"成绩:"<<s.score<<endl;
  32.     return os;
  33. }
  34. Student& Student:: operator =(const Student &stu)
  35. {
  36.     if(this!=&stu)
  37.     {
  38.         this->num=stu.num;
  39.         this->name=stu.name;
  40.         this->sex=stu.sex;
  41.         this->score=stu.score;
  42.     }
  43.     return *this;
  44. }

  45. int main()
  46. {
  47.     Student stu;
  48.     cout<<"请分别输入该学生的学号、姓名、性别和成绩(中间用空格隔开):";
  49.     cin >> stu;
  50.     cout << stu;
  51.     Student stu2;
  52.     stu2=stu;
  53.     cout<<stu2;
  54.     system("pause");
  55.     return 0;
  56. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-30 19:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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