VC驿站

 找回密码
 加入驿站

QQ登录

只需一步,快速开始

搜索
查看: 989|回复: 2

[已解决]运算符重载

[复制链接]
95_avatar_middle
最佳答案
0 
在线会员 发表于 2021-7-20 14:59:08 | 显示全部楼层 |阅读模式
  1. #include <iostream>
  2. #include <Cstring>
  3. using namespace std;

  4. class Cstudent
  5. {
  6. private:
  7.         char* name;
  8.         char  sex;
  9.         int   num;
  10.         int   age;
  11. public:
  12.         Cstudent(char* p_name, char p_sex, int p_num, int p_age): sex(p_sex), num(p_num), age(p_age)
  13.         {
  14.                 int longx = strlen(p_name)+1;
  15.                 name = new char (longx);
  16.                 memset(name, 0, longx);
  17.                 strcpy(name, p_name);
  18.         }
  19.         ~Cstudent()
  20.         {
  21.                 if(name)
  22.                 {
  23.                         delete name;
  24.                         name = NULL;
  25.                 }
  26.         }
  27.         bool operator==(const Cstudent* stud);
  28.         Cstudent& operator=(const Cstudent* stud);
  29.         void print_f()
  30.         {
  31.                 cout << name << "--" << sex << "--" << num << "--" << age << endl;
  32.         }
  33. };

  34. bool Cstudent:: operator==(const Cstudent* stud)
  35.         {
  36.                 int count = strcmp(this->name, stud->name);
  37.                 if(count == 0 && (this->sex == stud->sex) &&
  38.                    (this->num == stud->num) && (this->age == stud->age))
  39.                    {
  40.                         return true;
  41.                    }
  42.                 else
  43.                 {
  44.                         return false;
  45.                 }
  46.         }
  47.         
  48. Cstudent& Cstudent::operator=(const Cstudent* stud)
  49. {
  50.         int n_len = 0;
  51.     n_len = strlen(stud->name);
  52.     name = new char[n_len + 1];
  53.     memset(name, 0, n_len + 1);
  54.     strcpy(name, stud->name);
  55.     num = stud->num;
  56.     age = stud->age;
  57.     sex = stud->sex;
  58.         return *this;
  59. }
  60.         
  61. int main ()
  62. {
  63.         char arr[] = "zhangsan";
  64.         Cstudent* XX = new Cstudent(arr, 'F', 18, 1001);
  65.         Cstudent* CC = new Cstudent(arr, 'F', 18, 1001);
  66.         
  67.         CC->print_f();
  68.         XX->print_f();
  69.         
  70.         if(CC == XX)
  71.         {
  72.                 cout << "==" << endl;
  73.         }
  74.         else
  75.         {
  76.                 cout << "!=" << endl;
  77.         }
  78.         
  79.         return 0;
  80. }
复制代码


不知道什么地方出现了问题,求帮助!
最佳答案
31_avatar_small
2021-7-20 16:42:35
16行改成 name = new char[longx];
18行、56行要使用strcpy_s
24行改成 delete []name;

  1. #include <iostream>
  2. #include <Cstring>
  3. using namespace std;

  4. class Cstudent
  5. {
  6. private:
  7.         char* name;
  8.         char sex;
  9.         int num;
  10.         int age;
  11. public:
  12.         Cstudent(char* p_name, char p_sex, int p_num, int p_age) : sex(p_sex), num(p_num), age(p_age)
  13.         {
  14.                 int longx = strlen(p_name) + 1;
  15.                 name = new char[longx] { 0 };
  16.                 strcpy_s(name, longx, p_name);
  17.         }
  18.         ~Cstudent()
  19.         {
  20.                 if (name)
  21.                 {
  22.                         delete[]name;
  23.                         name = NULL;
  24.                 }
  25.         }
  26.         bool operator==(const Cstudent* stud);
  27.         Cstudent& operator=(const Cstudent* stud);
  28.         void print_f()
  29.         {
  30.                 cout << name << "--" << sex << "--" << num << "--" << age << endl;
  31.         }
  32. };

  33. bool Cstudent:: operator==(const Cstudent* stud)
  34. {
  35.         int count = strcmp(this->name, stud->name);
  36.         if (count == 0 && (this->sex == stud->sex) &&
  37.                 (this->num == stud->num) && (this->age == stud->age))
  38.         {
  39.                 return true;
  40.         }
  41.         else
  42.         {
  43.                 return false;
  44.         }
  45. }

  46. Cstudent& Cstudent::operator=(const Cstudent* stud)
  47. {
  48.         int n_len = 0;
  49.         n_len = strlen(stud->name);
  50.         name = new char[n_len + 1]{ 0 };
  51.         strcpy_s(name, n_len, stud->name);
  52.         num = stud->num;
  53.         age = stud->age;
  54.         sex = stud->sex;
  55.         return *this;
  56. }

  57. int main()
  58. {
  59.         char arr[] = "zhangsan";
  60.         Cstudent* XX = new Cstudent(arr, 'F', 18, 1001);
  61.         Cstudent* CC = new Cstudent(arr, 'F', 18, 1001);

  62.         CC->print_f();
  63.         XX->print_f();

  64.         if (CC == XX)
  65.         {
  66.                 cout << "==" << endl;
  67.         }
  68.         else
  69.         {
  70.                 cout << "!=" << endl;
  71.         }

  72.         return 0;
  73. }
复制代码




上一篇:vs2919
下一篇:基础班,菜单工具栏美化CTrueColorToolBar类,转换到vs2010出问题
31_avatar_middle
最佳答案
62 
在线会员 发表于 2021-7-20 16:42:35 | 显示全部楼层    本楼为最佳答案   
bestAnswer
16行改成 name = new char[longx];
18行、56行要使用strcpy_s
24行改成 delete []name;

  1. #include <iostream>
  2. #include <Cstring>
  3. using namespace std;

  4. class Cstudent
  5. {
  6. private:
  7.         char* name;
  8.         char sex;
  9.         int num;
  10.         int age;
  11. public:
  12.         Cstudent(char* p_name, char p_sex, int p_num, int p_age) : sex(p_sex), num(p_num), age(p_age)
  13.         {
  14.                 int longx = strlen(p_name) + 1;
  15.                 name = new char[longx] { 0 };
  16.                 strcpy_s(name, longx, p_name);
  17.         }
  18.         ~Cstudent()
  19.         {
  20.                 if (name)
  21.                 {
  22.                         delete[]name;
  23.                         name = NULL;
  24.                 }
  25.         }
  26.         bool operator==(const Cstudent* stud);
  27.         Cstudent& operator=(const Cstudent* stud);
  28.         void print_f()
  29.         {
  30.                 cout << name << "--" << sex << "--" << num << "--" << age << endl;
  31.         }
  32. };

  33. bool Cstudent:: operator==(const Cstudent* stud)
  34. {
  35.         int count = strcmp(this->name, stud->name);
  36.         if (count == 0 && (this->sex == stud->sex) &&
  37.                 (this->num == stud->num) && (this->age == stud->age))
  38.         {
  39.                 return true;
  40.         }
  41.         else
  42.         {
  43.                 return false;
  44.         }
  45. }

  46. Cstudent& Cstudent::operator=(const Cstudent* stud)
  47. {
  48.         int n_len = 0;
  49.         n_len = strlen(stud->name);
  50.         name = new char[n_len + 1]{ 0 };
  51.         strcpy_s(name, n_len, stud->name);
  52.         num = stud->num;
  53.         age = stud->age;
  54.         sex = stud->sex;
  55.         return *this;
  56. }

  57. int main()
  58. {
  59.         char arr[] = "zhangsan";
  60.         Cstudent* XX = new Cstudent(arr, 'F', 18, 1001);
  61.         Cstudent* CC = new Cstudent(arr, 'F', 18, 1001);

  62.         CC->print_f();
  63.         XX->print_f();

  64.         if (CC == XX)
  65.         {
  66.                 cout << "==" << endl;
  67.         }
  68.         else
  69.         {
  70.                 cout << "!=" << endl;
  71.         }

  72.         return 0;
  73. }
复制代码
95_avatar_middle
最佳答案
0 
ico_lz  楼主| 发表于 2022-1-2 22:02:26 | 显示全部楼层
cpp2019 发表于 2021-7-20 16:42
16行改成 name = new char[longx];
18行、56行要使用strcpy_s
24行改成 delete []name;

不好意思,当中发生了一些事情,导致我现在才重新上线给您一个最佳答案,对于中间给您产生的困扰,我表示抱歉
您需要登录后才可以回帖 登录 | 加入驿站 qq_login

本版积分规则

×【发帖 友情提示】
1、请回复有意义的内容,请勿恶意灌水;
2、纯数字、字母、表情等无意义的内容系统将自动删除;
3、若正常回复后帖子被自动删除,为系统误删的情况,请重新回复其他正常内容或等待管理员审核通过后会自动发布;
4、感谢您对VC驿站一如既往的支持,谢谢合作!

关闭

站长提醒上一条 /2 下一条

QQ|小黑屋|手机版|VC驿站 ( 辽ICP备09019393号-4 )|网站地图wx_jqr

GMT+8, 2023-12-3 19:19

Powered by CcTry.CoM

© 2009-2021 cctry.com

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