修改程序,添加一个fun函数,改写main函数。在main函数中调用fun函数,在fun函数中调用change和display函数。在fun函数中使用对象的引用(Student &)作为形参。
#include <iostream> using namespace std;
class Student { public: Student(int n,float s):num(n),score(s){} void change(int n,float s) { num = n; score = s; } void display() { cout << num << " " << score << endl; } void fun(Student &p,int n,float s) { p.change(n,s); p.display(); } private: int num; float score; };
int main() { Student stud(101,78.5); Student stud1(101,11.8); Student * const p = &stud; //p = &stud1; 编译报错,因此指向对象的常指针,不能更改已经指向的对象
p->display(); p->fun(stud,101,80.5); system("pause"); return 0; }
|
阅读(3152) | 评论(0) | 转发(0) |