作业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:
#include <iostream>
#include <cstring>
using namespace std;class Mystring
{char *str;int len;
public:Mystring():str(new char[4]{0}),len(4){}Mystring(const char *str){this->len = strlen(str);this->str = new char[len];for(int i=0;i<len;i++){*((this->str)+i) = *(str+i);}}~Mystring(){delete [] str;}Mystring(const Mystring &other){this->len = other.len;this->str = new char[this->len];for(int i=0;i<len;i++){*((this->str)+i) = *((other.str)+i);}}// 修改赋值运算符Mystring &operator=(const Mystring &other) {if(this != &other) // 添加自赋值检查{char *temp = new char[other.len]; // 先分配新内存for(int i = 0; i < other.len; i++){temp[i] = other.str[i];}delete[] str; // 再删除旧内存str = temp;len = other.len;}return *this;}Mystring operator+(Mystring &other){Mystring temp;delete[] temp.str; // 释放默认构造的内存temp.len = this->len+other.len;temp.str = new char [temp.len];for(int i = 0;i<temp.len;i++){if(i<this->len){*((temp.str)+i) = *((this->str)+i);}else{*((temp.str)+i) = *((other.str+i)-this->len);}}return temp;}void show(){for(int i = 0;i<len;i++){printf("%c",*((this->str)+i));}}};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;return 0;
}