📄 demo_8_const_pointer_3.cpp
字号:
//************************************************
# include <iostream.h>
class Student
{
public:
Student(int n,float s):num(n),score(s){} //构造函数
void change(int n,float s) {num=n;score=s;}
void display() const {cout<<num<<" : "<<score<<endl;}
private:
int num;
float score;
};
int main()
{
Student stud(101,78.5); //声明对象并初始化
const Student * const p=&stud; //指向常对象的常指针
// Student const * const p=&stud; //指向常对象的常指针
p->display(); //常对象只能调用常成员函数
// p->change(101,88.5); //Error!
stud.change(101,88.5); //用对象名引用成员函数改变对象的值
// p=&Student(101,88.5); //Error!
p->display();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -