C++ day5

#include <iostream>
using namespace std;
class Person
{
    string name;
    int *age;
public:
    Person():name("zhangsan"),age(new int(18)){cout << "Person的无参构造" << endl;}
    Person(string name,int age):name("zhangsan"),age(new int (18)){cout << "Person有参构造" << endl;}
    void show();
    ~Person(){delete age;}
    Person(const Person &other):name(other.name),age(new int(*(other.age))){cout << "Person的拷贝构造" << endl;}
    Person &operator=(const Person &other)
    {
        if(this!=&other)
        {
            this->name=other.name;
            *(this->age)=*(other.age);
        }
        cout << "Person的拷贝赋值函数" << endl;
        return *this;
    }
};
class Stu:public Person
{
    const double score;
public:
    Stu():Person(),score(90){cout << "Stu的无参构造" << endl;}
    Stu(const double score):Person(),score(90){cout <<"Stu的有参构造"  << endl;}
    ~Stu(){}
    Stu(const Stu &other):Person(other),score(other.score){cout << "Stu的拷贝构造" << endl;}
    Stu &operator=(const Stu &other)
    {
        if(this!=&other)
        {
            Person::operator=(other);
        }
        cout << "Stu的拷贝赋值函数" << endl;
        return *this;
    }
    void show();
};
void Person::show()
{
    cout << "P中的name:" << name << endl;
    cout << "P中的age:" << age << endl;
    cout << "P中的age:" << *age << endl;
}
void Stu::show()
{
    cout << "S中的score:" <<score << endl;
}
int main()
{
    Stu s1;
    Stu s2;
    s1 = s2;
    s1.Person::show();
    s2.Person::show();
    s1.show();
    s2.show();
    return 0;
}

#include <iostream>

using namespace std;

int Monster = 10000;

//英雄类
class Hero
{
protected:
    string name;
    int hp;
    int attck;
public:
    //构造函数
    Hero(){cout << "Hero无参构造" << endl;}
    Hero(string name,int hp,int attck):name(name),hp(hp),attck(attck){cout << "Hero有参构造" << endl;}
    //虚函数
    virtual void Atk(){Monster -= 0;}
};
//法师类
class Master: public Hero
{
    int ap_ack = 200;
public:
    //构造函数
    Master(string name,int hp,int attck):Hero(name,hp,attck){cout << "Master有参构造" << endl;}
    void Atk()
    {
        Monster -= (attck + ap_ack);
    }
};

//射手类
class Shooter:public Hero
{
    int ad_ack = 100;
public:
    //构造函数
    Shooter(string name,int hp,int attck):Hero(name,hp,attck){cout << "Shooter有参构造" << endl;}
    void Atk()
    {
        Monster -=(attck + ad_ack);
    }
};
int main()
{
    int num=0;
    Master m("法师",100,100);
    Shooter s("射手",100,100);
    while(Monster>=0)
    {
        m.Atk();
        num++;
        s.Atk();
    }
    cout << "Monster die" << endl;
    cout << "法师攻击次数:" << num <<endl;
    cout << "射手攻击次数:" << num <<endl;
    return 0;
}

 

相关推荐

  1. 如何来整合 CDI-Unit 和 JUnit 5

    2024-04-29 12:48:03       8 阅读
  2. MSc CDA Take-Home

    2024-04-29 12:48:03       33 阅读
  3. CDA一级备考策略分享

    2024-04-29 12:48:03       11 阅读
  4. CDA-LevelⅡ【考题整理-带答案】

    2024-04-29 12:48:03       21 阅读
  5. Spring Data访问Elasticsearch----CDI集成

    2024-04-29 12:48:03       18 阅读
  6. CDA Level Ⅰ 2023认证考试大纲

    2024-04-29 12:48:03       15 阅读
  7. web server apache tomcat11-33-CDI

    2024-04-29 12:48:03       10 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-29 12:48:03       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-29 12:48:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-29 12:48:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-29 12:48:03       18 阅读

热门阅读

  1. C++中的时间相关处理

    2024-04-29 12:48:03       14 阅读
  2. python基础知识

    2024-04-29 12:48:03       12 阅读
  3. Unity坐标相关——坐标系,单位

    2024-04-29 12:48:03       13 阅读
  4. Node.js 的 fs 模块分析及其应用

    2024-04-29 12:48:03       16 阅读
  5. 使用动态ip上网稳定吗?

    2024-04-29 12:48:03       13 阅读
  6. 安装k8s

    2024-04-29 12:48:03       12 阅读
  7. web server apache tomcat11-20-connectors 连接器

    2024-04-29 12:48:03       14 阅读
  8. Odoo用浏览器与企业版客户端的区别

    2024-04-29 12:48:03       12 阅读
  9. ssh连接自动断开的几种可能

    2024-04-29 12:48:03       17 阅读
  10. linux 提权总结_linux提权

    2024-04-29 12:48:03       14 阅读
  11. 阿赵Json工具AzhaoJson的Lua版本

    2024-04-29 12:48:03       11 阅读
  12. 【Spark】读取本地文件

    2024-04-29 12:48:03       9 阅读
  13. rust语言tokio库spawn, blocking_spawn等的使用

    2024-04-29 12:48:03       20 阅读