为了正常的体验网站,请在浏览器设置里面开启Javascript功能!

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

2017-12-22 35页 doc 61KB 40阅读

用户头像

is_842972

暂无简介

举报
李爱华、程磊_面向对象程序设计课后答案李爱华、程磊_面向对象程序设计课后答案 第二章 2-4 include # using namespace std; Add(int a,int b); int main() { int x,y,sum; cout>x>>y; sum = add(x,y); cout using namespace std; int main() { int *p,*init; int countp=0; int countn=0; p = new int[20]; init = p; for(int i...
李爱华、程磊_面向对象程序设计课后答案
李爱华、程磊_面向对象程序课后答案 第二章 2-4 include # using namespace std; Add(int a,int b); int main() { int x,y,sum; cout<<"please input x and y:"; cin>>x>>y; sum = add(x,y); cout < using namespace std; int main() { int *p,*init; int countp=0; int countn=0; p = new int[20]; init = p; for(int i=0;i<20;i++) { cin>>*p; p++; } p = p-20; for( i=0;i<20;i++) { if(*p>0) countp++; if(*p<0) countn++; cout<<*p<<" "; p++; } cout<<"正数有:"< //#include using namespace std; void checkagescore(string name,int age) { if (name == "exit") throw name; if(age<0||age>50) throw age; } int main() { string name; int age; for(int i=0 ;i<5 ;i++ ) { cin.ignore (); getline(cin,name ); cin>>age ; try { checkagescore(name,age); } catch( string) { cout<<"exception :name is exit"< //#include using namespace std; class Ctest { private: int x; const int y1; public: const int y2; Ctest(int i1,int 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<<"constructor C:";} 运行结果为: constructor A constructor B constructor C (2) 40 (3) 3 4 3 4-9略 4-10 #include #include class Student { int number; char name[20]; public: Student(int i=0,char *s="\0") //构造学生对象 { number=i; strcpy(name,s); } void Print() //输出结果 { cout<<"Number:"<st2.number; //返回成员number的比较结果 } int main() { Student st[5]={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<5;i++) { if(!greaterthan(st[max],st[i])) max = i; if(!greaterthan(st[i],st[min])) min = i; } cout<<"最大成绩:"< #include 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 char[strlen(a)+1]; strcpy(name,a); author = new char[strlen(b)+1]; strcpy(author,b); sale = c; } void print() {cout<<"autor "< using namespace std; class Shape { int x,y; public: Shape(int ix,int iy){x = ix; y = iy;} virtual void show(){cout<<"pos: "< using namespace std; class Mammal { public: virtual void Speak() {cout<<"in Mammal"<Speak (); return 0; } 运行结果: dog bark 6-7 #include using namespace std; class BaseClass { public: virtual ~BaseClass() {cout<<"destruct Base"< using namespace std; class Shape { public: 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 * pS[3]; pS[1] = new Circle(1); pS[2] = new Square(2); areaSum += pS[1]->area (); areaSum += pS[2]->area (); cout< using namespace std; class Student { public: virtual void show() = 0; }; class Junior :public Student { public: void show(){cout<<"this would be info for junior students"<show (); pstu = new Senior; pstu->show (); return 0; } 第七章 7-1 #include #include using namespace 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.0) { real =x; image =y; } bool operator !=(const Complex &c); Complex operator -(const Complex &c); bool operator ==(const Complex &c); Complex operator -(); Complex &operator +=(const Complex &c); 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 Date { private: int month, day, year; public: Date(int m, int d, int y); Date& operator+(int days); void showDate(); }; Date::Date(int y, int m, int d) { if (m>0 && m<13) month=m; if (d>0 && d<32) day=d; if (y>0 && y<3000) year=y; } Date& Date::operator+(int days) { int i; for(i = days;i>0;) { 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 -day;break; case 2:if (rn(year))diff = 29 - day; else diff = 28 -day; break; } if(i>diff) { i-= diff+1; day = 1; month++; if(month>12) { 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; sbuf=new char; sbuf[0]='\0'; } String(char *s) //用字符串初始化 { length=strlen(s); sbuf=new char[length+1]; strcpy(sbuf,s); } String (String& str) { length=str.length ; sbuf=new char[length+1]; strcpy(sbuf,str.sbuf ); } ~String() { delete[] sbuf; } String & operator =(String& str) { if(sbuf == str.sbuf ) return *this; else { sbuf = new char[str.length +1]; strcpy(sbuf,str.sbuf ); return *this; } } String operator +(String& str)//此函数需配合拷贝构造函数 { String st; st.length = length + str.length ; st.sbuf = new char[st.length+1 ]; st.sbuf [0] = '\0'; strcpy(st.sbuf,sbuf); strcat(st.sbuf,str.sbuf ); return st; } char & operator[](int i) { if(i<0||i>=length) //对下标进行检查,超出范围则报错退出程序 { 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=a>b?a:b; temp=temp>c?temp:c; return temp; } int main() { cout< #include using namespace std; template void sort_bubble(T arr[],int n) { for(int i=0; i #include using namespace std; int main() { fstream fs; ofstream fsc("d:\\lqing01.txt"); fs.open("D:\\lin.txt",ios::in|ios::out ); if(!fs) { cout<<"cant open file"< #include using namespace std; int main() { ofstream of("d:\\01.txt",ios::app);//文件01.txt应已存在 ifstream ifs("d:\\02.txt"); if(!of || !ifs) { cout<<"cant open file"; exit(-1); } char ch; while(ifs>>ch) { of<
/
本文档为【李爱华、程磊_面向对象程序设计课后答案】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。

历史搜索

    清空历史搜索