当前位置 博文首页 > Aaron_Yang:习题 3.4 建立一个对象数组,内放5个学生的数据(学

    Aaron_Yang:习题 3.4 建立一个对象数组,内放5个学生的数据(学

    作者:[db:作者] 时间:2021-08-01 17:55

    谭浩强c++ 面向对象程序设计(第2版)
    习题 3.4 建立一个对象数组,内放5个学生的数据(学号、成绩),用指针指向数组首元素,输出第1,3,5个学生的数据

    #include <iostream>
    using namespace std;
    
    class Student
    {
    	private:
    		int num;
    		int score;
    	public:
    		void display();
    	
    	Student(int n,int s):num(n),score(s){}
    };
    Student stud[5] = 
    {
    	Student(1,91),
    	Student(2,92),
    	Student(3,93),
    	Student(4,94),
    	Student(5,95)
    };
    void Student::display()
    {
    	cout << num << "  " << score << endl;
    }
    //int* temp=&stud[0];
    //容易错误的点
    Student* temp = stud;
    
    
    int main()
    {
    	temp->display();
    	temp++;
    	temp++;
    	temp->display();
    	temp++;
    	temp++;
    	temp->display();
    	//同理
    	/*stud[0].display();
    	stud[2].display();
    	stud[4].display();*/
    
    	system("pause");
    	return 0;
    }
    

    感谢访问 如果觉得有帮助请点个赞 谢谢
    欢迎交流

    cs
    下一篇:没有了