C++重点和练习

[复制链接]
发表于 2025-12-9 14:25:24 | 显示全部楼层 |阅读模式

 

 作业1:实现类中有类的几个特殊成员函数
  1. #include <iostream>
  2. using namespace std;
  3. class Person
  4. {
  5.     string name;
  6.     int *age;
  7. public:
  8.     Person():name("none"),age(new int(0)){}
  9.     Person(int age):age(new int(age)){}
  10.     Person(string name,int age):name(name),age(new int(age)){}
  11.     Person(const Person &other):name(other.name),age(new int(*(other.age))){}
  12.     ~Person()
  13.     {
  14.         delete age;
  15.     }
  16.     void show();
  17.     void set_age(int age)
  18.     {
  19.         *(this->age) = age;
  20.     }
  21.     Person &operator=(const Person &other)
  22.     {
  23.         name = other.name;
  24.         *age = *(other.age);
  25.         return *this;
  26.     }
  27. };
  28. class Stu
  29. {
  30.     Person p1;
  31.     const double score;
  32. public:
  33.     Stu(string name,int age,double score):p1(name,age),score(score){}
  34.     Stu():score(0){}
  35.     Stu(const Stu &other):p1(other.p1),score(other.score){}
  36.     ~Stu(){  }
  37.     void show();
  38.     void set_age(int age)
  39.     {
  40.         p1.set_age(age);
  41.     }
  42.     Stu &operator=(const Stu &other)
  43.     {
  44.         p1.operator=(other.p1);
  45.         return *this;
  46.     }
  47. };
  48. void Person::show()
  49. {
  50.     cout << "name: " << name << endl;
  51.     cout << "age: " << *age << endl;
  52. }
  53. void Stu::show()
  54. {
  55.     p1.show();
  56.     cout << "score: " << score << endl;
  57. }
  58. int main()
  59. {
  60.     Stu *s1 = new Stu;
  61.     s1->show();
  62.     delete s1;
  63.     cout << endl;
  64.     Stu *s2 = new Stu ("张三",18,99.8);
  65.     s2->show();
  66.     delete s2;
  67.     cout << endl;
  68.     Stu s3("李四",25,25.5);
  69.     s3.show();
  70.     cout << endl;
  71.     Stu s4 = s3;
  72.     s4.show();
  73.     cout << endl;
  74.     s3.set_age(99);
  75.     s3.show();
  76.     cout << endl;
  77.     s4.show();
  78.     cout << endl;
  79.     s4.operator=(s3);
  80.     s4.show();
  81.     return 0;
  82. }
复制代码
作业2:实现自界说Mystring
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4. char c = '\0';
  5. class My_string
  6. {
  7.     char *str;     //记录C风格的字符串
  8.     int size;      //记录字符串长度
  9. public:
  10.     //无参构造
  11.     //有参构造
  12.     //拷贝构造
  13.     //拷贝赋值
  14.     //析构函数
  15.     //at函数
  16.     char &my_at(int num);
  17. };
复制代码
 答案1(优化版):
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4. class Mystring
  5. {
  6.     char *str;
  7.     int len;
  8. public:
  9.     // 无参构造函数
  10.     Mystring() : str(new char[1]), len(0)
  11.     {
  12.         str[0] = '\0';  // 确保空字符串以null结尾
  13.     }
  14.     // 有参构造函数
  15.     Mystring(const char *str)
  16.     {
  17.         len = strlen(str);
  18.         this->str = new char[len + 1];  // +1 为结束符留空间
  19.         strcpy(this->str, str);
  20.     }
  21.     ~Mystring()
  22.     {
  23.         delete [] str;
  24.     }
  25.     //拷贝构造函数
  26.     Mystring(const Mystring &other)
  27.     {
  28.         this->len = other.len;
  29.         this->str = new char[other.len+1];
  30.         strcpy(this->str,other.str);
  31.     }
  32.     // 拷贝赋值函数
  33.     Mystring &operator=(const Mystring &other)
  34.     {
  35.         if (this != &other)
  36.         {  // 添加自赋值检查
  37.             delete[] str;  // 先删除原有内存
  38.             len = other.len;
  39.             str = new char[len + 1];  // +1 为结束符留空间
  40.             strcpy(str, other.str);
  41.         }
  42.         return *this;
  43.     }
  44.     // 加法运算符重载
  45.     Mystring operator+(const Mystring &other) const  // 返回新对象,参数使用const
  46.     {
  47.         Mystring result;
  48.         result.len = this->len + other.len;
  49.         delete[] result.str;  // 删除默认构造函数分配的内存
  50.         result.str = new char[result.len + 1];  // +1 为结束符留空间
  51.         strcpy(result.str, this->str);
  52.         strcat(result.str, other.str);
  53.         return result;
  54.      }
  55.     char &my_at(int location)
  56.     {
  57.         if(location>len)
  58.         {
  59.             perror("越界访问");
  60.         }
  61.         return *(str+location-1);
  62.     }
  63.     void show()
  64.     {
  65.         printf(str);
  66.     }
  67. };
  68. int main()
  69. {
  70.     char s[10] = "hello";
  71.     Mystring str1(s);
  72.     str1.show();
  73.     cout << endl;
  74.     Mystring str2("world");
  75.     str2.show();
  76.     cout << endl;
  77.     Mystring str3 = str2;  // 复制构造
  78.     str3.show();
  79.     cout << endl;
  80.     Mystring str4;
  81.     str4 = str1 + str2;    // 使用加法运算符
  82.     str4.show();
  83.     cout << endl;
  84.     cout << str4.my_at(16) << endl;
  85.     return 0;
  86. }
复制代码
答案2:
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4. class Mystring
  5. {
  6.     char *str;
  7.     int len;
  8. public:
  9.     Mystring():str(new char[4]{0}),len(4){}
  10.     Mystring(const char *str)
  11.     {
  12.         this->len = strlen(str);
  13.         this->str = new char[len];
  14.         for(int i=0;i<len;i++)
  15.         {
  16.             *((this->str)+i) = *(str+i);
  17.         }
  18.     }
  19.     ~Mystring()
  20.     {
  21.         delete [] str;
  22.     }
  23.     Mystring(const Mystring &other)
  24.     {
  25.         this->len = other.len;
  26.         this->str = new char[this->len];
  27.         for(int i=0;i<len;i++)
  28.         {
  29.             *((this->str)+i) = *((other.str)+i);
  30.         }
  31.     }
  32.     // 修改赋值运算符
  33.     Mystring &operator=(const Mystring &other)  
  34.     {
  35.         if(this != &other)  // 添加自赋值检查
  36.         {
  37.             char *temp = new char[other.len];  // 先分配新内存
  38.             for(int i = 0; i < other.len; i++)
  39.             {
  40.                 temp[i] = other.str[i];
  41.             }
  42.             delete[] str;  // 再删除旧内存
  43.             str = temp;
  44.             len = other.len;
  45.         }
  46.         return *this;
  47.     }
  48.     Mystring operator+(Mystring &other)
  49.     {
  50.         Mystring temp;
  51.         delete[] temp.str;  // 释放默认构造的内存
  52.         temp.len = this->len+other.len;
  53.         temp.str = new char [temp.len];
  54.         for(int i = 0;i<temp.len;i++)
  55.         {
  56.             if(i<this->len)
  57.             {
  58.                 *((temp.str)+i) = *((this->str)+i);
  59.             }
  60.             else
  61.             {
  62.                 *((temp.str)+i) = *((other.str+i)-this->len);
  63.             }
  64.         }
  65.         return temp;
  66.     }
  67.     void show()
  68.     {
  69.         for(int i = 0;i<len;i++)
  70.         {
  71.             printf("%c",*((this->str)+i));
  72.         }
  73.     }
  74. };
  75. int main()
  76. {
  77.     char s[10] = "hello";
  78.     Mystring str1(s);
  79.     str1.show();
  80.     cout << endl;
  81.     Mystring str2("world");
  82.     str2.show();
  83.     cout << endl;
  84.     Mystring str3 = str2;
  85.     str3.show();
  86.     cout << endl;
  87.     Mystring str4;
  88.     str4 = str1+str2;
  89.     str4.show();
  90.     cout << endl;
  91.     return 0;
  92. }
复制代码

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

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

×
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表