第五章

来源:互联网 发布:网络好坏英文怎么写 编辑:程序博客网 时间:2024/05/02 00:41
1.#include <iostream>using namespace std;class Student{public:  void get_value()   {cin>>num>>name>>sex;}  void display( )    {cout<<"num: "<<num<<endl;     cout<<"name: "<<name<<endl;     cout<<"sex: "<<sex<<endl;} 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: "<<age<<endl;          //引用派生类的私有成员,正确。       cout<<"address: "<<addr<<endl;}    //引用派生类的私有成员,正确。  private:       int age;       char addr[30]; };int  main(){Student1 stud1;  stud1.get_value_1();  stud1.display();  stud1.display_1();  return 0;}<img src="http://img.blog.csdn.net/20150518210632310" alt="" />
2.<pre name="code" class="cpp">#include <iostream>using namespace std;class Student{public:  void get_value()   {cin>>num>>name>>sex;}  void display( )    {cout<<"num: "<<num<<endl;     cout<<"name: "<<name<<endl;     cout<<"sex: "<<sex<<endl;} 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: "<<age<<endl;        //引用派生类的私有成员,正确。       cout<<"address: "<<addr<<endl;}    //引用派生类的私有成员,正确。  private:       int age;    char addr[30]; };int main() {Student1 stud1;  stud1.get_value_1();  stud1.display_1();  return 0;}

3.<pre name="code" class="cpp">#include <iostream>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: "<<num<<endl;  cout<<"name: "<<name<<endl;  cout<<"sex: "<<sex<<endl; }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: "<<num<<endl;         //引用基类的保护成员   cout<<"name: "<<name<<endl;       //引用基类的保护成员   cout<<"sex: "<<sex<<endl;         //引用基类的保护成员   cout<<"age: "<<age<<endl;         //引用派生类的私有成员   cout<<"address: "<<addr<<endl;    //引用派生类的私有成员  }int main( ) {Student1 stud1;                      //stud1是派生类student1类的对象  stud1.get_value_1();                 //调用派生类对象stud1的公用成员函数  stud1.display1( );                   //调用派生类对象stud1的公用成员函数  return 0; }
4.#include <iostream>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: "<<num<<endl;  cout<<"name: "<<name<<endl;  cout<<"sex: "<<sex<<endl; }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: "<<num<<endl;     //引用基类的保护成员,合法   cout<<"name: "<<name<<endl;       //引用基类的保护成员,合法   cout<<"sex: "<<sex<<endl;         //引用基类的保护成员,合法   cout<<"age: "<<age<<endl;         //引用派生类的私有成员,合法   cout<<"address: "<<addr<<endl;    //引用派生类的私有成员,合法  }int main( ) {Student1 stud1;                      //stud1是派生类student1类的对象  stud1.get_value_1();                 //调用派生类对象stud1的公用成员函数get_value_1  stud1.display1( );                   //调用派生类对象stud1的公用成员函数display1  return 0; }
5.
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 <iostream>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 <iostream>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;}   void display(){cout<<"a="<<a<<" b="<<b;}  private:   int a;   int b; };class B  : public A {  public:   B(){c=0;}   B(int i):A(i){c=0;}   B(int i,int j):A(i,j){c=0;}   B(int i,int j,int k):A(i,j){c=k;}   void display1()    {display();     cout<<" c="<<c<<endl;    }   private:    int c;};int main(){   B b1;    B b2(1);    B b3(1,3);    B b4(1,3,5);    b1.display1();         b2.display1();    b3.display1();    b4.display1();return 0;}

8.
#include <iostream>using namespace std;class A {  public:   A(){cout<<"constructing A "<<endl;}   ~A(){cout<<"destructing A "<<endl;} };class B  : public A {  public:   B(){cout<<"constructing B "<<endl;}   ~B(){cout<<"destructing B "<<endl;}};class C  : public B {  public:   C(){cout<<"constructing C "<<endl;}   ~C(){cout<<"destructing C "<<endl;}};int main(){ C c1;  return 0;}





0 0