1:
#include
using namespace std;
class Student
{public:
void get_value()
{cin>>num>>name>>sex;}
void display( )
{cout<<"num: "< cout<<"name: "< cout<<"sex: "< private : int num; char name[10]; char sex; }; class Student1: public Student {public: void get_value_1() {get_value(); cin>>age>>addr;} void display_1() { cout<<"age: "< cout<<"address: "< private: int age; char addr[30]; }; int main() {Student1 stud1; stud1.get_value_1(); stud1.display(); stud1.display_1(); return 0; } 2: #include using namespace std; class Student {public: void get_value() {cin>>num>>name>>sex;} void display( ) {cout<<"num: "< cout<<"name: "< cout<<"sex: "< private : int num; char name[10]; char sex; }; class Student1: private Student {public: void get_value_1() {get_value(); cin>>age>>addr;} void display_1() {display(); cout<<"age: "< cout<<"address: "< private: int age; char addr[30]; }; int main() {Student1 stud1; stud1.get_value_1(); stud1.display_1(); return 0; } 3: #include using namespace std; class Student //声明基类 {public: //基类公用成员 void get_value(); void display( ); protected : //基类保护成员 int num; char name[10]; char sex; }; void Student::get_value() {cin>>num>>name>>sex;} void Student::display( ) {cout<<"num: "< cout<<"name: "< cout<<"sex: "< } class Student1: protected Student //声明一个保护派生类 {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: "< cout<<"name: "< cout<<"sex: "< cout<<"age: "< cout<<"address: "< } int main( ) {Student1 stud1; //stud1是派生类student1类的对象 stud1.get_value_1(); //调用派生类对象stud1的公用成员函数 stud1.display1( ); //调用派生类对象stud1的公用成员函数 return 0; } 4: 解法一 #include using namespace std; class Student//声明基类 {public: //基类公用成员 void get_value(); void display( ); protected : //基类保护成员 int num; char name[10]; char sex; }; void Student::get_value() {cin>>num>>name>>sex;} void Student::display( ) {cout<<"num: "< cout<<"name: "< cout<<"sex: "< } class Student1: public Student //声明一个公用派生类 {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: "< cout<<"name: "< cout<<"sex: "< cout<<"age: "< cout<<"address: "< } int main( ) {Student1 stud1; //stud1是派生类student1类的对象 stud1.get_value_1(); //调用派生类对象stud1的公用成员函数get_value_1 stud1.display1( ); //调用派生类对象stud1的公用成员函数display1 return 0; } 解法二 #include using namespace std; class Student //声明基类 {public: //基类公用成员 void get_value(); void display( ); protected : //基类保护成员 int num; char name[10]; char sex; }; void Student::get_value() {cin>>num>>name>>sex;} void Student::display( ) {cout<<"num: "< cout<<"name:"< cout<<"sex:"< } class Student1: protected Student //声明一个公用派生类 {public: void get_value_1(); void display1( ); private: int age; char addr[30]; }; void Student1::get_value_1() {cin>>age>>addr;} void Student1::display1( ) {cout<<"age:"< cout<<"address:"< } int main( ) {Student1 stud1; //stud1是派生类student1类的对象 stud1.get_value(); stud1.get_value_1(); stud1.display( ); stud1.display1(); //合法。display1是派生类中的公用成员函数 return 0; } 5: class A //A为基类 {public: void f1( ); int i; protected: void f2(); int j; private: int k; }; class B: public A //B为A的公用派生类 {public: void f3( ); protected: int m; private: int n; }; class C: public B //C为B的公用派生类 {public: void f4(); private: int p; }; int main() {A a1; //a1是基类A的对象 B b1; //b1是派生类B的对象 C//c1是派生类C的对象 return 0; } 6: #include using namespace std; class A {public: void f1( ); protected: void f2(); private: int i; }; class B: public A {public: void f3( ); int k; private: int m; }; class C: protected B {public: void f4(); protected: int n; private: int p; }; class D: private C {public: void f5(); protected: int q; private: int r; }; int main() {A a1; B b1; C c1; D d1; return 0; } 7: #include using namespace std; class A { public: A(){a=0;b=0;} A(int i){a=i;b=0;} A(int i,int j){a=i;b=j;}