1. 程式人生 > >編寫設計一個People(人)類。該類的資料成員有年齡(age)、身高(height)、體重(weight)和人數(num),其中人數為靜態資料成員

編寫設計一個People(人)類。該類的資料成員有年齡(age)、身高(height)、體重(weight)和人數(num),其中人數為靜態資料成員

成員函式有建構函式(People)、進食(Eatting)、運動(Sporting)、睡眠(Sleeping)、顯示(Show)和顯示人數(ShowNum)。其中建構函式由已知引數年齡(a)、身高(h)和體重(w)構造物件,進食函式使體重加1,運動函式使身高加1,睡眠函式使年齡、身高、體重各加1,顯示函式用於顯示人的年齡、身高、體重,顯示人數函式為靜態成員函式,用於顯示人的個數。假設年齡的單位為歲,身高的單位為釐米,體重的單位為市斤,要求所有資料成員為protected訪問許可權,所有成員函式為public訪問許可權,在主函式中通過物件直接訪問類的所有成員函式。

#include"iostream"
using namespace std;
class person{
protected:
	int age;
	int height;
	double weight;
	static int num;
public:
	person(int a,int h,double w):age(a),height(h),weight(w){ num++; }
	void Eatting(){ weight++; }
	void Sproting(){ height++; }
	void Sleeping(){
		age++;
		weight++;
		height++;
	}
	void Show(){
		cout << "age: " << "height:  " << "weight:   "  << endl ;
		cout << age << "   " << height << "       " << weight << endl ;
	}
	static int number(){
		return num;
	}
};

int person::num=0;

void main()
{
	person a(10,30,140),b(20,60,170);
	a.Eatting();
	a.Sproting();
	a.Sleeping();
	a.Show();
	b.Eatting();
	b.Sproting();
	b.Sleeping();
	b.Show();
	cout << "person:" << endl ;
	cout << person::number() << endl ;
}