2024.4.28

文章详细描述了如何在C++中使用类和构造函数创建`Person`和派生类`Stu`,以及`Hero`、`Master`和`Shooter`类的定义,涉及特殊的成员函数如构造函数、析构函数和虚函数的实现。通过实例展示了如何通过继承和重写实现不同角色(如战士、法师和射手)的攻击逻辑,并在主函数中进行调用。
摘要由CSDN通过智能技术生成

  1. 有以下类,完成特殊成员函数
#include <iostream>

using namespace std;
class Person{
    string name;
    int* age;
public:
    Person():name("zhangsan"),age(new int(18)){}
    Person(string name,int* age):name(name),age(new int(*age)){}
    ~Person(){
        delete age;
    }
    Person(const Person &other):name(other.name){
        *(this->age)=*(other.age);
    }
    Person &operator=(const Person &other){
        this->name=other.name;
        *(this->age)=*(other.age);
        return *this;
    }
    void show();
};
class Stu:public Person{
    const double score;
public:
    Stu():score(60){}
    Stu(string name,int* age,double score):Person(name,age),score(score){}
    ~Stu(){}
    Stu(const Stu &other):Person(other),score(other.score){}
    Stu &operator=(const Stu &other){
        Person::operator=(other);
        //this->score=other.score;
        return *this;
    }
    void show();
};
void Person::show(){
    cout << "name:" << name << endl;
    cout << "age:" << *age << endl;
}
void Stu::show(){
    Person::show();
    cout << "score:" << score << endl;
}
int main()
{
    int age=20;
    Stu s2;
    Stu s1("lisi",&age,99);
    s1.show();
    s2.show();
    return 0;
}

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

#include <iostream>
int monster=10000;
using namespace std;
class Hero{
protected:
    string name;
    int hp;
    int attack;
    Hero():name("无名氏"),hp(100),attack(10){}
    Hero(string name,int hp,int attack):name(name),hp(hp),attack(attack){}
    virtual void Atk(){
        monster-=0;
    }
};
class Master:public Hero{
    int ap_ack;
public:
    Master(string name,int hp,int attack,int ap_ack):Hero(name,hp,attack),ap_ack(ap_ack){}
    void Atk(){
        monster-=(attack+ap_ack);
    }
};
class Shooter:public Hero{
    int ad_ack;
public:
    Shooter(string name,int hp,int attack,int ad_ack):Hero(name,hp,attack),ad_ack(ad_ack){}
    void Atk(){
        monster-=(attack+ad_ack);
    }
};

int main()
{
    string name,n;
    int hp,attack,ap_ack,ad_ack;
    int secend=0;
    cout << "请选择英雄" << endl;
    cin >> name;
    cout << "请输入英雄类型" << endl;
    cin >> n;
    cout << "请输入英雄属性" << endl;
    if(n=="法师"){
        cin >> hp >> attack >> ap_ack;

        Master m1(name,hp,attack,ap_ack);
        while(monster>0){
            m1.Atk();
            secend++;
        }
    }else if(n=="射手"){
        cin >> hp >> attack >> ad_ack;
        Shooter s1(name,hp,attack,ad_ack);
        while(monster>0){
            s1.Atk();
            secend++;
        }
    }
    cout << name << "  " << secend << "秒击败怪物" << endl;

    return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值