输入班上10名学生的身高,获得身高最高的学生,要求使用对象数组类型的带参方法来实现

如题所述

#include<iostream>

#include<string.h>

#include <stdlib.h>

const int TOTAL=4;

class student

{

private:

        char *namept;

        int height;

public:

        student(char *s, int hght):height(hght)

        {

                namept=new char[strlen(s)];

                strcpy(namept, s);

                print();

        }

        ~student()

        {

                delete [] namept;

        }

        void print()

        {

                std::cout<<namept<<"  "<<height<<std::endl;

        }

        friend int tallest(student **l, int n)

        {

                int top=l[0]->height;

                int num=0;

                for (int i=1; i< n; ++i)

                        if (l[i]->height>top)

                        {

                                top=l[i]->height;

                                num=i;

                        }

                return num;

        }

};

int main(void)

{

        class student* list[TOTAL];

        list[0]= new student("John", 180);

        list[1]= new student("Adam", 188);

        list[2]= new student("Judy", 168);

        list[3]= new student("Julia", 175);

        int n=tallest(list, TOTAL);

        std::cout<<"the tallest one is ";

        list[n]->print();

        system("pause");

        return 0;

}

 

 

 

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