1. 程式人生 > >類:繼承-1

類:繼承-1

#include <iostream>
//類的三大特點:封裝,繼承,多型
class Player
{
public:
	int hp;
};
class Moster1
{
public:
	int id;
	int hp;
	int mp;
	int attack;
	void Attack(Player* player)
	{
		player->hp -= 10;
	}
};

class Moster2
{
public:
	int id;
	int hp;
	int mp;
	int attack;
	int skill;//比Moster1多的資料
	void Attack(Player* player)
	{
		player->hp -= 10;
	}
	void AttackSkill(Player* player)
	{
		player->hp -= 20;
	}
};


class Moster3
{
public:
	int id;
	int hp;
	int mp;
	int attack;
	int baoji;//暴擊
	void Attack(Player* player)
	{
		player->hp -= 10;
	}
	void AttackBaoji(Player* player)
	{
		player->hp -= 100;
	}
};



void main()
{
	

	system("pause");

}

 

#include <iostream>
//類的三大特點:封裝,繼承,多型
class Player
{
public:
	int hp;
};

//基類:
class Moster1
{
public:
	int id;
	int hp;
	int mp;
	int attack;
	void Attack(Player* player)
	{
		player->hp -= 10;
	}
};
//Moster2公有繼承自Moster1
//Moster1是Moster2的父類
//Moster2是Moster1的子類
class Moster2 : public Moster1
{
public:
	int skill;//比Moster1多的資料
	void AttackSkill(Player* player)
	{
		player->hp -= 20;
	}
};

//Moster3公有繼承自Moster1
//Moster1是Moster3的父類
//Moster3是Moster1的子類
class Moster3 : public Moster1
{
public:
	int baoji;//暴擊
	void AttackBaoji(Player* player)
	{
		player->hp -= 100;
	}
};

void main()
{
	
	std::cout<<sizeof(Moster1)<<std::endl;//16
	std::cout<<sizeof(Moster2)<<std::endl;//20
	std::cout<<sizeof(Moster3)<<std::endl;//20
	system("pause");

}