c++ 做一个类数组 各种错误

/*定义一个PERSON类,数据成员包括姓名、年龄、身高和体重,函数成员包括构造函数、复制构造函数、析构函数及其他必要的成员函数。

要求定义三个私有static数据成员,能够获取到PERSON类对象的平均年龄、平均身高和平均体重。

编写主函数进行结果验证。*/
#include<iostream>
#include<string>
using namespace std;
#define MAX_SIZE 10
class Person{
char name[30];
double age,height,weight;

private:
static double AveAge,AveHeight,AveWeight;
void show();
void get();
void aveage();
void aveheight();
void aveweight();
};
Person::Person(char *name,int age,double height,double weight){
strcpy(this.name,name);
this.age = age;
this.height = height;
this.weight = weight;
}

void Person::get( Person *p )
{
//cout<<"请输入姓名 年龄 身高 体重"<<endl;

cin>>p.name;
cin>>p.age;
cin>>p.height;
cin>>p.weight;
}
/*void show(){

}*/
void Person::aveage( )
{
double sum=0;
for(int i=0;i<MAX_SIZE;i++)
{
sum+=member[i].age;
}
AveAge=sum/MAX_SIZE;
cout<<"aveage="<<AveAge<<endl;
}

void Person::aveheight( )
{
double sum=0;
for(int i=0;i<MAX_SIZE;i++)
{
sum+=member[i].height;
}
AveHeight=sum/MAX_SIZE;
cout<<"aveheight="<<AveHeight<<endl;
}

void Person::aveweight( )
{
double sum=0;
for(int i=0;i<MAX_SIZE;i++)
{
sum+=member[i].weight;
}
AveWeight=sum/MAX_SIZE;
cout<<"aveweight="<<AveWeight<<endl;
}
void main() {
Person a;
Person member[MAX_SIZE];
cout<<"Please input the count of people"<<endl;
int num;
cin>>num;
for(int i=0;i<num;i++)
{
a.get(*(member+i));
}
a.aveage();
a.aveheight();
a.aveweight();
}
}

#include <iostream>
#include <string>

using namespace std;

#define MAX_SIZE 10

class Person {
private :
char m_name[30];
int m_age;
int m_height;
int m_weight;
static int numOfObj;   // 类对象数量
static int sumOfAge;   // 年龄和
static int sumOfHeight;// 身高和
static int sumOfWeight;// 体重和
public :
Person() { }
Person(char name[],int age,int height,int weight);
void get();
void show();
static int getObjs() { return numOfObj; }
static double getAvrAge() { return 1.0 * sumOfAge / numOfObj; }
static double getAvrHeight() { return 1.0 * sumOfHeight / numOfObj; }
static double getAvrWeight() { return 1.0 * sumOfWeight / numOfObj; }
};

Person::Person(char name[],int age,int height,int weight) {
strcpy(m_name,name);
m_age = age;
m_height = height;
m_weight = weight;
++numOfObj;
sumOfAge += age;
sumOfHeight += height;
sumOfWeight += weight;
}

void Person::get() {
cout << "请输入姓名 年龄 身高 体重\n";
cin >> m_name;
cin >> m_age;
cin >> m_height;
cin >> m_weight;
++numOfObj;
sumOfAge += m_age;
sumOfHeight += m_height;
sumOfWeight += m_weight;
}

void Person::show() {
cout << m_name << "," << m_age << "," << m_height << "," << m_weight << endl;
}

// 在这里给类的每个静态数据成员初始化
int Person::numOfObj = 0;
int Person::sumOfAge = 0;
int Person::sumOfHeight = 0;
int Person::sumOfWeight = 0;

int main() {
Person member[MAX_SIZE]; // 调用默认构造函数声明一个数组
cout << "Please input the count of people\n";
int num;
cin >> num;
for(int i = 0; i < num && i < MAX_SIZE;i++)
member[i].get();
for(i = 0; i < num && i < MAX_SIZE; ++i)
member[i].show();
cout << "Average age : " << Person::getAvrAge() << endl;
cout << "Average height : " << Person::getAvrHeight() << endl;
cout << "Average weight : " << Person::getAvrWeight() << endl;
return 0;
}

温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答