?? xt11-3.cpp
字號:
#include <iostream>
using namespace std;
class Student //聲明基類
{public: //基類公用成員
void get_value();
void display( );
protected : //基類保護(hù)成員
int num;
char name[10];
char sex;
};
void Student::get_value()
{cin>>num>>name>>sex;}
void Student::display( )
{cout<<"num: "<<num<<endl;
cout<<"name: "<<name<<endl;
cout<<"sex: "<<sex<<endl;
}
class Student1: protected Student //聲明一個保護(hù)派生類
{public:
void get_value_1();
void display1( );
private:
int age;
char addr[30];
};
void Student1::get_value_1()
{get_value();
cin>>age>>addr;
}
void Student1::display1( )
{cout<<"num: "<<num<<endl; //引用基類的保護(hù)成員
cout<<"name: "<<name<<endl; //引用基類的保護(hù)成員
cout<<"sex: "<<sex<<endl; //引用基類的保護(hù)成員
cout<<"age: "<<age<<endl; //引用派生類的私有成員
cout<<"address: "<<addr<<endl; //引用派生類的私有成員
}
int main( )
{Student1 stud1; //stud1是派生類student1類的對象
stud1.get_value_1(); //調(diào)用派生類對象stud1的公用成員函數(shù)
stud1.display1( ); //調(diào)用派生類對象stud1的公用成員函數(shù)
return 0;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -