21次C++作业

  1 //第一题目
  2 class A                   //A为基类
  3 {public:
  4   void f1( );
  5   int i;
  6  protected:
  7   void f2();
  8   int j;
  9  private:
 10   int k;
 11 };
 12 /*f4里面是可以调用的
 13 C的对象不能直接调用f2()的成员函数里
 14 f2()也是类C的protected 函数在C++中派生类的成员函数是可以直接访问基类的保护成员和公有成员。
 15 */
 16 class B: public A            //B为A的公用派生类
 17 {public:
 18   void f3( );
 19  protected:
 20   int m;
 21  private:
 22   int n;
 23 };
 24 
 25 class C: public B              //C为B的公用派生类
 26 {public:
 27   void f4();
 28  private:
 29   int p;
 30 };
 31 
 32 int main()
 33 {A a1;                         //a1是基类A的对象
 34  B b1;                         //b1是派生类B的对象
 35  C c1;                         //c1是派生类C的对象
 36  return 0;
 37 }
 38 //(1)b1.i,b1.j可以,b1.k不可以
 39 //(2)可以
 40 //(3)i,j可以,k不可以
 41 //(4)i,j,m,p可以,k,n,p不可以
 42 //(5)可以
 43 //(6)可以
 44 //第二题目
 45 #include <iostream>
 46 using namespace std;
 47 class A
 48 {public:
 49   void f1( );
 50  protected:
 51   void f2();
 52  private:
 53   int i;
 54 };
 55 
 56 class B: public A
 57 {public:
 58   void f3( );
 59   int k;
 60  private:
 61   int m;
 62 };
 63 
 64 class C: protected B
 65 {public:
 66   void f4();
 67  protected:
 68   int n;
 69  private:
 70   int p;
 71 };
 72 
 73 class D: private C
 74 {public:
 75   void f5();
 76  protected:
 77   int q;
 78  private:
 79   int r;
 80 };
 81 
 82 int main()
 83 {A a1;
 84  B b1;
 85  C c1;
 86  D d1;
 87  return 0;
 88 }
 89 //第三题目
 90 #include <iostream>
 91 using namespace std;
 92 class A
 93  {
 94   public:
 95    A(){a=0;b=0;}
 96    A(int i){a=i;b=0;}
 97    A(int i,int j){a=i;b=j;}
 98    void display(){cout<<"a="<<a<<" b="<<b;}
 99   private:
100    int a;
101    int b;
102  };
103 
104 class B  : public A
105  {
106   public:
107    B(){c=0;}
108    B(int i):A(i){c=0;}
109    B(int i,int j):A(i,j){c=0;}
110    B(int i,int j,int k):A(i,j){c=k;}
111    void display1()
112     {display();
113      cout<<" c="<<c<<endl;
114     }
115    private:
116     int c;
117 };
118 
119 int main()
120 {   B b1;
121     B b2(1);
122     B b3(1,3);
123     B b4(1,3,5);
124     b1.display1();//a=0,b=0,c=0
125     b2.display1();//a=1,b=0,c=0
126     b3.display1();//a=1,b=3,c=0
127     b4.display1();//a=1,b=3,c=5
128     return 0;
129 }
130 //第四题目
131 #include <iostream>
132 using namespace std;
133 class A
134  {
135   public:
136    A(){cout<<"constructing A "<<endl;}
137    ~A(){cout<<"destructing A "<<endl;}
138  };
139 
140 class B  : public A
141  {
142   public:
143    B(){cout<<"constructing B "<<endl;}
144    ~B(){cout<<"destructing B "<<endl;}
145 };
146 
147 class C  : public B
148  {
149   public:
150    C(){cout<<"constructing C "<<endl;}
151    ~C(){cout<<"destructing C "<<endl;}
152 };
153 int main()
154 { C c1;
155   return 0;
156 }
157 //a(),b(),c(),~c(),~b(),~a()构造从基类开始,析构顺序相反
158 //第五题目
159 #include<string>
160 #include <iostream>
161 using namespace std;
162 class Teacher
163  {public:
164    Teacher(string nam,int a,char s,string tit,string ad,string t);
165    void display();
166   protected:
167     string name;
168     int age;
169     char sex;
170     string title;
171     string addr;
172     string tel;
173 };
174 
175 Teacher::Teacher(string nam,int a,char s,string tit,string ad,string t):
176      name(nam),age(a),sex(s),title(tit),addr(ad),tel(t){ }
177 void Teacher::display()
178      {cout<<"name:"<<name<<endl;
179       cout<<"age"<<age<<endl;
180       cout<<"sex:"<<sex<<endl;
181       cout<<"title:"<<title<<endl;
182       cout<<"address:"<<addr<<endl;
183       cout<<"tel:"<<tel<<endl;
184       }
185 
186 class Cadre
187  {public:
188    Cadre(string nam,int a,char s,string p,string ad,string t);
189    void display();
190   protected:
191    string name;
192    int age;
193    char sex;
194    string post;
195    string addr;
196    string tel;
197  };
198  
199 Cadre::Cadre(string nam,int a,char s,string p,string ad,string t):
200      name(nam),age(a),sex(s),post(p),addr(ad),tel(t){}
201      
202 void Cadre::display()
203     {cout<<"name:"<<name<<endl;
204      cout<<"age:"<<age<<endl;
205      cout<<"sex:"<<sex<<endl;
206      cout<<"post:"<<post<<endl;
207      cout<<"address:"<<addr<<endl;
208      cout<<"tel:"<<tel<<endl;
209     }
210 
211 class Teacher_Cadre:public Teacher,public Cadre
212  {public:
213    Teacher_Cadre(string nam,int a,char s,string tit,string p,string ad,string t,float w);
214    void show( );
215   private:
216     float wage;
217  };
218  
219 Teacher_Cadre::Teacher_Cadre(string nam,int a,char s,string t,string p,string ad,string tel,float w):
220   Teacher(nam,a,s,t,ad,tel),Cadre(nam,a,s,p,ad,tel),wage(w) {}
221 void Teacher_Cadre::show( )
222     {Teacher::display();//调用teacher的display函数输出
223      cout<<"post:"<<Cadre::post<<endl;
224      cout<<"wages:"<<wage<<endl;
225      }
226 
227 int main( )
228  {Teacher_Cadre te_ca("Wang-li",50,'f',"prof.","president","135 Beijing Road,Shanghai","(021)61234567",1534.5);
229   te_ca.show( );
230   return 0;
231 }

 

posted @ 2013-06-17 16:23  herizai  阅读(281)  评论(0编辑  收藏  举报