c++题库编程题 下载本文

内容发布更新时间 : 2024/6/24 3:06:07星期一 下面是文章的全部内容请认真阅读。

共享知识 分享快乐

}

string & string ::operator=(const string &s) { if(this==&s) return *this; delete xstring; xstring=new char [strlen(s.xstring)+1]; strcpy(xstring,s.xstring); return *this; } //6-4

#include #include using namespace std; class String { public:

String() { p=NULL;} String(char *str);

friend bool operator==(String &string1,String &string2); void display(); private: char *p; };

String::String(char *str) { p=str; }

void String::display()

{ cout<

bool operator==(String &string1,String &string2) { if(strcmp(string1.p,string2.p)==0) return true; else

return false; }

void compare(String &string1,String &string2) {

if(operator==(string1,string2)==0) { string1.display(); cout<<\ string2.display();

书籍是人类知识的总结,书籍是全世界的营养品。——莎士比亚

共享知识 分享快乐

} else cout<<\ }

int main()

{ String string1(\ compare(string1,string2); return 0; } //6-5

#include #include using namespace std; class String { char *p; public: String(){p=NULL;} String(const char *s) { p=new char(strlen(s)+1); strcpy(p,s); } char * operator +(const char *s) { char *temp=new char(strlen(p)+strlen(s)+1); strcpy(temp,p); strcat(temp,s); return temp; } friend bool operator ==(const String &a,const String &b) {return (!strcmp(a.p,b.p));} friend ostream &operator <<(ostream &output,const String &d) {output<

void main() { String Str1(\ Str3=Str1+\ cout<

书籍是人类知识的总结,书籍是全世界的营养品。——莎士比亚

共享知识 分享快乐

#include using namespace std; class Car { int wnum; float weight; public: Car(int a,float b):wnum(a),weight(b){} void Cshow() { cout<<\轮子个数: \个\ <<\车重: \ } };

class SmallCar:private Car { int s_cnum; public: SmallCar(int a,float b,int c):Car(a,b),s_cnum(c){} void Sshow() { Cshow(); cout<<\载客量: \人次\ } };

class BigCar:private Car { int b_cnum; float b_cweight; public: BigCar(int a,float b,int c,float d):Car(a,b),b_cnum(c),b_cweight(d){} void Bshow() { Cshow(); cout<<\载客量: \人次\载重量T\ } };

int main() { Car car(4,8); SmallCar smallcar(8,16,8); BigCar bigcar(16,32,16,32); cout<<\汽车: \

书籍是人类知识的总结,书籍是全世界的营养品。——莎士比亚

\ 共享知识 分享快乐

car.Cshow(); cout<<\小车: \ smallcar.Sshow(); cout<<\卡车: \ bigcar.Bshow(); return 0; } //7-2

#include #include using namespace std; class Shape { float x,y; public: Shape(float a,float b):x(a),y(b){} void show() { cout<<\左上角坐标 (x,y)= (\ } virtual float GetArea()=0; };

class Rec:virtual public Shape { float high,width; public: Rec(float a,float b,float c,float d):Shape(a,b),high(c),width(d){} void show() { cout<<\矩形 : \\n\ Shape::show(); cout<<\矩形宽、高: \ } float GetArea() { show(); return high*width; } };

class Ell:virtual public Shape { float xx,yy; public: Ell(float a,float b,float c,float d):Shape(a,b),xx(c),yy(d){}

书籍是人类知识的总结,书籍是全世界的营养品。——莎士比亚

共享知识 分享快乐

void show() { cout<<\椭圆 : \\n\ Shape::show(); cout<<\椭圆x轴,y轴: \ } float GetArea() { show(); return 4*atan(1)*xx*yy; } };

int main() { Rec rec(5,5,5,5); Ell ell(8,8,12,10); Shape *s; s=&rec; cout<<\矩形的面积: \ s=ℓ cout<<\椭圆的面积: \ return 0; } //7-3

#include //#include using namespace std; class mammal { int m; public: mammal(int i=0):m(i) { cout<<\ } ~mammal() {

cout<<\ } }; class cat:public mammal { int c; public: cat(int j=0):c(j)

书籍是人类知识的总结,书籍是全世界的营养品。——莎士比亚