习题 9.4 建立一个对象数组,内放5个学生的数据(学号、成绩),用指针指向数组首元素,输出第1,3,5个学生的数据。

爱被打了一巴掌 2022-05-16 03:36 164阅读 0赞

C++程序设计(第三版) 谭浩强 习题9.4 个人设计

习题 9.4 建立一个对象数组,内放5个学生的数据(学号、成绩),用指针指向数组首元素,输出第1,3,5个学生的数据。

代码块:

  1. #include <iostream>
  2. using namespace std;
  3. class Student
  4. {
  5. public:
  6. Student(int n=0, float s=0): num(n), score(s){}
  7. void input();
  8. void display();
  9. private:
  10. int num;
  11. float score;
  12. };
  13. void Student::input()
  14. {
  15. cin>>num>>score;
  16. }
  17. void Student::display()
  18. {
  19. cout<<num<<' '<<score<<endl;
  20. }
  21. int main()
  22. {
  23. Student stu[5], *p;
  24. int i;
  25. for (i=0; i<5; i++){
  26. cout<<"Pleas enter No."<<i+1<<" student info: ";
  27. stu[i].input();
  28. }
  29. for (p=stu; p<stu+5; p+=2){
  30. p->display();
  31. cout<<endl;
  32. }
  33. system("pause");
  34. return 0;
  35. }

发表评论

表情:
评论列表 (有 0 条评论,164人围观)

还没有评论,来说两句吧...

相关阅读