📄 p2-125.cpp
字号:
#include<iostream.h>
//基类First
class First {
int val1;
public:
SetVal1(int v) {
val1=v;
}
void show_First(void) {
cout<<"val1="<<val1<<endl;
}
};
//派生类Second
class Second:public First { //默认为private模式
int val2;
public:
void SetVal2(int v1,int v2) {
SetVal1(v1); //可见,合法
val2=v2;
}
void show_Second(void) {
// cout<<"val1="<<val1<<endl; 不能访问First私有成员
show_First();
cout<<"val2="<<val2<<endl;
}
};
main() {
Second s1;
//调用Second类定义的成员函数
s1.SetVal2(2,3);
cout<<"s1.show_Second():"<<endl;
s1.show_Second();
//调用First类定义的成员函数
s1.SetVal1(10);
cout<<"s1.show_First():"<<endl;
s1.show_First();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -