您的位置:首页 > 汽车 > 新车 > 网站制作平台建设_网页制作方法视频教程_制作链接的小程序_xp优化大师

网站制作平台建设_网页制作方法视频教程_制作链接的小程序_xp优化大师

2024/12/23 7:19:24 来源:https://blog.csdn.net/JOUzb/article/details/144408943  浏览:    关键词:网站制作平台建设_网页制作方法视频教程_制作链接的小程序_xp优化大师
网站制作平台建设_网页制作方法视频教程_制作链接的小程序_xp优化大师

1、实现课上练习中Person和Stu的拷贝构造和拷贝赋值函数

#include <iostream>using namespace std;class Person
{string name;int *age;
public://输出函数void show();//Person无参构造Person():age(new int(18)){cout << "Person无参构造" << endl;}//Person有参构造Person(string name,int age):name(name),age(new int(age)){cout << "Person的有参构造" << endl;}//深拷贝构造Person(const Person &other):name(other.name),age(new int (*other.age)){cout << "Person深拷贝构造" << endl;}//Person深拷贝赋值Person &operator = (const Person &other){this->name = other.name;this->age = new int(*other.age);cout << "Stu深拷贝赋值" << endl;return *this;}//析构函数~Person(){delete age;cout << "Person析构函数" << endl;}
};class Stu
{Person p1;double score;
public://Stu无参构造Stu():score(66.6) //无参构造需要给const修饰的成员初始值{cout << "Stu的无参构造" << endl;}//Stu有参构造Stu(string name1,int age1,double score):p1(name1,age1),score(score){cout << "Stu的有参构造" << endl;}//Stu输出函数void show();//没有指针成员,不需要手动析构//拷贝构造函数(深拷贝)Stu(const Stu &other):p1(other.p1),score(other.score){cout << "Stu深拷贝构造" << endl;}//Stu深拷贝赋值Stu &operator = (const Stu &other){this->p1 = other.p1;this->score = other.score;cout << "Stu深拷贝赋值" << endl;return *this;}
};void Stu::show()
{p1.show();cout << "score:" << score << endl;
}void Person::show()
{cout << "name:" << name << endl;cout << "*age:" << *age << endl;cout << "age地址:" << age << endl;
}int main()
{cout << "s1" << endl;Stu s1;s1.show();cout << "s2" << endl;Stu s2("张三",18,99.9);s2.show();cout << "s4" << endl;Stu s4 = s2;//深拷贝s4.show();cout << "s5" << endl;Stu s5("李四",22,50.5);s5 = s2;s5.show();return 0;
}

2、完成下列类

#include <iostream>
#include <cstring>
using namespace std;
char c = '\0';
class My_string
{char *str;int size;
public://输出私有项void show();//无参构造My_string():str(new char()){cout << "My_string无参构造" << endl;}//有参构造My_string(const char *str,int size):str(new char[size+1]),size(size){strcpy(this->str,str);cout << "My_string有参构造" << endl;}//深拷贝构造My_string(const My_string &other):str(new char[other.size+1]){strcpy(this->str,other.str);this->size = other.size;cout << "深拷贝构造" << endl;}//深拷贝赋值My_string &operator = (const My_string &other){delete []str;str = new char[size+1];strcpy(this->str,other.str);this->size = other.size;cout << "深拷贝赋值" << endl;return *this;}//析构函数~My_string(){delete []str;cout << "My_string析构函数" << endl;}//at函数char &My_at(int num);
};void My_string::show()
{cout << "str:" << str << endl;cout << "size:" << size << endl;
}
char &My_string::My_at(int num)
{if(num < 0||num >size){cout << "访问过界" << endl;}return str[num];
}
int main()
{My_string s1;s1.show();string str = "hello";const char *cstr = str.c_str();int size = str.length();My_string s2(cstr,size);s2.show();My_string s3 = s2;s3.show();string str2 = "world";const char *cstr2 = str2.c_str();int size2 = str2.length();My_string s4(cstr2,size2);s4 = s2;s4.show();cout << s4.My_at(1) << endl;return 0;
}

Xmind

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com