李爱华、程磊_面向对象程序设计课后答案

上传人:飞*** 文档编号:26893587 上传时间:2018-01-03 格式:PDF 页数:54 大小:236.11KB
返回 下载 相关 举报
李爱华、程磊_面向对象程序设计课后答案_第1页
第1页 / 共54页
李爱华、程磊_面向对象程序设计课后答案_第2页
第2页 / 共54页
李爱华、程磊_面向对象程序设计课后答案_第3页
第3页 / 共54页
李爱华、程磊_面向对象程序设计课后答案_第4页
第4页 / 共54页
李爱华、程磊_面向对象程序设计课后答案_第5页
第5页 / 共54页
点击查看更多>>
资源描述

《李爱华、程磊_面向对象程序设计课后答案》由会员分享,可在线阅读,更多相关《李爱华、程磊_面向对象程序设计课后答案(54页珍藏版)》请在金锄头文库上搜索。

1、第二章2-4 #include using namespace std; Add(int a,int b); int main() int x,y,sum; coutxy; sum = add(x,y); cout using namespace std; int main() int *p,*init; int countp=0; int countn=0; p = new int20; init = p; for(int i=0;i*p; p+; p = p-20; for( i=0;i0) countp+; if(*p /#include using namespace std; voi

2、d checkagescore(string name,int age) if (name = exit) throw name; if(age50) throw age; int main() string name; int age; for(int i=0 ;iage ; try checkagescore(name,age); catch( string) cout /#include using namespace std; class Ctest private: int x; const int y1; public: const int y2; Ctest(int i1,int

3、 i2):y1(i1),y2(i2) y1 =10;/y1 为常量不能赋值x = y1; int readme() const; ; int Ctest:readme ()const int i; i = x; x+; /常函数内不能改变成员值return x; int main() Ctest c(2,8); int i = c.y2; c.y2 = i;/y2 为常量 ,不能改值i = c.y1;/y1 私有 ,类外不能访问return 0; 将出错语句全部注释4-8 (1) 题中印刷错误,将 class C构造函数改为:C() cout #include class Student in

4、t number; char name20; public: Student(int i=0,char *s=0) /构造学生对象 number=i; strcpy(name,s); void Print() /输出结果 coutst2.number; / 返 回 成 员number 的比较结果 int main() Student st5=Student(65,Li),Student(78,Zhang),Student(80,wang),Student(92,zhao),Student(50,zhen); int max = 0; int min = 0; for(int i=1;i #in

5、clude using namespace std; class Book char *name; char*author; int sale; public: Book() name = 0; author = 0; sale = -1; Book(char* a ,char* b,int c) name = new charstrlen(a)+1; strcpy(name,a); author = new charstrlen(b)+1; strcpy(author,b); sale = c; void print() cout using namespace std; class Sha

6、pe int x,y; public: Shape(int ix,int iy)x = ix; y = iy; virtual void show()cout using namespace std; class Mammal public: virtual void Speak() coutSpeak (); return 0; 运行结果 : dog bark 6-7 #include using namespace std; class BaseClass public: virtual BaseClass() cout using namespace std; class Shape p

7、ublic: virtual float area() = 0; ; class Circle :public Shape int radius; public: Circle(int r):radius(r) float area() return 3.14*radius*radius; ; class Square :public Shape int width; public: Square(int w):width(w) float area() return width*width; ; int main() float areaSum= 0; Shape * pS3; pS1 =

8、new Circle(1); pS2 = new Square(2); areaSum += pS1-area (); areaSum += pS2-area (); cout using namespace std; class Student public: virtual void show() = 0; ; class Junior :public Student public: void show()coutshow (); pstu = new Senior; pstu-show (); return 0; 第七章7-1 #include #include using namesp

9、ace std; class vector int x,y; public: vector(int ix=0,int iy=0)x =ix;y = iy; vector operator+(vector& v1) return vector(x+v1.x,y+v1.y); vector& operator+=(vector& v1) x +=v1.x; y +=v1.y; return *this; void show() cout class Complex private: double real,image; public: Complex(double x=0.0,double y=0

10、.0) real =x; image =y; bool operator !=(const Complex Complex operator -(const Complex bool operator =(const Complex Complex operator -(); Complex void Print(); ; void Complex:Print() cout using namespace std; bool rn(int y) bool flag = 0; if(y%400=0 | y%4 =0&y%100!=0) flag = 1; return flag; class D

11、ate private: int month, day, year; public: Date(int m, int d, int y); Date void showDate(); ; Date:Date(int y, int m, int d) if (m0 & m0 & d0 & y0;) int diff ; switch(month) case 1: case 3: case 5: case 7: case 8: case 10: case 12: diff = 31 -day;break; case 4: case 6: case 9: case 11: diff = 30 -da

12、y;break; case 2:if (rn(year)diff = 29 - day; else diff = 28 -day; break; if(idiff) i-= diff+1; day = 1; month+; if(month12) year+; month = 1; else day+= i; break; return *this; void Date:showDate() cout #include using namespace std; class String char *sbuf; int length; public: String() length=0; sbu

13、f=new char; sbuf0=0; String(char *s) /用字符串初始化 length=strlen(s); sbuf=new charlength+1; strcpy(sbuf,s); String (String& str) length=str.length ; sbuf=new charlength+1; strcpy(sbuf,str.sbuf ); String() delete sbuf; String & operator =(String& str) if(sbuf = str.sbuf ) return *this; else sbuf = new cha

14、rstr.length +1; strcpy(sbuf,str.sbuf ); return *this; String operator +(String& str)/此函数需配合拷贝构造函数 String st; st.length = length + str.length ; st.sbuf = new charst.length+1 ; st.sbuf 0 = 0; strcpy(st.sbuf,sbuf); strcat(st.sbuf,str.sbuf ); return st; char & operator(int i) if(i=length) /对下标进行检查,超出范围则

15、报错退出程序 cout T fun(T a) . (2) template T test(T a) . (3) template class Array public: void fun(); template void Array:fun() . (4)Array a1,a2,a3; 8-4 #include using namespace std; template T max(T a,T b,T c) T temp; temp=ab?a:b; temp=tempc?temp:c; return temp; int main() cout #include using namespace std; template void sort_bubble(T arr,

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 研究报告 > 综合/其它

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号