关于C++的程序中友元函数的使用

来源:百度知道 编辑:UC知道 时间:2024/07/04 09:36:05
设计一个男生类BOY 女生类GIRL 分别存储男生女生的姓名身高体重
设计一个友元函数统计男女生的身高体重 并找出最高的人 (无论男女)
我在线等答案

其实男生女生可以设计成一个类,按照本题需求。不过勉为其难为两个类也可以。
#include <iostream>
#include <vector>

using std::cout;
using std::cin;
using std::endl;

class Person
{
private:
char name[10];
double height;
double weight;
public:
Person():height(0.0),weight(0.0)
{
strcpy(name,"null");
}

Person(const char *name,double h, double w):height(h),weight(w)
{
strncpy(this->name,name,sizeof(name));
}

virtual void setInfo()
{
cout << "Please input the name" << endl;
cin >> name;
cout << "Please intput the height" << endl;
cin >> height;
cout << "Please intput the weight" << endl;
cin >> weight;
}

virtual bool getSex()
{
return true;
}

friend void GetInfo(Person &p,double *sumH,