当前位置: 首页 > news >正文

C++day4

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


http://www.mrgr.cn/news/79819.html

相关文章:

  • yarn 安装问题
  • Linux24.04 安装企业微信
  • 学习记录:js算法(一百二十三):不同路径 II
  • 【nginx】Nginx中location路径匹配无效问题 如“ location /app“
  • 阿里云 云产品流转(实现设备与小程序交互)
  • 人工智能大语言模型起源篇(一),从哪里开始
  • 基于SpringBoot+Vue的高校电动车租赁系统
  • LeetCode118.杨辉三角
  • 系统安全分析与设计
  • Pytorch安装教程(CPU版本)+cv2的安装
  • unity打包sdk热更新笔记
  • transformer学习笔记-自注意力机制(1)
  • 【数据库】E-R模型、函数依赖、范式
  • 数字图像处理考研考点(持续更新)
  • [大数据]Hudi
  • day2:nginx基础(未完待续)
  • 【0363】Postgres内核 从 XLogReaderState readBuf 解析 XLOG Record( 8 )
  • CCF-GESP 编程能力认证 C++ 七级 2024年9月份选择题详细解析
  • 十一、容器化 vs 虚拟化-Docker
  • openGauss开源数据库实战二十一
  • UnityShaderLab-实现溶解效果
  • MVC基础——市场管理系统(三)Clean Architecture
  • Python 处理多人多笔医保缴费异常退回业务
  • nginx 部署 ModSecurity3
  • Elasticsearch:使用阿里 infererence API 及 semantic text 进行向量搜索
  • 自然语言处理的未来愿景