{ int length,width; public:
Rectangle(int x,int y,int l,int w):Point(x,y) { length=l;width=w;}
int getlength(){return length;} int getwidth(){return width;} };
void main()
{ Rectangle r(0,0,8,4); r.move(23,56);
cout< 定义一个Point类,派生出Rectangle类和Circle类,计算各派生类对象的面积Area()。将下列程序未完成的部分按要求完成之。 #include Point(double x=0, double y=0) {X=x;Y=y;} void ShowPoint() {cout<<\ private: double X,Y; }; class Rectangle: public Point { public: Rectangle(double w,double h,double x,double y):Point(x,y) {width=w,height=h;Area();} void Area() {area= width*height;} void ShowArea(){ cout<<\ } private: double width,height,area; }; class Circle: public Point { //**填入适当的语句 }; int main(){ Rectangle r(10,8,0,0); Circle c(4,3,5); r.ShowArea(); c.ShowArea(); return 0; } 写出程序运行结果: #include void f1(){cout<<\}; class D:public B{ public: void f1(){cout<<\}; void f(B& rb){ rb.f1(); } int main( ){ D d; B b,&rb1=b,&rb2=d; f(rb1); f(rb2); return 0; } 结果为: B::f1 B::f1 试题6:写出下列程序的运行结果: #include A(){ fvd(); } virtual void fvd(){cout<<\基类中的成员函数\}; class B:public A{ public: void fvd() { cout<<\派生类中的成员函数\}; void bar(){ A * a=new B; delete a;} void main(){ bar(); A a; B b; } 结果为: 基类中的成员函数 基类中的成员函数 基类中的成员函数 写出下列程序的运行结果: #include A(int i = 3){ x = i; } virtual void at(){ cout<<\ } void at2(){ at();} protected: int x; }; class B: public A{ public: B(int m){ y = m; } void at(){cout<<\ private : int y; }; void main(){ A k(5),* p; p = &k; p->at2(); B s(8); p = &s; p->at2(); } 结果为: x=5 y=8 试题8: #include A(){cout<<\ voidf(){ cout<<\}; class B :public A{ public: B(){ cout<<\ virtual voidf(){ cout<<\}; class C:public B{ public: C(){cout<<\ void f(){cout<<\}; void main(){ A * pa; B * pb; pa = pb = new C; pa->f(); pb->f(); } 输出为: A constructor called! B constructor called! C constructorcalled! f() is called in A! f() is called in A! Rect是一个矩形类,main()函数中定义了3个对象,分别用到了两个构造函数,其中的缺省构造函数将数据成员全部初始化为0。main()中又执行了两矩形相加操作和输出操作。请完善程序。 class Rect{ private: float x: //左下角X坐标 float y; //左下角Y坐标 float w: //宽 float h: //高 public: Rect(){ (1) } Rect( (2) ){ x=a;y:=b;w=c;h=d; }; Rect operator+(Rect b); void Display(); }; Rect (3) (Rect b){ Rect s; s.x=x+b.x;s.y=y+b.y; s.w=w+b.w; s.h=h+b.h; return (4) ; } void Rect::Display(){ cout<<”x=”< void main(){ Rect A,B(1.4,2,3,20),c(2.5,5,3,4.8); A=B+C: A.Display(); } 阅读程序回答问题: #include int x; public: void Show() { cout<<”x=”< { x=a;cout<<”A”<<’\\n’;- ~A(){ cout<<”~A”<<’\\n’;- }; void main(void) { A f; f.Show(); f=20; f.Show(); } 问题一:本程序共输出 行,依次是: 问题二:与语句f=20等价的语句是 问题三:语句f=20是系统采用了 的类型转换 问题四:语句f=20的作用是 A.将常数20赋给对象f,然后调用构造函数 B.先用对象f调用构造函数,然后将20赋给f C.f自动产生一个临时对象,再调用构造函数,将20初始化构造函数 D.=左边调用构造函数,并将20转换为临时对象,完成初始化后将临时对象赋给f 问题五:程序执行结果是: 阅读程序,回答问题 #include int x,y; public: