收藏 分享(赏)

新标准C++程序设计教材答案 郭炜.pdf

上传人:精品资料 文档编号:10351463 上传时间:2019-11-03 格式:PDF 页数:59 大小:1.06MB
下载 相关 举报
新标准C++程序设计教材答案 郭炜.pdf_第1页
第1页 / 共59页
新标准C++程序设计教材答案 郭炜.pdf_第2页
第2页 / 共59页
新标准C++程序设计教材答案 郭炜.pdf_第3页
第3页 / 共59页
新标准C++程序设计教材答案 郭炜.pdf_第4页
第4页 / 共59页
新标准C++程序设计教材答案 郭炜.pdf_第5页
第5页 / 共59页
点击查看更多>>
资源描述

1、新标准 C+程序设计习题解答 第 11 章 -第 20 章 郭炜 第十一章习题 1. 结构化程序设计有什么不足?面向对象的程序设计如何改进这些不足? 2. 以下说法正确的是: A) 每个对象内部都有成员函数的实现代码 B) 一个类的私有成员函数内部不能访问本类的私有成员变量 C) 类的成员函数之间可以互相调用 D) 编写一个类时,至少要写一个成员函数 #C 3. 以下对类 A 的定义,哪个是正确的? A) class A private: int v; public : void Func() B) class A int v; A * next; void Func() ; C) class

2、 A int v; public: void Func(); ; A:void Func() D)class A int v; public: A next; void Func() ; #B 4. 假设有以下类 A: class A public: int func(int a) return a * a; ; 以下程序片段,哪个是不正确的? A) A a; a.func(5); B) A * p = new A; p-func(5); C) A a; A r.func(5); D) A a,b; if( a != b) a.func(5); #D 5.以下程序, 哪 个是不正确的? A)

3、int main() class A int v; ; A a; a.v = 3; return 0; B) int main() class A public: int v; A * p; ; A a; a.p = return 0; C)int main() class A public: int v; ; A * p = new A; p-v = 4; delete p; return 0; D) int main() class A public: int v; A * p; ; A a; a.p = new A; delete a.p; return 0; #A 6. 实现一个学生信

4、息处理程序 。输入: 姓名,年龄,学号(整数),第一学年平均成绩,第二学年平均成绩,第三学年平均成绩,第四学年平均成绩 。 输出:姓名,年龄,学号,四年平均成绩 。 例如: 输入: Tom,18,7817,80,80,90,70 输出: Tom,18,7817,80 要求实现一个代表学生的类,并且所有成员变量都应该是私有的。 #include #include #include #include using namespace std; class CStudent private: int age; int id; char name20; int averageScore4; public

5、: int average() int sum = 0; for( int i = 0;i using namespace std; class Sample public: int v; Sample() ; Sample(int n):v(n) ; Sample( const Sample ; Sample PrintAndDouble( Sample o) cout using namespace std; class Sample public: int v; Sample(int n):v(n) ; ; int main() Sample a(5); Sample b = a; co

6、ut using namespace std; class Base public: int k; Base(int n):k(n) ; class Big public: int v; Base b; Big _ Big _ ; int main() Big a1(5); Big a2 = a1; cout #include #include using namespace std; #define WARRIOR_NUM 5 /* char * CWarrior:NamesWARRIOR_NUM = “dragon“,“ninja“,“iceman“,“lion“,“wolf“ ; 红方司

7、令部按照 iceman、 lion、 wolf、 ninja、 dragon 的顺序制造武士。 蓝方司令部按照 lion、 dragon、 ninja、 iceman、 wolf 的顺序制造武士。 */ class CHeadquarter; class CWarrior private: CHeadquarter * pHeadquarter; int nKindNo; /武士的种类编号 0 dragon 1 ninja 2 iceman 3 lion 4 wolf int nNo; public: static char * NamesWARRIOR_NUM; static int Ini

8、tialLifeValue WARRIOR_NUM; CWarrior( CHeadquarter * p,int nNo_,int nKindNo_ ); void PrintResult(int nTime); ; class CHeadquarter private: int nTotalLifeValue; bool bStopped; int nTotalWarriorNum; int nColor; int nCurMakingSeqIdx; /当前要制造的武士是制造序列中的第几个 int anWarriorNumWARRIOR_NUM; /存放每种武士的数量 CWarrior *

9、 pWarriors1000; public: friend class CWarrior; static int MakingSeq2WARRIOR_NUM; /武士的制作顺序序列 void Init(int nColor_, int lv); CHeadquarter () ; int Produce(int nTime); void GetColor( char * szColor); ; CWarrior:CWarrior( CHeadquarter * p,int nNo_,int nKindNo_ ) nNo = nNo_; nKindNo = nKindNo_; pHeadqua

10、rter = p; void CWarrior:PrintResult(int nTime) char szColor20; pHeadquarter-GetColor(szColor); printf(“%03d %s %s %d born with strength %d,%d %s in %s headquartern“ , nTime, szColor, NamesnKindNo, nNo, InitialLifeValuenKindNo, pHeadquarter-anWarriorNumnKindNo,NamesnKindNo,szColor); void CHeadquarter

11、:Init(int nColor_, int lv) nColor = nColor_; nTotalLifeValue = lv; nTotalWarriorNum = 0; bStopped = false; nCurMakingSeqIdx = 0; for( int i = 0;i nTotalLifeValue if( nColor = 0) printf(“%03d red headquarter stops making warriorsn“,nTime); else printf(“%03d blue headquarter stops making warriorsn“,nT

12、ime); return 0; nTotalLifeValue -= CWarrior:InitialLifeValuenKindNo; nCurMakingSeqIdx = ( nCurMakingSeqIdx + 1 ) % WARRIOR_NUM ; pWarriorsnTotalWarriorNum = new CWarrior( this,nTotalWarriorNum+1,nKindNo); anWarriorNumnKindNo+; pWarriorsnTotalWarriorNum-PrintResult(nTime); nTotalWarriorNum +; return

13、1; void CHeadquarter:GetColor( char * szColor) if( nColor = 0) strcpy(szColor,“red“); else strcpy(szColor,“blue“); char * CWarrior:NamesWARRIOR_NUM = “dragon“,“ninja“,“iceman“,“lion“,“wolf“ ; int CWarrior:InitialLifeValue WARRIOR_NUM; int CHeadquarter:MakingSeq2WARRIOR_NUM = 2,3,4,1,0 ,3,0,1,2,4 ; /

14、两个司令部武士的制作顺序序列 int main() int t; int m; CHeadquarter RedHead,BlueHead; scanf(“%d“, int nCaseNo = 1; while ( t - ) printf(“Case:%dn“,nCaseNo+); scanf(“%d“, for( int i = 0;i #include using namespace std; class Complex private: double r,i; public: void Print() cout using namespace std; class MyInt int

15、nVal; public: MyInt( int n) nVal = n ; int ReturnVal() return nVal; ; int main () MyInt objInt(10); objInt-2-1-3; cout using namespace std; class Point private: int x; int y; public: Point(int x_,int y_ ):x(x_),y(y_) ; _; ; _ operator using namespace std; int main() Array2 a(3,4); int i,j; for( i =

16、0;i #include #include #include using namespace std; int CompareString( const void * e1, const void * e2) MyString * s1 = (MyString * ) e1; MyString * s2 = (MyString * ) e2; if( * s1 *s2 ) return 1; main() MyString s1(“abcd-“),s2, s3(“efgh-“),s4(s1); MyString SArray4 = “big“,“me“,“about“,“take“; cout

17、 (const MyString MyString operator + ( const MyString /确保能分配一个数组 strcpy(tmp,str); strcat(tmp,s.str); MyString os(tmp); delete tmp; return os; MyString strcpy( tmp,str); strcat( tmp,s.str); size += s.size; delete str; str = tmp; return * this; char MyString operator()(int start,int len) const char *

18、tmp = new charlen + 1; for( int i = 0;i using namespace std; class B public: B() cout using namespace std; class Base public: int val; Base() cout #include #include using namespace std; /下面这些东西都是常量,而且不止一个类都要用到,就声明为全局的较为简单 #define WARRIOR_NUM 5 #define WEAPON_NUM 3 enum DRAGON,NINJA,ICEMAN,LION,WOLF

19、; /* char * CWarrior:NamesWARRIOR_NUM = “dragon“,“ninja“,“iceman“,“lion“,“wolf“ ; 红方司令部按照 iceman、 lion、 wolf、 ninja、 dragon 的顺序制造武士。 蓝方司令部按照 lion、 dragon、 ninja、 iceman、 wolf 的顺序制造武士。 */ class CHeadquarter; class CDragon; class CNinja; class CIceman; class CLion; class CWolf; class CWeapon; class CW

20、arrior; class CWeapon public: int nKindNo; int nForce; static int InitialForceWEAPON_NUM; static const char * NamesWEAPON_NUM; ; class CWarrior protected: CHeadquarter * pHeadquarter; int nNo; public: static const char * NamesWARRIOR_NUM; static int InitialLifeValue WARRIOR_NUM; CWarrior( CHeadquart

21、er * p,int nNo_): pHeadquarter(p),nNo(nNo_) virtual void PrintResult(int nTime,int nKindNo); virtual void PrintResult(int nTime) = 0; virtual CWarrior() ; class CHeadquarter private: static const int MAX_WARRIORS = 1000; int nTotalLifeValue; bool bStopped; int nColor; int nCurMakingSeqIdx; int anWar

22、riorNumWARRIOR_NUM; int nTotalWarriorNum; CWarrior * pWarriorsMAX_WARRIORS; public: friend class CWarrior; static int MakingSeq2WARRIOR_NUM; void Init(int nColor_, int lv); CHeadquarter () ; int Produce(int nTime); void GetColor( char * szColor); int GetTotalLifeValue() return nTotalLifeValue; ; voi

23、d CWarrior:PrintResult(int nTime,int nKindNo) char szColor20; pHeadquarter-GetColor(szColor); printf(“%03d %s %s %d born with strength %d,%d %s in %s headquartern“ , nTime, szColor, NamesnKindNo, nNo, InitialLifeValuenKindNo, pHeadquarter-anWarriorNumnKindNo,NamesnKindNo,szColor); class CDragon:publ

24、ic CWarrior private: CWeapon wp; double fmorale; public: void Countmorale() fmorale = pHeadquarter - GetTotalLifeValue() /(double)CWarrior:InitialLifeValue 0; CDragon( CHeadquarter * p,int nNo_): CWarrior(p,nNo_) wp.nKindNo = nNo % WEAPON_NUM; wp.nForce = CWeapon:InitialForcewp.nKindNo ; Countmorale

25、(); void PrintResult(int nTime) CWarrior:PrintResult(nTime,DRAGON); printf(“It has a %s,and its morale is %.2fn“, CWeapon:Nameswp.nKindNo, fmorale); ; class CNinja:public CWarrior private: CWeapon wps2; public: CNinja( CHeadquarter * p,int nNo_): CWarrior(p,nNo_) wps0.nKindNo = nNo % WEAPON_NUM; wps

26、0.nForce = CWeapon:InitialForcewps0.nKindNo; wps1.nKindNo = ( nNo + 1) % WEAPON_NUM; wps1.nForce = CWeapon:InitialForcewps1.nKindNo; void PrintResult(int nTime) CWarrior:PrintResult(nTime,NINJA); printf(“It has a %s and a %sn“, CWeapon:Nameswps0.nKindNo, CWeapon:Nameswps1.nKindNo); ; class CIceman:p

27、ublic CWarrior private: CWeapon wp; public: CIceman( CHeadquarter * p,int nNo_): CWarrior(p,nNo_) wp.nKindNo = nNo % WEAPON_NUM; wp.nForce = CWeapon:InitialForce wp.nKindNo ; void PrintResult(int nTime) CWarrior:PrintResult(nTime,ICEMAN); printf(“It has a %sn“, CWeapon:Nameswp.nKindNo); ; class CLion:public CWarrior private: int nLoyalty; public: void CountLoyalty()

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 企业管理 > 管理学资料

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报