面向对象程序设计上机练习八(对象数组)
Time Limit: 1000MS
Memory Limit: 65536KB
Problem Description
利用类对象数组完成N个学生数据(学号是字符串类型、成绩是整型)的输入、输出。
Input
输入有N+1行:
第一行的整数N表示学生数目;
以下N行是N个学生的数据,每行中第一个是表示学号的字符串,第二个是表示学生成绩的整数。
第一行的整数N表示学生数目;
以下N行是N个学生的数据,每行中第一个是表示学号的字符串,第二个是表示学生成绩的整数。
Output
输出N个学生数据。每个学生的数据占一行。
Example Input
5 01 89 02 78 03 56 04 92 05 76
Example Output
01 89 02 78 03 56 04 92 05 76
#include <iostream>
#include <string.h>
using namespace std;
class Point
{
private:
char id[20];
int so;
public:
void get(char ch[],int s);
void put();
};
void Point::get(char ch[],int s)
{
strcpy(id,ch);
so=s;
}
void Point::put()
{
cout<<id<<" "<<so<<endl;
}
int main()
{
Point stu[100];
char ch[20];
int s,n,i;
cin>>n;
for(i=0;i<n;i++)
{
cin>>ch>>s;
stu[i].get(ch,s);
}
for(i=0;i<n;i++)
stu[i].put();
return 0;
}
#include <iostream>
#include <string.h>
using namespace std;
class Point
{
private:
char id[20];
int so;
public:
void get(char ch[],int s);
void put();
};
void Point::get(char ch[],int s)
{
strcpy(id,ch);
so=s;
}
void Point::put()
{
cout<<id<<" "<<so<<endl;
}
int main()
{
Point stu[100];
char ch[20];
int s,n,i;
cin>>n;
for(i=0;i<n;i++)
{
cin>>ch>>s;
stu[i].get(ch,s);
}
for(i=0;i<n;i++)
stu[i].put();
return 0;
}
本文介绍了一个使用对象数组处理学生数据的程序设计案例。通过定义一个包含学号和成绩的类,实现了学生信息的输入和输出功能。适用于初学者理解面向对象编程中的对象数组概念。
543

被折叠的 条评论
为什么被折叠?



