📄 demo_8_const_pointer_1.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 *p=&stud; //指向常对象的指针
// Student const *p=&stud; //指向常对象的指针
p->display(); //常对象只能调用常成员函数
// p->change(101,88.5); //Error! 不能通过指针来改变它所指向常对象的值
// stud.change(101,88.5); //用对象名引用成员函数改变对象的值
p=&Student(101,88.5); //调用默认的拷贝构造函数,指针可以指向其他对象
p->display(); //常对象只能调用常成员函数
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -