C + + and objects after class exercise simple example _c language

Source: Internet
Author: User

Set up an object array, put 5 students (student number, score), set up a function of max, with pointers to objects as function parameters, in the Max function to find the highest scores of 5 students, and output its number.

#include <iostream>
using namespace std;

Class Student
{public:
  Student (int=10,int=0);
  int number;
  int score;
  void display ();

student::student (int num,int SCO): Number (num), score (SCO) {
}

void Student::d isplay ()
{
  cout <<number<< "" <<score<<endl;
}

void Max (Student *p)
{
  int maxi=p[0].score;
  int temp=0;
  for (int i=1;i<5;i++)
  if (P[i].score>maxi)
  {
  maxi=p[i].score;
  temp=i;
  }
  cout<<p[temp].number<< "" <<maxi<<endl;
}

int main ()
{
  Student stu[5]={
student{1001,90},
student{1002,80},
student{1003,99},
student{1004,84},
student{1005,85}
};
  Student *p=&stu[0];
   Max (p);
  return 0;
}

Above this C + + and object after class exercises simple example is small series to share all the content, hope to give you a reference, also hope that we support cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.