#include <iostream>
using namespace std;
class Person
{
public:
string name;
int *age;
Person():age(new int){}
Person(string name,int *age):name(name),age(new int(*age)){}
Person(const Person &other):name(other.name),age(new int(*(other.age))){}
Person &operator=(const Person &other)
{
this->name = other.name;
*(this->age) = *(other.age);
}
~Person()
{
delete age;
}
};
class Stu:public Person
{
public:
const double score;
//透传函数
Stu():Person(),score(10){}
Stu(const double score,string name,int *age):score(score),Person(name,age){}
//继承构造
using Person::Person;
// Stu():Person(),score(20){}
// Stu(string name,int *age):Person(name,age),score(10){}
//委托构造
Stu(const double score):Person(),score(score){}
//拷贝构造
Stu(const Stu &other):Person(other),score(other.score){}
//拷贝赋值
Stu &operator=(const Stu &other)
{
Person::operator=(other);
}
//析构函数
~Stu(){}
};
int main()
{
cout << "Hello World!" << endl;
return 0;
}
打怪兽
#include <iostream>
using namespace std;
int monster = 10000;
class Hero
{
protected:
string name;
int HP;
int attack;
public:
Hero(){}
Hero(string name,int HP,int attack):name(name),HP(HP),attack(attack){}
void virtual Atk()
{
monster -=attack;
}
};
class Wizard:public Hero
{
int ap_atk;
public:
Wizard(string name,int HP,int attack):Hero(name,HP,attack),ap_atk(attack)
{
cout << "法师已加入" << endl;
cout << " 昵称" << Wizard::name << endl;
cout << " 血量" << Wizard::HP << endl;
cout << " 法术伤害" << Wizard::ap_atk << endl;
}
void Atk()
{
monster -=ap_atk;
}
};
class Archer:public Hero
{
int ad_atk;
public:
Archer(string name,int HP,int attack):Hero(name,HP,attack),ad_atk(attack)
{
cout << "射手已加入" << endl;
cout << " 昵称" << Archer::name << endl;
cout << " 血量" << Archer::HP << endl;
cout << " 远程伤害" << Archer::ad_atk << endl;
}
void Atk()
{
monster -=ad_atk;
}
};
int main()
{
Wizard w1("BB",3000,200);
Archer a1("WM",3500,250);
putchar(10);
int cout_w=0,cout_a=0;
while(1)
{
if(monster<=0)
{
cout << "怪物已被击败" << endl;
break;
}
w1.Atk();cout_w++;
a1.Atk();cout_a++;
}
printf("法师攻击了 %d 次\n",cout_w);
printf("射手攻击了 %d 次\n",cout_a);
return 0;
}