C++ student class example

Define a student class and design private data members:

age int age;

name string name;

Public member functions:

Initialization function with parameters Input(int a, string str);

Get data member function Output();

Define an object array with 3 elements in the main function and input them separately, and then output the information of the object array.

#include<iostream>
using namespace std;
class Student
{
public:
 int age;
 string name;

 int Input(int a, string str);
 int Output();
};
int Student::Input(int a, string str)
{
  a=age;
  str=name;
  return 0;
}

int Student::Output()
{
    //cout<<"输入学生的年龄和姓名"<<endl;
    cout<<age<<","<<name<<endl;
    return 0;
}
int main()
{
  int i;
  Student a[3];
  cout<<"输入学生的年龄和姓名"<<endl;
   for(i=0;i<3;i++)
{
  cin>>a[i].age>>a[i].name;
  a[i].Input(a[i].age,a[i].name);
}
   cout<<"输出信息:"<<endl;
  for(i=0;i<3;i++)
{

  a[i].Output();
}
  return 0;
}

Guess you like

Origin blog.csdn.net/y0205yang/article/details/130208160