2024/4/28 C++day5

文章详细介绍了如何在C++中定义和使用类,包括Person和其派生类Stu的构造函数、析构函数和拷贝操作。同时展示了如何使用虚函数、继承和派生类重写方法,用于创建一个战斗模拟场景,其中英雄、法师和射手类各有不同的攻击行为。
摘要由CSDN通过智能技术生成

有以下类,完成特殊成员函数

class Person {

string name;

int *age;

}

class Stu:public Person

{

const double score;

}

#include <iostream>  
#include <string>  
using namespace std;
class Person {   
    string name;  
    int *age ;
public:  
    // 无参构造函数  
    Person() : name(""), age(nullptr) {}  
  
    // 有参构造函数  
    Person(string name,int age):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) {  
        if (this != &other) {  
            name = other.name;  
            int* newAge = new int(*other.age);  
            delete age;  
            age = newAge;  
        }  
        return *this;  
    }  
};  
  
class Stu: public Person { 
private:  
    const double score; 
public:  
   
    Stu(string name, int age, double score):Person(name, age), score(score) {}  
    ~Stu(){}
    Stu(const Person &other):Person(other),score(score){}
    Stu &operator=(const Person &other)
    {
        if(this!=&other)
        {
            Person::operator=(other);
            
        }
        return *this;
    }
 
};  
int main() {   
    Person person1("Alice", 25);  
    Person person2 = person1; // 拷贝构造函数被调用  
    person1 = person2; // 拷贝赋值函数被调用  
  
    Stu stu1("Bob", 30, 85.5);  
    return 0;  
}

  1. 尝试写:定义一个全局变量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)
    {cout << "H的有参构造" << endl;}
    virtual void Atk()
    {
        monster-=0;
    }
};
class Master:public Hero
{
    int ap_atk=50;
public:
    Master(){}
    Master(string name,int hp,int attck,int ap_atk):Hero(name,hp,attck),ap_atk(ap_atk)
    {cout << "Master的有参构造" << endl;}
    void Atk()
    {
        monster-=(attck+ap_atk);
    }
};
class Shooter:public Hero
{
    int ac_atk=100;
public:
    Shooter(){}
    Shooter(string name,int hp,int attck):Hero(name,hp,attck)
    {cout << "Shooter的有参构造" << endl;}
    void Atk()  //对父类虚函数的重写
    {
        monster-=(attck+ac_atk);
    }
};
int main()
{
    Master m1("妲己",3000,100,90);
    Shooter s1("小鲁班",3500,120);
    Hero *p1 = &m1,*p2 = &s1;


    int i = 0;
    while(monster>0)
    {
        p1->Atk();
        i++;
        if(monster>0)
        {
            p2->Atk();
            i++;
        }
//        m1.Atk();
//        s1.Atk();
    }

    cout << i << endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值