 

作业1:实现类中有类的几个特殊成员函数- #include <iostream>
- using namespace std;
- class Person
- {
- string name;
- int *age;
- public:
- Person():name("none"),age(new int(0)){}
- Person(int age):age(new int(age)){}
- Person(string name,int age):name(name),age(new int(age)){}
- Person(const Person &other):name(other.name),age(new int(*(other.age))){}
- ~Person()
- {
- delete age;
- }
- void show();
- void set_age(int age)
- {
- *(this->age) = age;
- }
- Person &operator=(const Person &other)
- {
- name = other.name;
- *age = *(other.age);
- return *this;
- }
- };
- class Stu
- {
- Person p1;
- const double score;
- public:
- Stu(string name,int age,double score):p1(name,age),score(score){}
- Stu():score(0){}
- Stu(const Stu &other):p1(other.p1),score(other.score){}
- ~Stu(){ }
- void show();
- void set_age(int age)
- {
- p1.set_age(age);
- }
- Stu &operator=(const Stu &other)
- {
- p1.operator=(other.p1);
- return *this;
- }
- };
- void Person::show()
- {
- cout << "name: " << name << endl;
- cout << "age: " << *age << endl;
- }
- void Stu::show()
- {
- p1.show();
- cout << "score: " << score << endl;
- }
- int main()
- {
- Stu *s1 = new Stu;
- s1->show();
- delete s1;
- cout << endl;
- Stu *s2 = new Stu ("张三",18,99.8);
- s2->show();
- delete s2;
- cout << endl;
- Stu s3("李四",25,25.5);
- s3.show();
- cout << endl;
- Stu s4 = s3;
- s4.show();
- cout << endl;
- s3.set_age(99);
- s3.show();
- cout << endl;
- s4.show();
- cout << endl;
- s4.operator=(s3);
- s4.show();
- return 0;
- }
复制代码 作业2:实现自界说Mystring- #include <iostream>
- #include <cstring>
- using namespace std;
- char c = '\0';
- class My_string
- {
- char *str; //记录C风格的字符串
- int size; //记录字符串长度
- public:
- //无参构造
- //有参构造
- //拷贝构造
- //拷贝赋值
- //析构函数
- //at函数
- char &my_at(int num);
- };
复制代码 答案1(优化版):- #include <iostream>
- #include <cstring>
- using namespace std;
- class Mystring
- {
- char *str;
- int len;
- public:
- // 无参构造函数
- Mystring() : str(new char[1]), len(0)
- {
- str[0] = '\0'; // 确保空字符串以null结尾
- }
- // 有参构造函数
- Mystring(const char *str)
- {
- len = strlen(str);
- this->str = new char[len + 1]; // +1 为结束符留空间
- strcpy(this->str, str);
- }
- ~Mystring()
- {
- delete [] str;
- }
- //拷贝构造函数
- Mystring(const Mystring &other)
- {
- this->len = other.len;
- this->str = new char[other.len+1];
- strcpy(this->str,other.str);
- }
- // 拷贝赋值函数
- Mystring &operator=(const Mystring &other)
- {
- if (this != &other)
- { // 添加自赋值检查
- delete[] str; // 先删除原有内存
- len = other.len;
- str = new char[len + 1]; // +1 为结束符留空间
- strcpy(str, other.str);
- }
- return *this;
- }
- // 加法运算符重载
- Mystring operator+(const Mystring &other) const // 返回新对象,参数使用const
- {
- Mystring result;
- result.len = this->len + other.len;
- delete[] result.str; // 删除默认构造函数分配的内存
- result.str = new char[result.len + 1]; // +1 为结束符留空间
- strcpy(result.str, this->str);
- strcat(result.str, other.str);
- return result;
- }
- char &my_at(int location)
- {
- if(location>len)
- {
- perror("越界访问");
- }
- return *(str+location-1);
- }
- void show()
- {
- printf(str);
- }
- };
- int main()
- {
- char s[10] = "hello";
- Mystring str1(s);
- str1.show();
- cout << endl;
- Mystring str2("world");
- str2.show();
- cout << endl;
- Mystring str3 = str2; // 复制构造
- str3.show();
- cout << endl;
- Mystring str4;
- str4 = str1 + str2; // 使用加法运算符
- str4.show();
- cout << endl;
- cout << str4.my_at(16) << endl;
- return 0;
- }
复制代码 答案2:
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |