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;
}

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值