(1)(2)
#include<iostream>
using namespace std;
class Student
{
public:
Student(int n,float s):num(n),score(s){}
void change(int n,float s) const {num=n;score=s;}
void display() const {cout<<num<<" "<<score<<endl;}
private:
mutable int num;
mutable float score;
};
int main()
{
const Student stud(101,78.5);
stud.display();
stud.change(101,80.5);
stud.display();
return 0;
}
(3)
#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;}
private:
int num;
float score;
};
int main()
{
Student stud(101,78.5);
Student *p=&stud;
p->display();
p->change(101,80.5);
p->display();
return 0;
}
(4)
意为将p定义为一个指向常对象的指针,并使其指向stud。这里stud不是常对象,应不能通过编译。
(5)
这里是将p定义为一个指向对象的常指针,即p的指向不能再发生变化。在程序中p的指向没有再发生变化,应该
可以通过编译,其运行结果与第6题相同。