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

C++中的特殊成员函数

1思维导图

#include <iostream>using namespace std;//设计一个per类
class Per
{
private:string name;int age;double *weight;double *high;
public://无参构造函数Per(){weight = nullptr;high = nullptr;cout << "Per:: 无参构造函数" << endl;}//有参构造函数Per(string name,int age,double weight,double high):name(name),age(age),weight(new double(weight)),high(new double(high)){cout << "Per::有参构造函数" << endl;}//拷贝构造函数Per(const Per &other):name(other.name),age(other.age),weight(new double(*other.weight)),high(new double(*other.high)){cout << "拷贝构造函数" << endl;}//拷贝赋值函数Per& operator=(const Per &other){if(this!=&other){name = other.name;age = other.age;weight = new double(*other.weight);high = new double(*other.high);}cout << "Per::拷贝赋值函数" << endl;return *this;}//析构函数~Per(){delete weight;delete high;weight = nullptr;high = nullptr;cout << "Per::析构函数" << endl;}void show(){cout << "name = " << name << endl;cout << "age = " << age << endl;cout << "high = " << *high << endl;cout << "weight = " << *weight << endl;}
};//Stu类
class Stu
{
private:double score;Per p1;
public:Stu(){cout << "Stu::无参构造函数" << endl;}Stu(double score,string name ,int age,double wight,double high):score(score),p1(name,age,wight,high){cout << "Stu::有参构造函数" << endl;}Stu& operator=(const Stu &other){if(this!=&other){score = other.score;p1 = other.p1;}cout << "Stu::拷贝赋值函数" << endl;return *this;}Stu(const Stu &other):score(other.score),p1(other.p1){cout << "Stu::拷贝构造函数" << endl;}~Stu(){cout << "Stu::析构函数" << endl;}void show(){p1.show();cout << "Stu::score = " << score << endl;}
};int main()
{Stu s1;Stu s2(99,"张三",18,200,160);s1 =s2;s1.show();Stu s3 = s1;return 0;
}

http://www.dtcms.com/a/424451.html

相关文章:

  • sward,一款比confluence更轻量、简洁的知识管理工具
  • 【Docker项目实战】使用Docker部署TaskTrove任务管理工具
  • 第四部分:VTK常用类详解(第116章 vtkRibbonFilter带状过滤器类)
  • 上海网站建设公司怎么分辨好坏广告在线设计
  • 用PyTorch实现CBOW模型:从原理到实战的Word2Vec入门指南
  • seo网站推广怎么收费有效的网络营销方式
  • 怎么给网站添加图标注册网站租空间哪里租
  • 通快在ECOC 2025上发布最新100G VCSEL
  • 探寻优质abencc实时AI数据引擎
  • 网站项目评价丰台网站开发联系电话
  • 网站推广手段有哪些wordpress电商主题完成度
  • 【java实现项目库保存删除出资人信息】
  • Dubbo源码解读与实战-基础知识(下)
  • html社交网站模板网络认证入口
  • vue自定义数字滚动插件
  • xtuoj 回文串
  • JavaScript中常见关键字了解(不能作为变量名和函数名)
  • 归并排序的三重境界
  • 刷网站软件微信网站建设开发
  • 论坛网站设计wordpress 启动wordpress mu
  • 大数据离线数仓之业务域设计
  • wordpress 主题 建站网站seo 最好
  • 电脑格式化了还能恢复数据吗?硬盘格式化恢复教程分享
  • 网站空间租用多少钱练手网站开发
  • Docker经典安装命令失效排查:Ubuntu/CentOS多系统测试与解决方案
  • 慧知开源重卡充电桩平台建设方案 - 慧知开源充电桩平台(我们是有真实上线案例的)
  • 做教育机构中介网站百度小程序制作网站
  • 软件设计师——03 数据结构(上)
  • 专业定制网站需要什么技能便捷的网站建设
  • 深圳html5网站制作个人网站 商业