C++ 设计People类-1

题目描述

该类的数据成员有Name,Age,Height,Weight,成员函数有构造函数People,进食Eating,运动Sporting,显示Show,
其中构造函数用已知参数姓名nm、年龄a、身高h、体重w构造对象,进食函数使体重加1,运动函数使身高加1,显示函数用于显示姓名、年龄、身高、体重。
要求数据成员都是private,成员函数都是public访问权限。

输入描述

首行输入姓名 年龄 身高 体重,用空格隔开,第二行输入一天的进食次数和运动次数

输出描述

显示一天后这个人的所有信息

输入样例

张三 19 175 70
3 1

输出样例

姓名 张三
年龄 19
身高 176
体重 73
#include <iostream>

using namespace std;


// 定义一个类; class 为创建类的关键字 
class Person{ //定义一个类名
	private: //访问控制符  访问权限  private 修饰的属性,只能在该类中被访问 
		char name[64];    //成员变量
		int age;
		int height;
		int weight;
		

	public: // 公共权限,类外部可被函数或其它类访问。
		Person();
		Person(char* nm, int a, int h, int w);
		void sporting(int count);
		void eating(int count);
		void show();
	
	
}; 

// 成员函数定义,包括构造函数
Person::Person(void)
{
    
}

Person::Person(char* nm, int a, int h, int w)
{
	strcpy(name, nm); 
    age = a;
    height = h;
    weight = w;
}


void Person::eating(int count)
{
   weight = weight + count;
}

void Person::sporting(int count)
{
   height = height + count;
}

void Person::show(){
   cout << "姓名 " << name << endl;
   cout << "年龄 " << age << endl;
   cout << "身高 " << height << endl;
   cout << "体重 " << weight << endl;
}




int main(void){
	
	char name[64];    //成员变量
	int age;
	int height;
	int weight;
	
	int eating = 0;
	int sporting = 0;
	
	
	cin >> name >> age >> height >> weight;
	Person person(name, age, height, weight);  // 实例化创建一个 Person 对象 
	
	cin >> eating >> sporting;
	
	person.sporting(sporting);
	person.eating(eating);
	person.show();
}

版权声明:本文为qq_44989881原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。