C++作业

发布于:2024-04-29 ⋅ 阅读:(26) ⋅ 点赞:(0)

1、思维导图

#include <iostream>

using namespace std;
class Person
{
    string name;
    int *age;
public:
    Person():name("zhao"),age(new int(82))
    {
        cout<<"Person的无参构造"<<endl;
    }
    Person(string name,int age):name(name),age(new int(age))
    {
        cout<<"Person的有参构造"<<endl;
    }
        void show();
        ~Person()
        {
            delete age;
            cout<<"Person的析构函数"<<endl;
        }
        Person(const Person &other):age(new int(*(other.age)))
        {
            cout<<"Father的构造拷贝"<<endl;
            this->name=other.name;
        }
        Person &operator=(const Person &other)
        {
            if(this!=&other)
            {
                *(this->age)=*(other.age);
                this->name=other.name;
            }
            cout<<"Person中的拷贝赋值函数"<<endl;
            return *this;
        }
};
class Stu:public Person
{
    const double score;
public:
    Stu():Person(),score(82.13)
    {
        cout<<"Stu的无参构造"<<endl;
    }
    ~Stu()
    {
        cout<<"Stu的析构函数"<<endl;
    }
    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<<name<<endl;
        cout<<age<<endl;
    }
    void Stu::show()
    {
        cout<<score<<endl;
    }
int main()
{
    Stu s1;
    Stu s2=s1;
    Stu s3;
    Stu s4;
    s3=s4;
    s1.Person::show();
    s2.Person::show();
    s1.show();
    s2.show();
    s3.Person::show();
    s4.Person::show();
    s3.show();
    s4.show();
    return 0;
}

#include <iostream>

using namespace std;
int monster=10000;
class Hero
{
protected:
    string name;
    int hp;
    int attck;
public:
    Hero():name("godman"),hp(300),attck(55)
    {

    }
    Hero(string name,int hp,int attck):name(name),hp(hp),attck(attck)
    {

    }
    virtual void Atk()
    {
        monster-=0;
    }
};
class fashi:public Hero
{
    int ap_ack;
public:
    fashi(int ap_ack,string name,int hp,int attck):ap_ack(ap_ack),Hero(name,hp,attck)
    {

    }
    void Atk()
    {
        monster-=(ap_ack+attck);
    }
};
class sheshou:public Hero
{
    int ad_ack;
public:
    sheshou(int ad_ack,string name,int hp,int attck):ad_ack(ad_ack),Hero(name,hp,attck)
    {

    }
    void Atk()
    {
        monster-=(ad_ack+attck);
    }
};
int main()
{
    string name,n;
       int ap_ack,ad_ack,hp,attack;
       int s=0;
       cout << "请给英雄冠名" << endl;
       cin >> name;
       cout << "输入英雄类型" << endl;
       cin >> n;
       if(n=="战斗法师"){
           cout<<"请设置战斗法师数值"<<endl;
           cout<<"请设置血量"<<endl;
           cin >> hp;
           cout<<"请设置攻击力"<<endl;
           cin >> attack ;
           cout<<"请设置附加属性"<<endl;
           cin >> ap_ack;

           fashi m1(ap_ack,name,hp,attack);
           while(monster>0){
               m1.Atk();
               s++;
           }
       }else if(n=="近战弓兵"){
           cout<<"请设置近战弓兵数值"<<endl;
           cout<<"请设置血量"<<endl;
           cin >> hp;
           cout<<"请设置攻击力"<<endl;
           cin >> attack ;
           cout<<"请设置附加属性"<<endl;
           cin >> ad_ack;
           sheshou s1(ad_ack,name,hp,attack);
           while(monster>0){
               s1.Atk();
               s++;
           }
       }
       cout << name << "  " << s << "秒刷一遍怪" << endl;
    return 0;
}


网站公告

今日签到

点亮在社区的每一天
去签到