logo资料库

C++李爱华、程磊_面向对象程序设计详解.doc

第1页 / 共11页
第2页 / 共11页
第3页 / 共11页
第4页 / 共11页
第5页 / 共11页
第6页 / 共11页
第7页 / 共11页
第8页 / 共11页
资料共11页,剩余部分请下载后查看
第二章 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 <>*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; 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,a ge); } catch( string) { cout<<"exception :na me is exit"< using namespace std; checkagescore(string void name,int age) { if (name == "exit") cout<<"exception :age is not proper"<
cout<<"name:"< #include class Student { int number; void Print() //输出结果 { cout<<"Number:"<st2.number; // 返 回 成 员 number 的比较结果 } int main() { Student st[5]={Student(65,"Li"),Stu dent(78,"Zhang"),Student(8 0,"wang"),Student(92,"zhao "),Student(50,"zhen")}; int max = 0; int min = 0; for(int i=1;i<5;i++)
{ if(!greaterthan(st[max], author = new char[strlen(b)+1]; st[i])) max = i; strcpy(author,b); sale = c; } void print() {cout<<"autor "< #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 = char[strlen(a)+1]; new strcpy(name,a);
第五章 5-8 改错题答案不唯一 (1) class DC { int x; public: DC(){x =100;} }; (2)编译无错,但逻辑错误, 可改为: class BC { protected: int x; public: BC(int i=0){x = i} }; class DC:private BC { public: DC(int i):BC(i){} { int x,y; public: Shape(int ix,int iy){x = ix; y = iy;} virtual show(){cout<<"pos: "< using namespace std; }; class Shape int main() C::print(),cinfo=2 C::print(),cinfo=2 D::print(),dinfo=4 B 类不能定义对象,否则 编译通不过,因为 B 未定 义基类 A 中的虚函数 print(),它也是个虚基类。 6-6 #include using namespace std; class Mammal { public: virtual void Speak() {cout<<"in Mammal"<
bark"<Speak (); return 0; } 运行结果: dog bark 6-7 #include using namespace std; class BaseClass { public: } 结果将不能正常执行子类 析构 6-8 以圆形和正方形为例 #include using namespace std; class Shape { public: virtual float area() = 0; }; class Circle :public Shape { int radius; public: Circle(int r):radius(r){} pS[1] = new Circle(1); new pS[2] Square(2); = areaSum pS[1]->area (); areaSum pS[2]->area (); += += cout< using namespace std; class Student { public: virtual void show() = virtual ~BaseClass() {cout<<"destruct float area() {return 3.14*radius*radius;} 0; }; Base"<
int main() { Student *pstu; pstu = new Junior; pstu->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; } voidshow() { cout<<'('< class Complex { private: double real,image; public: Complex(double x=0.0,double y=0.0) { bool Complex &c); Complex operator Complex &c); bool operator Complex &c); Complex operator -(); operator real =x; image =y; } !=(const -(const ==(const &operator Complex +=(const Complex &c); void Print(); }; void Complex::Print() { cout<
}; 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) 1; year=y; } Date& Date::operator+(int days) { i-= diff+1; day = 1; month++; if(month>12) { year++; month = } } else { day+= i; break;} c3.Print(); if(c3==c1)cout<<"c3 equals to c1"< 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: } return *this; } void Date::showDate() { cout<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; Date(int m, int d, } int y); Date& operator+(int days); void showDate(); if(i>diff) { 7-4 以+,=为例 #include #include using namespace std; class String {
分享到:
收藏