目录
题目
【对象数组和对象指针的使用】建立一个对象数组,内放5个学生数据(学号(字符串类型)、成绩(整型)),用指针指向数组首元素,输出第1,3,5个学生的数据。
源码
#include<iostream>
#include<string>
using namespace std;
class Student
{
private:
string number_;
int score_;
public:
Student(string number,int score):number_(number),score_(score){}
void display() { cout << number_ << ' ' << score_ << endl; }
};
int main()
{
Student stu[5] = {Student{"0001", 95}, {"0002", 90}, {"0003", 85}, {"0004", 80}, {"0005", 75}};
Student *s = stu;
s->display();
s += 2;
s->display();
s += 2;
s->display();
return 0;
}