C++課后習(xí)題答案 第八章多態(tài)性_第1頁
C++課后習(xí)題答案 第八章多態(tài)性_第2頁
C++課后習(xí)題答案 第八章多態(tài)性_第3頁
C++課后習(xí)題答案 第八章多態(tài)性_第4頁
C++課后習(xí)題答案 第八章多態(tài)性_第5頁
已閱讀5頁,還剩17頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、第 八 章 多態(tài)性8-1 什么叫做多態(tài)性 ?在C+中是如何實(shí)現(xiàn)多態(tài)的?解:多態(tài)是指同樣的消息被不同類型的對象接收時(shí)導(dǎo)致完全不同的行為,是對類的特定成員函數(shù)的再抽象。C+支持的多態(tài)有多種類型,重載(包括函數(shù)重載和運(yùn)算符重載)和虛函數(shù)是其中主要的方式。8-2 什么叫做抽象類?抽象類有何作用?抽象類的派生類是否一定要給出純虛函數(shù)的實(shí)現(xiàn)?解:帶有純虛函數(shù)的類是抽象類。抽象類的主要作用是通過它為一個(gè)類族建立一個(gè)公共的接口,使它們能夠更有效地發(fā)揮多態(tài)特性。抽象類聲明了一組派生類共同操作接口的通用語義,而接口的完整實(shí)現(xiàn),即純虛函數(shù)的函數(shù)體,要由派生類自己給出。但抽象類的派生類并非一定要給出純虛函數(shù)的實(shí)現(xiàn),如

2、果派生類沒有給出純虛函數(shù)的實(shí)現(xiàn),這個(gè)派生類仍然是一個(gè)抽象類。8-3 聲明一個(gè)參數(shù)為整型,無返回值,名為fn1的虛函數(shù)。解:virtual void fn1( int );8-4 在C+中,能否聲明虛構(gòu)造函數(shù)?為什么?能否聲明虛析構(gòu)函數(shù)?有何用途?解:在C+中,不能聲明虛構(gòu)造函數(shù),多態(tài)是不同的對象對同一消息有不同的行為特性,虛函數(shù)作為運(yùn)行過程中多態(tài)的基礎(chǔ),主要是針對對象的,而構(gòu)造函數(shù)是在對象產(chǎn)生之前運(yùn)行的,因此虛構(gòu)造函數(shù)是沒有意義的;可以聲明虛析構(gòu)函數(shù),析構(gòu)函數(shù)的功能是在該類對象消亡之前進(jìn)行一些必要的清理工作,如果一個(gè)類的析構(gòu)函數(shù)是虛函數(shù),那么,由它派生而來的所有子類的析構(gòu)函數(shù)也是虛函數(shù)。析構(gòu)函

3、數(shù)設(shè)置為虛函數(shù)之后,在使用指針引用時(shí)可以動(dòng)態(tài)聯(lián)編,實(shí)現(xiàn)運(yùn)行時(shí)的多態(tài),保證使用基類的指針就能夠調(diào)用適當(dāng)?shù)奈鰳?gòu)函數(shù)針對不同的對象進(jìn)行清理工作。8-5 實(shí)現(xiàn)重載函數(shù)Double(x),返回值為輸入?yún)?shù)的兩倍;參數(shù)分別為整型、長整型、浮點(diǎn)型、雙精度型,返回值類型與參數(shù)一樣。解:源程序:#include <iostream.h>int Double(int);long Double(long);float Double(float);double Double(double);int main()int myInt = 6500;long myLong = 65000;float myFlo

4、at = 6.5F;double myDouble = 6.5e20;int doubledInt;long doubledLong;float doubledFloat;double doubledDouble;cout << "myInt: " << myInt << "n"cout << "myLong: " << myLong << "n"cout << "myFloat: " << myFl

5、oat << "n"cout << "myDouble: " << myDouble << "n"doubledInt = Double(myInt);doubledLong = Double(myLong);doubledFloat = Double(myFloat);doubledDouble = Double(myDouble);cout << "doubledInt: " << doubledInt << "n&q

6、uot;cout << "doubledLong: " << doubledLong << "n"cout << "doubledFloat: " << doubledFloat << "n"cout << "doubledDouble: " << doubledDouble << "n"return 0;int Double(int original)cout <

7、;< "In Double(int)n"return 2 * original;long Double(long original)cout << "In Double(long)n"return 2 * original;float Double(float original)cout << "In Double(float)n"return 2 * original;double Double(double original)cout << "In Double(double)n

8、"return 2 * original;程序運(yùn)行輸出:myInt: 6500myLong: 65000myFloat: 6.5myDouble: 6.5e+20In Double(int)In Double(long)In Double(float)In Double(double)DoubledInt: 13000DoubledLong: 130000DoubledFloat: 13DoubledDouble: 1.3e+218-6 定義一個(gè)Rectangle類,有長itsWidth、寬itsLength等屬性,重載其構(gòu)造函數(shù)Rectangle()和Rectangle(int w

9、idth, int length)。解:源程序:#include <iostream.h>class Rectanglepublic:Rectangle();Rectangle(int width, int length);Rectangle() int GetWidth() const return itsWidth; int GetLength() const return itsLength; private:int itsWidth;int itsLength;Rectangle:Rectangle()itsWidth = 5;itsLength = 10;Rectangl

10、e:Rectangle (int width, int length)itsWidth = width;itsLength = length;int main()Rectangle Rect1;cout << "Rect1 width: " << Rect1.GetWidth() << endl;cout << "Rect1 length: " << Rect1.GetLength() << endl;int aWidth, aLength;cout << "E

11、nter a width: "cin >> aWidth;cout << "nEnter a length: "cin >> aLength;Rectangle Rect2(aWidth, aLength);cout << "nRect2 width: " << Rect2.GetWidth() << endl;cout << "Rect2 length: " << Rect2.GetLength() << endl;

12、return 0;程序運(yùn)行輸出:Rect1 width: 5Rect1 length: 10Enter a width: 20Enter a length: 50Rect2 width: 20Rect2 length: 508-7 定義計(jì)數(shù)器Counter類,對其重載運(yùn)算符 + 。解:源程序:typedef unsigned short USHORT;#include <iostream.h>class Counterpublic:Counter();Counter(USHORT initialValue);Counter()USHORT GetItsVal()const retu

13、rn itsVal; void SetItsVal(USHORT x) itsVal = x; Counter operator+ (const Counter &);private:USHORT itsVal;Counter:Counter(USHORT initialValue):itsVal(initialValue)Counter:Counter():itsVal(0)Counter Counter:operator+ (const Counter & rhs)return Counter(itsVal + rhs.GetItsVal();int main()Count

14、er varOne(2), varTwo(4), varThree;varThree = varOne + varTwo;cout << "varOne: " << varOne.GetItsVal()<< endl;cout << "varTwo: " << varTwo.GetItsVal() << endl;cout << "varThree: " << varThree.GetItsVal() << endl;retur

15、n 0;程序運(yùn)行輸出:varOne: 2varTwo: 4varThree: 68-8 定義一個(gè)哺乳動(dòng)物Mammal類,再由此派生出狗Dog類,二者都定義 Speak()成員函數(shù),基類中定義為虛函數(shù),定義一個(gè)Dog類的對象,調(diào)用Speak函數(shù),觀察運(yùn)行結(jié)果。解:源程序:#include <iostream.h>class Mammalpublic:Mammal():itsAge(1) cout << "Mammal constructor.n" Mammal() cout << "Mammal destructor.n"

16、; virtual void Speak() const cout << "Mammal speak!n" ;class Dog : public Mammalpublic:Dog() cout << "Dog Constructor.n" Dog() cout << "Dog destructor.n" void Speak()const cout << "Woof!n" ;int main()Mammal *pDog = new Dog;pDog->Spea

17、k();return 0;程序運(yùn)行輸出:Mammal constructor.Dog constructor.Woof!Dog destructor.Mammal destructor.8-9 定義一個(gè)Shape抽象類,在此基礎(chǔ)上派生出Rectangle和Circle,二者都有GetArea()函數(shù)計(jì)算對象的面積,GetPerim()函數(shù)計(jì)算對象的周長。解:源程序:#include <iostream.h>class Shapepublic:Shape()Shape()virtual float GetArea() =0 ;virtual float GetPerim () =0

18、;class Circle : public Shapepublic:Circle(float radius):itsRadius(radius)Circle()float GetArea() return 3.14 * itsRadius * itsRadius; float GetPerim () return 6.28 * itsRadius; private:float itsRadius;class Rectangle : public Shapepublic:Rectangle(float len, float width): itsLength(len), itsWidth(wi

19、dth);Rectangle();virtual float GetArea() return itsLength * itsWidth; float GetPerim () return 2 * itsLength + 2 * itsWidth; virtual float GetLength() return itsLength; virtual float GetWidth() return itsWidth; private:float itsWidth;float itsLength;void main()Shape * sp;sp = new Circle(5);cout <

20、< "The area of the Circle is " << sp->GetArea () << endl;cout << "The perimeter of the Circle is " << sp->GetPerim () << endl;delete sp;sp = new Rectangle(4,6);cout << "The area of the Rectangle is " << sp->GetArea()

21、 << endl;cout << "The perimeter of the Rectangle is " << sp->GetPerim () << endl;delete sp;程序運(yùn)行輸出:The area of the Circle is 78.5The perimeter of the Circle is 31.4The area of the Rectangle is 24The perimeter of the Rectangle is 208-10 對Point類重載+(自增)、-(自減)運(yùn)算符解:#in

22、clude <iostream.h>class Pointpublic:Point& operator+();Point operator+(int);Point& operator-();Point operator-(int);Point() _x = _y = 0; int x() return _x; int y() return _y; private:int _x, _y;Point& Point:operator+()_x+;_y+;return *this;Point Point:operator+(int)Point temp = *thi

23、s;+*this;return temp;Point& Point:operator-()_x-;_y-;return *this;Point Point:operator-(int)Point temp = *this;-*this;return temp;void main()Point A;cout << "A的值為:" << A.x() << " , " << A.y() << endl;A+;cout << "A的值為:" << A.

24、x() << " , " << A.y() << endl;+A;cout << "A的值為:" << A.x() << " , " << A.y() << endl;A-;cout << "A的值為:" << A.x() << " , " << A.y() << endl;-A;cout << "A的值為:"

25、 << A.x() << " , " << A.y() << endl;程序運(yùn)行輸出:A的值為:0 , 0A的值為:1 , 1A的值為:2 , 2A的值為:1 , 1A的值為:0 , 08-11 定義一個(gè)基類BaseClass,從它派生出類DerivedClass,BaseClass有成員函數(shù)fn1()、fn2(),fn1()是虛函數(shù),DerivedClass也有成員函數(shù)fn1()、fn2(),在主程序中定義一個(gè)DerivedClass的對象,分別用BaseClass和DerivedClass的指針來調(diào)用fn1()、fn2()

26、,觀察運(yùn)行結(jié)果。解:#include <iostream.h>class BaseClasspublic:virtual void fn1();void fn2();void BaseClass:fn1()cout << "調(diào)用基類的虛函數(shù)fn1()" << endl;void BaseClass:fn2()cout << "調(diào)用基類的非虛函數(shù)fn2()" << endl;class DerivedClass : public BaseClasspublic:void fn1();void fn

27、2();void DerivedClass:fn1()cout << "調(diào)用派生類的函數(shù)fn1()" << endl;void DerivedClass:fn2()cout << "調(diào)用派生類的函數(shù)fn2()" << endl;void main()DerivedClass aDerivedClass;DerivedClass *pDerivedClass = &aDerivedClass;BaseClass *pBaseClass = &aDerivedClass;pBaseClass-&

28、gt;fn1();pBaseClass->fn2();pDerivedClass->fn1();pDerivedClass->fn2();程序運(yùn)行輸出:調(diào)用派生類的函數(shù)fn1()調(diào)用基類的非虛函數(shù)fn2()調(diào)用派生類的函數(shù)fn1()調(diào)用派生類的函數(shù)fn2()8-12 定義一個(gè)基類BaseClass,從它派生出類DerivedClass,BaseClass中定義虛析構(gòu)函數(shù),在主程序中將一個(gè)DerivedClass的對象地址賦給一個(gè)BaseClass的指針,觀察運(yùn)行過程。解:#include <iostream.h>class BaseClass public:vir

29、tual BaseClass() cout << "BaseClass()" << endl;class DerivedClass : public BaseClass public:DerivedClass() cout << "DerivedClass()" << endl;void main()BaseClass* bp = new DerivedClass;delete bp;程序運(yùn)行輸出:DerivedClass()BaseClass()8-13 定義Point類,有成員變量X、Y,為其定義友元函

30、數(shù)實(shí)現(xiàn)重載+。解:#include <iostream.h>class Pointpublic:Point() X = Y = 0; Point( unsigned x, unsigned y ) X = x; Y = y; unsigned x() return X; unsigned y() return Y; void Print() cout << "Point(" << X << ", " << Y << ")" << endl; frien

31、d Point operator+( Point& pt, int nOffset );friend Point operator+( int nOffset, Point& pt );private:unsigned X;unsigned Y;Point operator+( Point& pt, int nOffset )Point ptTemp = pt;ptTemp.X += nOffset;ptTemp.Y += nOffset;return ptTemp;Point operator+( int nOffset, Point& pt )Point p

32、tTemp = pt;ptTemp.X += nOffset;ptTemp.Y += nOffset;return ptTemp;void main()Point pt( 10, 10 );pt.Print();pt = pt + 5; / Point + intpt.Print();pt = 10 + pt; / int + Pointpt.Print();程序運(yùn)行輸出:Point(10, 10)Point(15, 15)Point(25, 25)8-14 為某公司設(shè)計(jì)一個(gè)人事管理系統(tǒng),其基本功能為輸入、編輯、查看和保存公司的人事檔案。職工人事檔案包括姓名、性別、出生日期、婚姻狀況、所在部門

33、、職務(wù)和工資。程 序:由于列表框尚未初始化,所以為CEmpDlg類重載OnInitDialog()成員函數(shù)(可使用ClassWizard完成),并添加相應(yīng)代碼:BOOL CEmpDlg:OnInitDialog()CListBox *pLB = (CListBox *)GetDlgItem(IDC_DEPT);pLB->InsertString(-1, "辦公室");pLB->InsertString(-1, "開發(fā)部");pLB->InsertString(-1, "生產(chǎn)部");pLB->InsertStri

34、ng(-1, "銷售部");pLB->InsertString(-1, "人事部");return CDialog:OnInitDialog();其中GetDlgItem()為對話框類的成員函數(shù),用于取對話框控件的指針。為項(xiàng)目添加有關(guān)自定義的職工類CEmployee。選擇Developer Studio菜單的Insert/New Class選項(xiàng),調(diào)出New Class對話框。在Class Type組合框中選擇Generic(普通類),填寫類名CEmployee,在對話框下方的Base class (es)框中輸入基類CObject。在Workspa

35、ce窗口的Class View中選擇生成的CEmployee類的定義,添加代碼:class CEmployee : public CObjectDECLARE_SERIAL(CEmployee)public:CString m_strName; / 姓名int m_nSex; / 性別COleDateTime m_tBirthdate; / 出生日期BOOL m_bMarried; / 婚否CString m_strDept; / 工作部門CString m_strPosition; / 職務(wù)float m_fSalary; / 工資CEmployee()CEmployee& oper

36、ator = (CEmployee& e);virtual CEmployee();virtual void Serialize(CArchive &ar);CEmployee類的對象即為一個(gè)職工的檔案,我們用序列化實(shí)現(xiàn)文檔的存取,所以要為CEmployee類編寫序列化代碼。這包括DECLARE_SERIAL()宏和IMPLEMENT_SERIAL()宏(在CEmployee類的源代碼文件中),一個(gè)沒有參數(shù)的構(gòu)造函數(shù),重載的賦值運(yùn)算符和Serialize()成員函數(shù)。在CEmployee類的源代碼文件中添加以下代碼:IMPLEMENT_SERIAL(CEmployee, COb

37、ject, 1)/ 重載的賦值運(yùn)算符CEmployee& CEmployee:operator = (CEmployee& e)m_strName = e.m_strName;m_nSex = e.m_nSex;m_tBirthdate = e.m_tBirthdate;m_bMarried = e.m_bMarried;m_strDept = e.m_strDept;m_strPosition = e.m_strPosition;m_fSalary = e.m_fSalary;return *this;/ 序列化函數(shù)void CEmployee:Serialize(CArch

38、ive& ar)CObject:Serialize(ar);if(ar.IsStoring()ar << m_strName;ar << m_nSex;ar << m_tBirthdate;ar << m_bMarried;ar << m_strDept;ar << m_strPosition;ar << m_fSalary;elsear >> m_strName;ar >> m_nSex;ar >> m_tBirthdate;ar >> m_bMarri

39、ed;ar >> m_strDept;ar >> m_strPosition;ar >> m_fSalary;然后修改文檔類CMyDocument類定義,添加一個(gè)CEmployee類的數(shù)組:#include "employee.h"#define MAX_EMPLOYEE 1000class CMy1501Doc : public CDocumentDECLARE_DYNCREATE(CMy1501Doc)public:CEmployee m_empListMAX_EMPLOYEE;int m_nCount;public:virtual

40、BOOL OnNewDocument();virtual void Serialize(CArchive& ar);virtual void DeleteContents();DECLARE_MESSAGE_MAP();為了節(jié)省篇幅,這段程序經(jīng)過刪節(jié),與原來由AppWizard生成的程序有所不同。其中黑體部分為要添加的代碼。注意重載成員函數(shù)DeleteContents()可以手工進(jìn)行,也可以通過ClassWizard進(jìn)行。Serialize()和DeleteContents()兩個(gè)成員函數(shù)的代碼如下:void CMy1501Doc:Serialize(CArchive& ar)

41、if(ar.IsStoring()ar << m_nCount;elsear >> m_nCount;for(int i=0; i<m_nCount; i+)m_empListi.Serialize(ar);void CMy1501Doc:DeleteContents()m_nCount = 0; / 在打開文件和建立新文件時(shí)將數(shù)組大小置0CDocument:DeleteContents();即在文檔類的Serialize()函數(shù)中,數(shù)據(jù)的序列化工作是通過調(diào)用Cemployee類的Serialize()函數(shù)實(shí)現(xiàn)的。實(shí)際上,要為本程序添加的大部分代碼均在視圖類中。首

42、先在視圖類CmyView類的定義中添加一個(gè)用于記錄當(dāng)前操作的是哪個(gè)記錄的數(shù)據(jù)成員:int m_nCurrEmp;并為視圖類重載OnInitialUpdate()成員函數(shù),在其中初始化該變量:void CMy1501View:OnInitialUpdate()CView:OnInitialUpdate();m_nCurrEmp = 0;Invalidate();視圖類的OnDraw()成員函數(shù)用于顯示正在操作的職工檔案:void CMy1501View:OnDraw(CDC* pDC)CMy1501Doc* pDoc = GetDocument();ASSERT_VALID(pDoc);/ 顯示

43、職工人數(shù)和當(dāng)前職工編號CString s;s.Format("職工人數(shù): %d", pDoc->m_nCount);pDC->SetTextColor(RGB(255, 0, 0);pDC->TextOut( 40, 40, s);s.Format("職工編號: %d", m_nCurrEmp+1);pDC->TextOut(340, 40, s);pDC->MoveTo( 40, 70);pDC->LineTo(600, 70);/ 如果檔案非空, 顯示當(dāng)前記錄if(pDoc->m_nCount > 0)

44、/ 顯示欄目名稱pDC->SetTextColor(RGB(0, 0, 0);pDC->TextOut(140, 90, "姓 名:");pDC->TextOut(140, 130, "性 別:");pDC->TextOut(140, 170, "出生日期: ");pDC->TextOut(140, 210, "婚姻狀態(tài):");pDC->TextOut(140, 250, "部 門:");pDC->TextOut(140, 290, "職 務(wù):

45、");pDC->TextOut(140, 330, "工 資:");/ 顯示欄目內(nèi)容pDC->SetTextColor(RGB(0, 0, 255);pDC->TextOut(300, 90, pDoc->m_empListm_nCurrEmp.m_strName);if(pDoc->m_empListm_nCurrEmp.m_nSex=0)pDC->TextOut(300, 130, "男");elsepDC->TextOut(300, 130, "女");s = pDoc->

46、;m_empListm_nCurrEmp.m_tBirthdate.Format("%Y.%m.%d");pDC->TextOut(300, 170, s);if(pDoc->m_empListm_nCurrEmp.m_bMarried)pDC->TextOut(300, 210, "已婚");elsepDC->TextOut(300, 210, "未婚");pDC->TextOut(300, 250, pDoc->m_empListm_nCurrEmp.m_strDept);pDC->Tex

47、tOut(300, 290, pDoc->m_empListm_nCurrEmp.m_strPosition);s.Format("%8.2f", pDoc->m_empListm_nCurrEmp.m_fSalary);pDC->TextOut(300, 330, s);在編輯資源時(shí),我們框架窗口添加了5個(gè)菜單選項(xiàng),并將對應(yīng)的消息響應(yīng)函數(shù)映射到了視圖類中。這些消息響應(yīng)函數(shù)的代碼如下:void CMy1501View:OnAppend()CMy1501Doc* pDoc = GetDocument();ASSERT_VALID(pDoc);CEmpDlg

48、dlg;if(dlg.DoModal() = IDOK)pDoc->m_nCount+;m_nCurrEmp = pDoc->m_nCount-1;pDoc->m_empListm_nCurrEmp.m_strName = dlg.m_strName;pDoc->m_empListm_nCurrEmp.m_nSex = dlg.m_nSex;pDoc->m_empListm_nCurrEmp.m_tBirthdate = dlg.m_tBirthdate;pDoc->m_empListm_nCurrEmp.m_bMarried = dlg.m_bMarrie

49、d;pDoc->m_empListm_nCurrEmp.m_strDept = dlg.m_strDept;pDoc->m_empListm_nCurrEmp.m_strPosition = dlg.m_strPosition;pDoc->m_empListm_nCurrEmp.m_fSalary = dlg.m_fSalary;pDoc->SetModifiedFlag();Invalidate();/ 刪除當(dāng)前記錄void CMy1501View:OnDelete()CMy1501Doc* pDoc = GetDocument();ASSERT_VALID(pDoc

50、);if(pDoc->m_nCount)for(int i=m_nCurrEmp; i<pDoc->m_nCount-1; i+)pDoc->m_empListi = pDoc->m_empListi+1;pDoc->m_nCount-;if(m_nCurrEmp > pDoc->m_nCount-1)m_nCurrEmp = pDoc->m_nCount-1;pDoc->SetModifiedFlag();Invalidate();/ 編輯當(dāng)前記錄void CMy1501View:OnEdit()CMy1501Doc* pDoc =

51、 GetDocument();ASSERT_VALID(pDoc);if(pDoc->m_nCount)CEmpDlg dlg;dlg.m_strName = pDoc->m_empListm_nCurrEmp.m_strName;dlg.m_nSex = pDoc->m_empListm_nCurrEmp.m_nSex;dlg.m_tBirthdate = pDoc->m_empListm_nCurrEmp.m_tBirthdate;dlg.m_bMarried = pDoc->m_empListm_nCurrEmp.m_bMarried;dlg.m_strDe

52、pt = pDoc->m_empListm_nCurrEmp.m_strDept;dlg.m_strPosition = pDoc->m_empListm_nCurrEmp.m_strPosition;dlg.m_fSalary = pDoc->m_empListm_nCurrEmp.m_fSalary;if(dlg.DoModal() = IDOK)pDoc->m_empListm_nCurrEmp.m_strName = dlg.m_strName;pDoc->m_empListm_nCurrEmp.m_nSex = dlg.m_nSex;pDoc->m

53、_empListm_nCurrEmp.m_tBirthdate = dlg.m_tBirthdate;pDoc->m_empListm_nCurrEmp.m_bMarried = dlg.m_bMarried;pDoc->m_empListm_nCurrEmp.m_strDept = dlg.m_strDept;pDoc->m_empListm_nCurrEmp.m_strPosition = dlg.m_strPosition;pDoc->m_empListm_nCurrEmp.m_fSalary = dlg.m_fSalary;pDoc->SetModifie

54、dFlag();Invalidate();/ 查看后一記錄void CMy1501View:OnNext()CMy1501Doc* pDoc = GetDocument();ASSERT_VALID(pDoc);if(pDoc->m_nCount > 1)if(m_nCurrEmp = pDoc->m_nCount-1)m_nCurrEmp = 0;elsem_nCurrEmp+;Invalidate();/ 查看前一記錄void CMy1501View:OnPrev()CMy1501Doc* pDoc = GetDocument();ASSERT_VALID(pDoc);i

55、f(pDoc->m_nCount > 1)if(m_nCurrEmp = 0)m_nCurrEmp = pDoc->m_nCount-1;elsem_nCurrEmp-;Invalidate();8-15 用基于對話框的應(yīng)用程序結(jié)構(gòu)實(shí)現(xiàn)例14-5的彩色吹泡泡程序。由于對話框本身結(jié)構(gòu)簡單,沒有明顯的客戶區(qū),顏色也不醒目,所以我們在對話框上自行建立一個(gè)矩形區(qū)域作為吹泡泡的客戶區(qū),并通過一個(gè)“顏色設(shè)置”按鈕來設(shè)置泡泡的顏色。說 明:用AppWizard建立一個(gè)基于對話框的應(yīng)用程序框架(參看15.4:“用AppWizard生成基于對話框的應(yīng)用程序”),所有設(shè)置均使用缺省值。使用對話框

56、模板編輯器編輯作為主界面窗口的對話框模板,將其上的靜態(tài)文本控件和“Cancel”按鈕刪除,將“OK”按鈕的Caption設(shè)置為“完成”,并將對話框大小調(diào)整為400×300左右。為對話框模板添加一個(gè)Picture控件,將其Type設(shè)置為Frame,Color設(shè)置為Black,并設(shè)置Sunken屬性(在Styles選項(xiàng)卡中)。調(diào)整其位置為(7,7),大小為287×287。這個(gè)框中即為自定義的吹泡泡客戶區(qū),所有的吹泡泡活動(dòng)均在該區(qū)域中進(jìn)行。為對話框模板添加一個(gè)按鈕,將其ID改為IDC_COLOR,Caption改為“顏色設(shè)置”。使用ClassWizard為對話框類添加一個(gè)鼠標(biāo)左鍵

57、消息響應(yīng)函數(shù)OnLButtonDown()和一個(gè)按鈕命令消息響應(yīng)函數(shù)OnColor()。程 序:在對話框類的頭文件前面添加一行:#define MAX_BUBBLE 250并在對話框類定義中添加存放泡泡的幾何參數(shù)和顏色的數(shù)組數(shù)據(jù)成員:CRect m_rectBubbleMAX_BUBBLE;COLORREF m_colorBubbleMAX_BUBBLE;int m_nBubbleCount;以及一個(gè)存放自定義客戶區(qū)矩形的數(shù)據(jù)成員和一個(gè)存放當(dāng)前泡泡顏色設(shè)置的數(shù)據(jù)成員:CRect m_rectClient;COLORREF m_colorCurrent;修改對話框類的OnInitDialog()

58、成員函數(shù),添加計(jì)算自定義客戶區(qū)位置和大小的代碼,并將泡泡的數(shù)目初始化為0:BOOL CMyDlg:OnInitDialog()CDialog:OnInitDialog();CStatic *pST = (CStatic *)GetDlgItem(IDC_CLIENT);pST->GetWindowRect(&m_rectClient);ScreenToClient(&m_rectClient);m_nBubbleCount = 0;return TRUE;修改OnPaint()成員函數(shù),添加畫出泡泡的有關(guān)代碼:void CMyDlg:OnPaint()CPaintDC dc(this);CRgn rgn;rgn.CreateRectRgnIndirect(&m_rectClient); / 生成一個(gè)區(qū)域?qū)ο骴c.SelectClipRgn(&rgn); / 選擇區(qū)域dc.Rectangle(m_rectClient); / 將客戶區(qū)背景設(shè)置CBrush brushNew, *pbrushOld; / 白色for(int i=0; i<m_nBubbleCount; i+)brushNe

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論