




版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、程序設(shè)計(jì)實(shí)習(xí)第二十一講 習(xí)題課, ,類(lèi)和對(duì)象基本概念(1),寫(xiě)出下面程序的運(yùn)行結(jié)果 class Apple private : static int nTotalNumber; public: Apple() nTotalNumber +; Apple( ) nTotalNumber -; static void PrintTotal() cout nTotalNumber endl; ; int Apple:nTotalNumber = 0; Apple Fun( const Apple ,解析: return返回時(shí),產(chǎn)生臨時(shí)變量,Total Number析構(gòu)時(shí)會(huì)減小 4 1,類(lèi)和對(duì)象基本概
2、念(2),*寫(xiě)出下面程序的運(yùn)行結(jié)果 class Sample public: int v; Sample() ; Sample(int n):v(n) ; Sample( Sample ,解析:構(gòu)造函數(shù)和復(fù)制構(gòu)造函數(shù)的調(diào)用 9 22 5 注意區(qū)分: Sample b=a; d=a;,類(lèi)和對(duì)象基本概念(3),程序輸出結(jié)果如下,請(qǐng)?zhí)羁?0 5 class A public: int val; A(_ ) val = n; ; _ GetObj() return _; ; main() A a; cout a.val endl; a.GetObj() = 5; cout a.val endl; ,A
3、(int n=0) A ,類(lèi)和對(duì)象基本概念(4),程序輸出結(jié)果如下,請(qǐng)?zhí)羁?3+4i 5+6i 補(bǔ)足Complex類(lèi)的成員函數(shù),不能增加成員變量 class Complex private: double r,i; public: void Print() cout r + i i endl; ; int main() Complex a; a = 3+4i;a.Print(); a = 5+6i;a.Print(); ,類(lèi)和對(duì)象基本概念(4),構(gòu)造函數(shù) Complex() ; Complex (const char t) if(!t) r=0.0;i=0.0; else r = t0 - 0
4、; i = t2 - 0; ,另一種思路 重載賦值操作符 Complex ,類(lèi)和對(duì)象基本概念(5),程序輸出結(jié)果如下,請(qǐng)?zhí)羁?10 補(bǔ)足Sample類(lèi)的成員函數(shù),不能增加成員變量 class Sample public: int v; Sample(int n):v(n) ; ; main() Sample a(5); Sample b = a; cout b.v ; ,Sample( Sample ,類(lèi)和對(duì)象基本概念(6),程序輸出結(jié)果如下,請(qǐng)?zhí)羁?This Hello 補(bǔ)足MyString類(lèi)的成員函數(shù),不能增加成員變量 class MyString char * p; public: My
5、String( char * s ) p = new charstrlen(s)+1; strcpy(p,s); MyString() delete p; const char * c_str() return p; ;,main() MyString s1(This), s2 =s1; s2.Copy ( Hello); cout s1.c_str () endl s2.c_str () ; ,類(lèi)和對(duì)象基本概念(6),補(bǔ)足成員函數(shù) void Copy( char * s) delete p; p = new charstrlen(s)+1; strcpy(p,s); MyString( My
6、String ,類(lèi)和對(duì)象基本概念(7),程序輸出結(jié)果如下,請(qǐng)?zhí)羁?5,5 5,5 class Base public: int k; Base (int n) : k(n) ; class Big public: int v; Base b; Big _ Big _ ;,main() Big a1(5); Big a2 = a1; cout a1.v , a1.b.k endl; cout a2.v , a2.b.k endl; ,Big(int n):v(n),b(n) Big(Big public: MyInt( int n) nVal = n ; int ReturnVal() retu
7、rn nVal; . ;,main () MyInt objInt(10); objInt-2-1-3; cout objInt.ReturnVal(); cout ,; objInt-2-1; cout objInt.ReturnVal(); ,MyInt ,運(yùn)算符重載(2),*程序輸出結(jié)果如下,請(qǐng)?zhí)羁?(4,5) (7,8) class Point private: int x; int y; public: Point(int x_,int y_ ):x(x_),y(y_) ; _; ; _ operator (_, const Point ,main() cout Point(4,5)
8、 Point(7,8); ,運(yùn)算符重載(2),class Point private: int x; int y; public: Point(int x_,int y_ ):x(x_),y(y_) ; _; ; _ operator (_, const Point ,friend ostream ,ostream ,o,運(yùn)算符重載(3),*程序輸出結(jié)果如下,寫(xiě)一個(gè)二維數(shù)組類(lèi) Array2 0,1,2,3, 4,5,6,7, 8,9,10,11 next 0,1,2,3, 4,5,6,7, 8,9,10,11,運(yùn)算符重載(3),using std:cout; using std:endl; i
9、nt main() Array2 a(3,4); int i,j; for( i = 0;i 3; i + ) for( j = 0; j 4; j + ) aij = i * 4 + j; for( i = 0;i 3; i + ) for( j = 0; j 4; j + ) cout a(i,j) ,; cout endl; ,cout next endl; Array2 b; b = a; for( i = 0;i 3; i + ) for( j = 0; j 4; j + ) cout bij ,; cout endl; return 0; ,運(yùn)算符重載(3),class Array
10、2 private: int * p; int r,c; public: Array2() p = NULL ; Array2( int r_, int c_ ):r(r_),c(c_) p = new int r * c; Array2( Array2 ,Array2 ,注:重載的實(shí)際上是第二維的,第一維的直接調(diào)用int型一維數(shù)組的定義,運(yùn)算符重載(3):第二種解法,class CWordArr private: int *m_pdat; long m_size; public: CWordArr(long size) m_pdat= new intsize; m_size = size;
11、; CWordArr() delete m_pdat; ; int,運(yùn)算符重載(3):第二種解法,class Array2 private: CWordArr *m_p; long m_col,m_row; public: Array2(long col,long row) m_p=new CWordArr*col; for (long i=0;icol;i+) m_pi= new CWordArr(row); m_col=col; m_row=row; , Array2() for (long i=0;im_col;i+) delete m_pi; m_pi=NULL; delete m_p
12、; CWordArr ,通過(guò)兩次重載的疊加,就可以用axy的方式來(lái)訪(fǎng)問(wèn)數(shù)組元素 C+不可能在一個(gè)類(lèi)里面實(shí)現(xiàn)的重載(若這種相當(dāng)于三元算符),因此有兩個(gè)類(lèi):一個(gè)是一維數(shù)組類(lèi),實(shí)現(xiàn)的重載;另一個(gè)類(lèi)是二維數(shù)組類(lèi),必須持有一組一維數(shù)組類(lèi)的實(shí)例,存放二維數(shù)組的各個(gè)行的數(shù)組,二維數(shù)組類(lèi)也要重載運(yùn)算符,運(yùn)算符重載(4),程序輸出結(jié)果如下,寫(xiě)一個(gè)HugeInt類(lèi) 1)100000089000000 2)100000089000000 3)10000 4)10000 5)10001 6)10006 7)100000089010006,運(yùn)算符重載(4),void main() CHugeInt a(12345454
13、36342424354354365289899834234235); CHugeInt d(9999); CHugeInt temp = CHugeInt(100000088888888) + 111112; CHugeInt temp2 = 111112+CHugeInt(100000088888888); cout 1) temp endl; cout 2) temp2 endl; cout 3) +d endl; cout 4) d+ endl; cout 5) d endl; d += 5; cout 6) d endl; cout 7) d + temp; ,運(yùn)算符重載(4),代碼過(guò)
14、多,只截取部分,/重載運(yùn)算符+ CHugeInt operator +(const CHugeInt ,const CHugeInt ,運(yùn)算符重載(4),friend ostream ,繼承和多態(tài)(1),程序輸出結(jié)果如下,寫(xiě)一個(gè)Mystring類(lèi) 1. abcd-efgh-abcd-2. abcd-3.4. abcd-efgh-5. efgh-6. c7. abcd-8. ijAl-9. ijAl-mnop10. qrst-abcd-11. abcd-qrst-abcd- uvw xyz,aboutbigmetakeabcdqrst-abcd-,Mystring類(lèi)(1),#include #i
15、nclude using namespace std; class MyString public: char *p; MyString() / 構(gòu)造函數(shù) p = ; MyString(char *t) / 構(gòu)造函數(shù) p = new charstrlen(t) + 1; strcpy(p,t); MyString(const MyString ,Mystring類(lèi)(2),MyString() / 析構(gòu)函數(shù) if(p) deletep; MyString operator+(const MyString ,Mystring類(lèi)(3),MyString operator+(const char *s
16、) / +號(hào)重載,這里表示 /MyString類(lèi)型+字符串的情形 char *q; q = new charstrlen(p) + strlen(s) + 1; strcpy(q, p); strcat(q, s); MyString temp(q); delete q; return temp; MyString 0,Mystring類(lèi)(4),char ,Mystring類(lèi)(5),MyString operator()(int i, int n) / ()重載 char *q; q = new charn+1; strncpy(q, p+i, n); qn = 0; MyString tem
17、p(q); delete q; return temp; ; ostream ,Mystring類(lèi)(6),int operator 號(hào)的重載 if(strcmp(s1.p,s2.p) (const MyString ,Mystring類(lèi)(7),int CompareString(const void * e1, const void * e2) / 字符串比較函數(shù) MyString * s1 = (MyString * ) e1; MyString * s2 = (MyString * ) e2; if(*s1 *s2 ) return 0; else return 1; ,Mystring類(lèi)
18、(8),從string類(lèi)派生的寫(xiě)法 class MyString : public string public: MyString():string() ; MyString( const char * s):string(s); MyString( const string ,繼承和多態(tài)(2),寫(xiě)出下面程序的運(yùn)行結(jié)果,class B private: int nBVal; public: void Print() cout nBVal= nBVal endl; void Fun() cout B:Fun endl; B ( int n ) nBVal = n; ;,class D:publi
19、c B private : int nDVal; public: void Print() B:Print(); cout nDVal=nDVal endl; D( int n) : B(3*n) nDVal = n; void Fun() cout D:Fun endl; ;,繼承和多態(tài)(2),main() B * pb; D * pd; D d(4); d.Fun(); pb = new B(2); pd = new D(8); pb - Fun(); pd-Fun(); pb-Print (); pd-Print (); pb = ,D:Fun B:Fun D:Fun nBVal=2 n
20、BVal=24 nDVal=8 B:Fun nBVal=12,繼承和多態(tài)(3),程序輸出結(jié)果如下,請(qǐng)?zhí)羁?A:Fun A:Do A:Fun C:Do0,class A private: int nVal; public: void Fun() cout A:Fun endl; ; virtual void Do() cout A:Do endl; ;,class B:public A public: virtual void Do() cout B:Do endl; ; class C:public B public: void Do( ) cout C:Doendl; void Fun()
21、cout C:Fun endl; ;,繼承和多態(tài)(3),void Call(_) p-Fun(); p-Do(); main() Call( new A(); Call( new C(); ,A * p,void Call(_) p.Fun(); p.Do(); main() Call(A(); Call(C(); ,A Node * next; ; template class CLinkList private: Node * pHead; public: CLinkList(); void AppendNode( D data); void PrintList(); ;,template
22、 CLinkList:CLinkList() pHead = new Node; pHead-next = NULL; ; main() CLinkList l; for( int i = 0;i 4;i +) l.AppendNode(i); l.PrintList(); cout endl; for( i = 9;i 12;i +) l.AppendNode(i); l.PrintList(); ,模板(1),template void CLinkList:AppendNode( D data) Node * p = pHead; while( p-next ) p = p-next; p
23、-next = new Node; p = p-next; p-data = data; p-next = NULL; ,template void CLinkList:PrintList() Node * p = pHead; while( p-next ) cout next-data next; ,模板(2),填空使程序能編譯通過(guò),并寫(xiě)出運(yùn)行的輸出結(jié)果,#include template class myclass T i; public: myclass (T a) i = a; void show( ) cout obj(This); obj.show(); ,class T,cha
24、r *,該程序輸出結(jié)果為:_,This,模板(2),填空得到以下結(jié)果 TomHanks 注意,不允許使用任何常量,template class myclass _; int nSize; public: myclass ( _, int n) p = new Tn; for( int i = 0;i n;i + ) pi = ai; nSize = n; ,myclass( ) delete p; void Show() for( int i = 0;i obj (_ _); obj.Show(); ,T * p,T * a,szName,strlen(szName),STL(1),看程序?qū)懡Y(jié)
25、果,class A private : int nId; public: A(int n) nId = n; cout nId contructor endl; ; A( const A ,main() vector vp; vp.push_back(new A(1); vp.push_back(new A(2); vector v; v.push_back (3); ,1 contructor 2 contructor 3 contructor 3 copy constructor 3 destructor 3 destructor,STL(2),程序輸出結(jié)果如下,請(qǐng)?zhí)羁?Tom,Jack,
26、Mary,John,,template class MyClass T arrayT2; public: MyClass( T * begin ) copy( begin, begin + T2, array); void List() T * i; for( i = array; i != array + T2;i +) cout * i , ; ;,main() string array4 = Tom,Jack,Mary,John; _ ; obj.List(); ,class T int T2,MyClass obj(array),STL(3),程序輸出結(jié)果如下,請(qǐng)?zhí)羁?Tom,Jack
27、,Mary,John,template class MyClass vector array; public: MyClass ( T * begin,int n ):array(n) copy( begin, begin + n, array.begin(); void List() _; for( i = array.begin(); i != array.end();i +) cout * i , ; ;,main() string array4 = Tom,Jack,Mary,John; _; obj.List(); ,vector:iterator i;,MyClass obj( a
28、rray,4),STL(4),程序輸出結(jié)果如下,請(qǐng)?zhí)羁?1 2 6 7 8 9 main() int a = 8,7,8,9,6,2,1; _; for( int i = 0;i o(cout, ); copy( v.begin(),v.end(),o); ,setv,v.insert(ai),STL(5),程序輸出結(jié)果如下,請(qǐng)?zhí)羁?A:Print: 1 B:Print: 2 B:Print: 3,template void PrintAll( const T ,(*i)-Print(),STL(5),class B:public A public: B(int i):A(i) void Pr
29、int() cout B:Print: nVal endl; ; main() _; v.push_back( new A(1); v.push_back (new B(2); v.push_back (new B(3); PrintAll( v); ,vector v,STL(6),*寫(xiě)一個(gè)自己的 CMyostream_iterator 模板,使之能和 ostream_iterator 模板達(dá)到一樣的效果,using namespace std; main() int a5 = 1,2,3,4,5; CMyostream_iterator output(cout,*); vector v(a
30、,a+5); copy(v.begin(),v.end(),output); ,提示,參考 copy 的help template OutIt copy(InIt first, InIt last, OutIt x); The template function evaluates *(x + N) = *(first + N) once for each N in the range 0, last - first), for strictly increasing values of N beginning with the lowest value. It then returns x
31、+ N. If x and first designate regions of storage, x must not be in the range first, last) copy 的源代碼: template inline _OI copy(_II _F, _II _L, _OI _X) for (; _F != _L; +_X, +_F) *_X = *_F; return (_X); ,48,要支持copy函數(shù)必須重載三個(gè)運(yùn)算符:*,=,+,STL(6),template class CMyostream_iterator private: ostream ,STL(7),程序輸
32、出結(jié)果如下,請(qǐng)?zhí)羁?5*3*4*2*1* 1*2*3*4*5* 1*2*9*4*5*,template class MyClass: public list public: _ (int n) iterator i; int k = 0; for( _) if( k = n) return _; k +; MyClass(int n):_ ;,main() MyClass obj(5); int a = 5, 3, 4, 2,1 ; copy( a, a + 5, obj.begin(); ostream_iterator output (cout,*); copy( obj.begin(),obj.end(),output); cout endl; obj.sort(); copy( obj.begin(),obj.end(),output); cout endl; obj2 = 9; copy( obj.begin(),obj.end(),output); ,Ti != end(); i +,* i,list(n),注意:list
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 班級(jí)未來(lái)發(fā)展的愿景與規(guī)劃計(jì)劃
- 老師個(gè)人專(zhuān)業(yè)發(fā)展目標(biāo)計(jì)劃
- 急診工作中的時(shí)間管理計(jì)劃
- 杭州某樓盤(pán)營(yíng)銷(xiāo)工作總結(jié)
- 電子商務(wù)系統(tǒng)的分析與設(shè)計(jì)第1章
- 客戶(hù)需求分析課程
- 統(tǒng)編版小學(xué)語(yǔ)文二年級(jí)下冊(cè)第14課《小馬過(guò)河》精美課件
- 統(tǒng)編版小學(xué)語(yǔ)文二年級(jí)下冊(cè)《快樂(lè)讀書(shū)吧》精美課件
- 2025年高中地理壓軸題答題技巧分享教你快速拿高分
- 第5課+古代非洲與美洲+高一歷史下學(xué)期統(tǒng)編版(2019)必修中外歷史綱要下
- GB/T 10059-2023電梯試驗(yàn)方法
- GB/T 21837-2023鐵磁性鋼絲繩電磁檢測(cè)方法
- 貨物運(yùn)輸服務(wù)投標(biāo)方案(技術(shù)方案)
- 15D500-15D505 防雷與接地圖集(合訂本)
- 2023年高考全國(guó)卷英語(yǔ)完型填空講解 課件 2024屆高考英語(yǔ)一輪復(fù)習(xí)
- 第二章1:公文寫(xiě)作的構(gòu)成要素
- 單兵隊(duì)列教學(xué)法
- DB14-T 2803-2023 藥品委托儲(chǔ)存配送管理規(guī)范
- 第13課-香港和澳門(mén)的回歸
- 人教部編版三年級(jí)下冊(cè)道德與法治 1、我是獨(dú)特的 教案
- 合同法合同的效力教學(xué)課件
評(píng)論
0/150
提交評(píng)論