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

华清远见作业第三十四天——C++(第三天)

思维导图:

题目:

设计一个Per类,类中包含私有成员:姓名、年龄、指针成员身高、体重,再设计一个Stu类,类中包含私有成员:成绩、Per类对象p1,设计这两个类的构造函数、析构函数和拷贝构造函数。

代码:

#include <iostream>using namespace std;
class Per
{
private:string name;int age;int *heigh;int *weith;
public://构造函数Per(){cout << "Per::无参构造函数" << endl;cout << this << endl;}Per(string name,int age,int heigh,int weith):name(name),age(age),heigh(new int(heigh)),weith(new int(weith)){cout << "Per::有参构造函数" << endl;cout << this << endl;}//析构函数~Per(){cout << "Per::析构函数" << endl;cout << this << endl;}//拷贝函数Per(const Per &other):name(other.name),age(other.age),heigh(new int(*other.heigh)), weith(new int(*other.weith)){cout << "Stu::拷贝构造函数" << endl;cout << this << endl;}void show(){cout << "名字=" << name << endl;cout << "年龄=" << age << endl;cout << "身高=" << *heigh << endl;cout << "体重=" << *weith << endl;}
};
class Stu
{
private:int score;Per p1;
public://构造函数Stu(){cout << "Stu::无参构造函数" << endl;cout << this << endl;}Stu(int score,string name,int age,int heigh,int weith):score(score),p1(name,age,heigh,weith){cout << "Stu::有参构造函数" << endl;cout << this << endl;}//析构函数~Stu(){cout << "Stu::析构函数" << endl;cout << this << endl;}//拷贝函数Stu(const Stu &other):score(other.score),p1(other.p1){cout << "Stu::拷贝构造函数" << endl;cout << this << endl;}void show(){cout << "成绩=" << score << endl;cout << this << endl;p1.show();}
};
int main()
{Stu s1;Stu s2(88,"sss",22,123,32);cout << "&s1=" << &s1 << endl;cout << "&s2=" << &s2 << endl;s2.show();Stu s3=s2;s3.show();cout << "&s3=" << &s3 << endl;return 0;
}

运行效果:

http://www.lryc.cn/news/290420.html

相关文章:

  • Shell中正则表达式
  • Flutter Canvas 属性详解与实际运用
  • Django配置websocket时的错误解决
  • (免费分享)springboot,vue在线考试系统
  • WebSocket 整合 记录用法
  • 推荐5个我常用的软件,简单高效
  • 代码随想录训练营第三十一天|122.买卖股票的最佳时机II55.跳跃游戏45.跳跃游戏II
  • python17-Python的字符串格式化
  • HTTPS 之fiddler抓包--jmeter请求
  • Kotlin快速入门系列6
  • w24文件上传之PHP伪协议
  • SQL注入攻击 - 基于时间的盲注
  • 比VS Code快得多
  • 将一个excel文件里面具有相同参数的行提取后存入新的excel
  • Linux下安装edge
  • Java / Spring Boot + POI 给 Word 添加水印
  • Unity打包Android,jar文件无法解析的问题
  • postman之接口参数签名(js接口HMAC-SHA256签名)
  • 从c到c++——6:auto
  • 前端面试题:字符串中字符出现的最多次数
  • 获取鼠标点击图片时候的坐标,以及利用html 中的useMap 和area 实现图片固定位置的点击事件
  • webassembly003 TTS BARK.CPP
  • HiveSQL题——排序函数(row_number/rank/dense_rank)
  • 【C语言】(9)分支结构
  • Flink 集成 Debezium Confluent Avro ( format=debezium-avro-confluent )
  • R语言(数据导入,清洗,可视化,特征工程,建模)
  • springboot 整合 Activiti6
  • 微信小程序canvas画布实现直线自由缩放、移动功能
  • Cesium数据加载
  • 【C++历练之路】探秘C++三大利器之一——多态