C++-5

embedded/2024/9/23 10:16:52/

 完成特殊成员函数

#include <iostream>using namespace std;class Person
{string name;int *age;
public://构造,析构,拷贝构造,拷贝赋值Person():age(new int ){}Person(int *age,string name):name(name),age(new int (*(age))){}~Person(){delete age;}Person(const Person& other):name(other.name),age(new int (*(other.age))){}Person& operator=(const Person& other){*(this->age)=*(other.age);this->name = other.name;return *this;}};class Stu:public Person
{const double score;
public://构造,析构,拷贝构造,拷贝赋值Stu():score(12){}Stu(double score):score(score){}Stu(double score,string name,int *age):Person(age,name),score(score){}~Stu(){}Stu(const Stu& other):Person(other),score(other.score){}Stu& operator=(const Stu& other){Person::operator=(other);return *this;}};

 定义一个全局变量int monster = 10000;定义一个英雄类Hero,受保护的属性,string name,int hp,int attck,写一个无参构造、有参构造,类中有虚函数:void Atk(){monster-=0;};法师类,公有继承自英雄类,私有属性:int ap_ack;写有参,重写父类中的虚函数,射手类,公有继承自英雄类,私有属性:int ad_ack;写有参构造,重写父类中的虚函数,主函数内完成调用,判断怪物何时被杀死。

#include <iostream>using namespace std;
int monster = 10000;
class Hero
{
protected:string name;int hp;int attck;
public:Hero(){}Hero(string name,int hp,int attck):name(name),hp(hp),attck(attck){}virtual void Atk(){monster-=0;}
};
class Master:public Hero
{int ap_ack;
public:Master(int ap_ack,string name,int hp,int attck):Hero(name,hp,attck),ap_ack(ap_ack){}void Atk(){monster-=ap_ack+attck;}
};
class Shooter:public Hero
{int ad_ack;
public:Shooter(int ad_ack,string name,int hp,int attck):Hero(name,hp,attck),ad_ack(ad_ack){}void Atk(){monster-=ad_ack+attck;}
};
int main()
{Shooter s1(500,"Draven",1500,500);Master m1(800,"ali",1000,100);int i=0;while(monster>=0){s1.Atk();if(monster<=0){cout << "monster还剩" << monster <<endl;break;}m1.Atk();cout << "monster还剩" << monster <<endl;i++;}cout << "time is taken "<< i << " s " <<  endl;return 0;
}

 


http://www.ppmy.cn/embedded/27451.html

相关文章

AI大模型探索之路-训练篇6:大语言模型预训练数据准备-预处理

系列篇章&#x1f4a5; AI大模型探索之路-训练篇1&#xff1a;大语言模型微调基础认知 AI大模型探索之路-训练篇2&#xff1a;大语言模型预训练基础认知 AI大模型探索之路-训练篇3&#xff1a;大语言模型全景解读 AI大模型探索之路-训练篇4&#xff1a;大语言模型训练数据集概…

字节跳动(社招)三面算法原题

TikTok 喘息 继上月通过强制剥离 TikTok 法案后&#xff0c;美国众议院在当地时间 20 日下午以 360 票赞成 58 票反对通过了新的法案&#xff1a;剥离 TikTok 的期限由生效后 165 天调整至 270 天之内&#xff0c;即今年 11 月的美国总统大选后。 之前我们讲过&#xff0c;TikT…

(四)小程序学习笔记——自定义组件

1、组件注册——usingComponents &#xff08;1&#xff09;全局注册&#xff1a;在app.json文件中配置 usingComponents进行注册&#xff0c;注册后可以在任意页面使用。 &#xff08;2&#xff09;局部注册&#xff0c;在页面的json文件中配置suingComponents进行注册&#…

视觉语言模型详解

视觉语言模型可以同时从图像和文本中学习&#xff0c;因此可用于视觉问答、图像描述等多种任务。本文&#xff0c;我们将带大家一览视觉语言模型领域: 作个概述、了解其工作原理、搞清楚如何找到真命天“模”、如何对其进行推理以及如何使用最新版的 trl 轻松对其进行微调。 什…

CHATGPT对写作业的好处

原文&#xff1a;chatGPT对写作业的好处 - 夸智网 本文目录一览1、chatGPT对写作业的好处2、chatGPT批改作业3、chatGPT完成作业4、chatGPT写作业水平超过大学生5、美国大学生用chatGPT写作业大家好&#xff0c;今天来为您分享chatGPT对写作业的好处的一些知识&#xff0c;本文…

【面经】2024春招-后端开发工程师3(运营,安全,测开)

主要是面试前准备的相关材料与网络上的面经&#xff0c;个人不透露面试题目哈&#xff5e; 文章目录 岗位与面经基础1&#xff1a;测试 & 测开 岗位与面经 1、腾讯云CSIG 星星海实验室2、小鹏汽车 【24届校招】后端研发工程师&#xff08;销服方向&#xff09; 广州正式研…

kubectl_入门_Pod控制器

Pod控制器 在k8s中&#xff0c;按照pod的创建方式可以将其分为两类 自主式pod&#xff1a;k8s直接创建出来的pod&#xff0c;这种pod删除后就没有了&#xff0c;也不会重建控制器创建的pod&#xff1a;通过控制器创建的pod&#xff0c;这种pod删除了之后还会自动重建 1. 什么…

CSS面试题

1.HTML5新增了哪些内容 语义化更好的内容标签(header,footer,main,nav,aside,article,section)音频 ,视频标签(audio, video)画布(canvas)表单控件 calendar , date , time , email , url , search , tel , file , number地理位置API(geolocation)拖拽释放API(Drap and drop)W…