- 有以下类,完成特殊成员函数
#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;
}