c++程序设计第3版谭浩强课后答案
建立一个对象数组,内放5个学生的数据(学号、成绩),用指针指向数组首元素,输出第1,3,5个学生的数据。
以下是此题的【c++源代码】
#include <iostream>
using namespace std;
class Student
{public:
Student(int n,float s):num(n),score(s){}
void display();
private:
int num;
float score;
};
void Student::display()
{cout<<num<<" "<<score<<endl;}
int main()
{Student stud[5]={
Student(101,78.5),Student(102,85.5),Student(103,98.5),
Student(104,100.0),Student(105,95.5)};
Student *p=stud;
for(int i=0;i<=2;p=p+2,i++)
p->display();
return 0;
}
本文链接:http://www.wb98.com/cjia/post/cjia_9.4.html
本站文章搜索: