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

C++(day4)

一、思维导图

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

#include <iostream>using namespace std;class Per
{
private:string name;int age;double *high;double *weigh;
public:Per(){cout<<"Per::无参构造函数"<<endl;}Per(string name,int age,double high,double weigh):name(name),age(age),high(new double(high)),weigh(new double(weigh)){cout<<"Per::构造函数"<<endl;}~Per(){delete high;delete weigh;cout<<"Per::析构函数"<<endl;};Per(const Per &other):name(other.name),age(other.age),high(new double(*(other.high))),weigh(new double(*(other.weigh))){cout<<"Per::拷贝构造函数"<<endl;}Per &operator=(const Per &other){if(this != &other){name=other.name;age=other.age;high=new double(*(other.high));weigh=new double(*(other.weigh));}cout<<"Per::拷贝赋值函数"<<endl;return *this;}void show(){cout<<"姓名:"<<name<<endl;cout<<"年龄:"<<age<<endl;cout<<"身高"<<*high<<endl;cout<<"体重"<<*weigh<<endl;}
};class Stu
{
private:double score;Per p1;
public:Stu(){cout<<"Stu::无参构造函数"<<endl;}Stu(double score,string name,int age,double high,double weigh):score(score),p1(name,age,high,weigh){cout<<"Stu::构造函数"<<endl;}~Stu(){cout<<"Stu::析构函数"<<endl;}Stu(const Stu &other):score(other.score),p1(other.p1){cout<<"Stu::拷贝构造函数"<<endl;}Stu &operator=(const Stu &other){if(this != &other){score=other.score;p1=other.p1;}cout<<"Stu::拷贝赋值函数"<<endl;return *this;}void show(){cout<<"*学生信息*"<<endl;p1.show();cout<<"成绩:"<<score<<endl;}
};int main()
{
//    Per p1;
//    Per p2("Beidou",17,173,122);
//    Per p3(p2);
//    p1=p2;
//    p2.show();Stu s1;Stu s2(0,"Univin",16,170,112);Stu s3(s2);s1=s3;s2.show();return 0;
}

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

相关文章:

  • GESP2025年9月C++三级编程题日历制作题解参考
  • 网站建设与管理实训心得怎么写2023热点新闻事件
  • 做网站很火的APP来个网站吧好人一生平安百度贴吧
  • 花未全开月未圆:布隆过滤器的留白美学
  • 网站安全措施优秀个人网页
  • Flutter完整开发指南 | FlutterDart – The Complete Guide
  • 营销型企业网站系统模板下载建设网站前期准备工作
  • Android车机系统上电获取历史定位信息异常分析
  • 建设银行征信中心官方网站企业网站建设与推广多少钱
  • 如何利用开源库和安全芯片设计fido令牌
  • 全国信息网查询平台知名seo公司
  • 加强网站信息内容建设的意见最新网站制作公司哪个好
  • 橙域名网站网站开发天津网站开发
  • 学校网站推广方案凡科网h5
  • 网站建设xiu021网站域名备案注册证书查询
  • 建设网站政策风险百度推广优化师是什么
  • 深入理解MySQL_1 MySQL系统架构
  • C++之友元函数与前向引用
  • 01_系统架构设计
  • 解决 jenkins 用户 SSH 连接目标服务器时的 Permission denied 问题
  • 【html】canvas实现一个时钟
  • 怎么用支付宝做发卡网站优质的seo网站排名优化软件
  • 论文阅读四-第三章
  • 工程网站模板wordpress查看
  • openai代码研读:OpenAI Python SDK 中 AsyncOpenAI 类的定义
  • 广东公诚通信建设监理有限公司网站建设内容管理网站的目的
  • 乐平市网站建设企业运营数据分析报告
  • 一张草稿纸
  • GSPO论文阅读
  • 做设计去哪个网站找素材怎么推广效果好呢网站怎么做推广