6.1 分析以下程序的執行結果 #include<iostream.h> class base { public: base(){cout<<"constrUCting base class"<<endl;} ~base(){cout<<"destructing base class"<<endl; } }; class subs:public base { public: subs(){cout<<"constructing sub class"<<endl;} ~subs(){cout<<"destructing sub class"<<endl;} }; void main() { subs s; } 解: 本題說明單繼續情況下構造函數和析構函數的調用順序。這里base為基類,subs為派生類。 所以輸出為: constructing base class constructing sub class destructing sub class destrcuting base class 注重:在單繼續情況下,首先調用基類的構造函數 ,隨后調用派生類的構造函數,析構函數的調用順序則正好相反。
6.2 分析以下程序的執行結果: #include<iostream.h> class base { int n; public: base(int a) { cout<<"constructing base class"<<endl; n=a; cout<<"n="<<n<<endl; } ~base(){cout<<"destructing base class"<<endl;} }; class subs:public base { base bobj; int m; public: subs(int a,int b,int c):base(a),bobj(c) { cout<<"constructing sub cass"<<endl; m=b; cout<<"m="<<m<<endl; } ~subs(){cout<<"destructing sub class"<<endl;} }; void main() { subs s(1,2,3); } 解: 本題說明 派生類中含有對象成員情況下構造函數和析構函數的調用順序。這里base為基類,subs為派生類,subs類的構造函數中含有對象成員。 所以輸出為: constrcuting base class n=1 constructing base class n=3 constructing sub class m=2 destructing sub class destructing base class destructing base class 注重:當派生類中含有對象成員時,構造函數的調用順序如下: 1)基類的構造函數 2)對象成員的構造函數 3)派生類的構造函數
析構函數的調用順序與之相反
-------------------------------------------------
6.3 分析以下程序的執行結果 #include<iostream.h> class A { public: int n; }; class B:public A{}; class C:public A{}; class D:public B,public C { int getn(){return B::n;} }; void main() { D d; d.B::n=10; d.C::n=20; cout<<d.B::n<<","<<d.C::n<<endl; } 解: D類是從類和類派生的而類和類又都是從類派生的,但各有自己的副本。所以對于對象d,d.B::n與d.C::n是兩個不同的數據成員它們互無聯系。 所以輸出為: 10,20 -------------------------------------------- 6.4 分析以下程序的執行結果 #include<iostream.h> class A { public: int n; }; class B:virtual public A{}; class C:virtual public A{}; class D:public B,public C { int getn(){return B::n;} }; void main() { D d; d.B::n=10; d.C::n=20; cout<<d.B::n<<","<<d.C::n<<endl; } 解: D類是從類和類派生的而類和類又都是從類派生,但這是虛繼續關系即是虛基類因此和共用一個的副本所以對于對象d,d.B::n與d.C::n是一個成員。 所以輸出為: 20,20 --------------------------------------- 6.5 假設圖書館的圖書包含書名、編號作者屬性讀者飲包含姓名和借書證屬性每位讀者最多可借5本書,編寫程序列出某讀者的借書情況。 解: 設計一個類,從它派生出胃病書類book和讀者類reader,在reader類中有一個rentbook()成員函數用于借閱圖書。 程序代碼如下: 本程序的執行結果如下: #include<iostream.h> #include<string.h> class object { char name[20]; int no; public: object(){} object(char na[],int n) { strcpy(name,na);no=n; } void show() { cout<<name<<"("<<no<<")"; } }; class book:public object { char author[10]; public: book(){} book(char na[],int n,char auth[]):object(na,n) { strcpy(author,auth); } void showbook() { show(); cout<<"作者:"<<author; } }; class reader:public object { book rent[5]; int top; public: reader(char na[],int n):object(na,n){top=0;} void rentbook(book &b) { rent[top]=b; top++; } void showreader() { cout<<"讀者:";show(); cout<<endl<<"所借圖書:"<<endl; for(int i=0;i<top;i++) { cout<<" "<<i+1<<":"; // 5個空格 rent[i].show(); cout<<endl; } } }; void main() { book b1("C語言",100,"譚浩強"),b2("數據結構",110,"嚴蔚敏"); reader r1("王華",1234); r1.rentbook(b1); r1.rentbook(b2); r1.showreader(); }
本程序的執行結果如下: 輸出結果: 李明,年齡42歲,擔任設計處處長, 高級工程師,從事電站鍋爐設計專業。 三層交換技術 交換機與路由器密碼恢復 交換機的選購 路由器設置專題 路由故障處理手冊 數字化校園網解決方案 題 1. 分析以下程序的執行結果: #include<iostream.h> class base { int n; public: base(){}; base (int a) { cout << "constructing base class" << endl; n=a; cout << "n=" << n << endl; } ~base() { cout << "destructing base class" << endl; } }; class subs : public base { int m; public: subs(int a, int b) : base(a) { cout << "constructing sub class" << endl; m=b; cout << "m=" << m << endl; } subs() { cout << "destructing sub class" << endl; } }; void main () { subs s(1,2); }
解: 這里base 是基類,subs為派生類,subs類的構造函數中含有調用基本類的構造函數。 所以輸出為: constructing base class n=1 constructing sub class n=2 destructing base class destructing sub class
題 3 分析以下程序的執行結果: #include <iostream.h> class A { int a; public: A(int i) { a=i;cout << "constructing class A" << endl; } void print() { cout << a << endl; } ~A() { cout << "destructing class A" << endl; } }; class Bi:public A { int bl; public: Bl(int i,int j):A(i) { bl=j;cout << "constructing class BI" << endl; void print() { A::print (); cout << bl << endl; } ~BI(){ cout << "destructing class BI" << endl; } }; class B2:public A { int b2; public: B2(int i,int j):A(i); { b2=j;cout << "constructing class B2" << endl; } void print() { A::print (); cout << b2 << endl; } ~B2() { cout << "destructing class B2" << endl; } }; class C:public B1,public B2 { int c; public: C(int i,int j,int k, int 1,int m) :Bl(i,j),B2(k,1),c(m) { cout << "constructing class C" << endl; } void print() { Bl::print(); B2::print(); cout << c << endl; } ~C( ){ cout << "destructing class C" << endl; } }; void main() { C c1(1,2,3,4,5); cl.print(); }
解: C類是從B1類和B2類派生的,而B1和B2類又都是從A類派生,但各有自己的副本,所有這些成員函數均有print()成員函數。所以,在C的成員函數實現中,調用print()時,要加上類作用域運算符“::”。 所以輸出為: constructing class A constructing class B1 constructing class A constructing class B2 constructing class C 1 2 3 4 5 destructing class C destructing class B2 destructing class A destructing class B1 destructing class A