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