4月25日 C++day4

#include <iostream>
using namespace std;class Person
{const string name;int age;char sex;
public:Person():name("lisi"){cout << "Person无参构造" << endl;}Person(string name,int age,char sex):name(name),age(age),sex(sex){cout << "Person有参构造" << endl;}void show(){cout <<"person的show:"<< name << age << sex <<endl;}Person(const Person &other):name(other.name){this->age = other.age;this->sex=other.sex;cout << "Person的拷贝构造函数" << endl;}Person &operator=(const Person &other){if(&other!=this){string name=other.name;this->age=other.age;this->sex=other.sex;cout << "Person的拷贝赋值函数" << endl;}return *this;}
};
class Stu
{Person p1;double *pd;
public:Stu():pd(new double){cout << "Stu的无参构造" << endl;}Stu(string name,int age,char sex,double score):p1(name,age,sex),pd(new double(score)){cout << "Stu有参构造" << endl;}void show(){p1.show();cout << *pd <<endl;}Stu(const Stu &other):p1(other.p1){this->pd=other.pd;}Stu &operator=(const Stu &other){if(&other!=this){this->p1=other.p1;*(this->pd)=*(other.pd);cout << "Stu的拷贝赋值函数" << endl;}return *this;}
};
int main()
{Person p2("wangwu",20,'n');p2.show();Person p3=p2;p3.show();Person p4;p4=p2;p4.show();Stu s2("zhangsan",20,'n',99.2);s2.show();Stu s3=s2;s3.show();Stu s4;s4=s2;s4.show();return 0;
}
#include <iostream>
#include <cstring>
using namespace std;using namespace std;
class myString
{private:char *str;          //变化c风格的字符串int size;            //记录字符串的实际长度public://无参构造myString() : str(new char), size(0){str[0] = '\0';cout<<"无参构造"<<endl;}//有参构造myString(const char* s){size = strlen(s);str = new char[size + 1];strcpy(str, s);cout<<"有参构造"<<endl;}//拷贝构造myString(const myString &other){size = other.size;str = new char[size + 1];strcpy(str, other.str);cout<<"拷贝构造函数"<<endl;}//拷贝赋值函数myString& operator=(const myString &other){if (this != &other) {size = other.size;str = new char[size + 1];strcpy(str, other.str);cout<<"拷贝赋值函数"<<endl;}return *this;}//析构函数~myString(){delete[] str;cout<<"析构"<<endl;}//判空函数void empty(){if(size == 0){cout<<"空"<<endl;}else{cout<<"非空"<<endl;}}//size函数void mysize(){cout<<"size = "<<size<<endl;}//c_str函数char* c_str(){return str;}//at函数char &at(int pos){if (pos < 0 || pos >= size) {cout<<"位置不合法"<<endl;}return str[pos];}
};
int main()
{myString s1("hello");myString s2 = s1;myString s3;cout<<s1.at(4)<<endl;cout<<s1.c_str()<<endl;cout<<s2.at(4)<<endl;cout<<s2.c_str()<<endl;s2.empty();s2.mysize();return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/650386.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

vue项目打包时因为图片问题报错

执行 npm run build命令打包项目时报错&#xff0c;看起来是图片的问题&#xff1a; package.json里面image-webpack-loader的版本是^7.0.1 解决方案&#xff1a; 1、先卸载 npm uninstall image-webpack-loader 2、用cnpm重新安装 cnpm install image-webpack-loader --save…

二倍体胡桃树(Juglans regia L.)基因组-文献精读-10

A haplotype-resolved genome provides insight into allele-specific expression in wild walnut (Juglans regia L.) 二倍体解析基因组为野生胡桃树&#xff08;Juglans regia L.&#xff09;的等位基因特异性表达提供了洞察 摘要 野生种质资源因其特殊的性状表现&#xff…

git提交常用

git config --global user.name "你的名字或昵称" git config --global user.email "你的邮箱" 第一次上传到码云 1.找到要提交到码云的文件夹 右击打开Git Bash Here 2.用命令行创建本地仓库 git init 3.将待全部文件放入缓冲区 git add . 4.提交缓…

短距离无线通信-Zigbee

阅读引言&#xff1a; 最近在复习之前做过的项目&#xff0c; 所以向将zigbee这个协议分享出来&#xff0c; 一方面可以给需要的人看到&#xff0c; 一方面也方便自己整理zigbee的知识。 目录 一、什么是 ZigBee? 二、Zigbee 通信频段和信道 三、Zigbee协议和Zigbee协议栈 …

泽尼克波前拟合 matlab

tic; %tic()函数可以用来启动一个计时器m zernikePhase unwrappedPhaseMask((img_rows/2-0.3*img_rows1):(img_rows/20.3*img_rows),(img_cols/2-0.3*img_cols1):(img_cols/20.3*img_cols)); [zernike_rows, zernike_cols] size…

prompt提示词:AI英语词典优化版Pro,让AI教你学英语,通过AI实现一个网易有道英语词典

目录 一、前言二、效果对比三、优化《AI英语词典》提示词四、其他获奖作品链接 一、前言 不可思议&#xff01;我的AI有道英语字典助手竟然与百度千帆AI应用创意挑战赛K12教育主题赛榜首作品差之毫厘 &#xff0c;真的是高手都是惺惺相惜的&#xff0c;哈哈&#xff0c;自恋一…

后端工程师——C++工程师如何准备面试?

相比 Java 语言方向,C++ 入门简单,精通难,找工作竞争压力更小,但 C++ 依然是近年来招聘的热门岗位之一。本文将从以下三个方面进行详细讲解,帮助你对 C++ 相关岗位的就业前景、岗位要求、学习路线等有更充分的了解。 C++工程师面试准备 上两篇文章对 C++ 工程师的招聘需求…

Python:解析pyserial串口通讯

简介&#xff1a;串行接口简称串口&#xff0c;也称串行通信接口或串行通讯接口&#xff08;通常指COM接口&#xff09;&#xff0c;是采用串行通信方式的扩展接口。串行接口 &#xff08;Serial Interface&#xff09;是指数据一位一位地顺序传送。其特点是通信线路简单&#…

Spring Boot 中Mybatis使用Like的使用方式和注意点

说明 模糊查询在项目中还是经常使用的&#xff0c;本文就简单整理Mybatis中使用Like进行模糊查询的几种写法以及一些常见的问题。 使用Springboot简单配置一下Mybatis&#xff0c;然后进行说明。Springboot集成Mybatis这里就不做介绍了&#xff0c;这里我们主要介绍一下在mybat…

简单把玩下SpringAI

Hello大家好&#xff0c;今天写一些不烧脑的文章&#xff0c;我们来体验一下Spring的新框架Spring AI&#xff0c;只是简单玩玩不深入&#x1f60f; Spring AI 简介 Spring AI是为了简化人工智能相关应用程序的开发Spring AI的诞生&#xff0c;灵感来自于LangChain等项目 Oll…

【Java EE】总结12种锁策略以及synchronized的实现原理

˃͈꒵˂͈꒱ write in front ꒰˃͈꒵˂͈꒱ ʕ̯•͡˔•̯᷅ʔ大家好&#xff0c;我是xiaoxie.希望你看完之后,有不足之处请多多谅解&#xff0c;让我们一起共同进步૮₍❀ᴗ͈ . ᴗ͈ აxiaoxieʕ̯•͡˔•̯᷅ʔ—CSDN博客 本文由xiaoxieʕ̯•͡˔•̯᷅ʔ 原创 CSDN 如…

在线课程|基于SprinBoot+vue的在线课程管理系统(源码+数据库+文档)

在线课程目录 基于SprinBootvue的在线课程管理系统 一、前言 二、系统设计 三、系统功能设计 1 管理员模块的实现 2在线课程 四、数据库设计 五、核心代码 六、论文参考 七、最新计算机毕设选题推荐 八、源码获取&#xff1a; 博主介绍&#xff1a;✌️大厂码农|毕…