77范文网 - 专业文章范例文档资料分享平台

c++ 期末练习题+答案(4)

来源:网络收集 时间:2020-05-12 下载这篇文档 手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:或QQ: 处理(尽可能给您提供完整文档),感谢您的支持与谅解。点击这里给我发消息

a=10,b=10 a=15,b=18 Donstructor. Donstructor.

5、#include #include class Base { private:

char msg[30]; protected: int n; public:

Base(char s[],int m=0):n(m) { strcpy(msg,s); }

void output(void)

{ cout<

class Derived1:public Base{ private: int n; public:

Derived1(int m=1): Base(\ { n=m; }

void output(void) { cout<

class Derived2:public Derived1 {

private: int n; public:

Derived2(int m=2): Derived1(m-1) { n=m; }

void output(void) { cout<

Derived1::output(); }

};

int main() {

Base B(\ Derived2 D; B.output(); D.output(); }

程序运行结果: 1

Base Class 2 1 0 Base

6、#include class X{ public:

X(int i) {

x=i; cout<<”int: ”<

x=(int)i*2; cout<<” double: ”<

{ cout<<” ~X(): ”<

void main() {

int a=8;

double b=11.0; X x1(55), x2(a*5); X x3=X(2*a); X x4=X(b); }

程序输出结果: int: 55,called int: 40,called int: 16,called double: 11,called ~X(): 22,called

~X(): 16,called ~X(): 40,called ~X(): 55,called 7.

#include class Samp {public:

void Setij(int a,int b){i=a,j=b;} ~Samp()

{ cout<<\ }

int GetMuti(){return i*j;} protected: int i; int j; };

int main() {

Samp *p;

p=new Samp[5]; if(!p)

{ cout<<\ return 1; }

for(int j=0;j<5;j++) p[j].Setij(j,j); for(int k=0;k<5;k++)

cout<<\

<

delete[]p; return 0; }

Muti[0] is:0 Muti[1] is:1 Muti[2] is:4 Muti[3] is:9 Muti[4] is:16 Destroying..4 Destroying..3 Destroying..2 Destroying..1

Destroying..0

8、 #include int func(int a) { int b=0; static int c=4; b++; c--; return(a+b+c); }

void main( ) { int a=2;

for(int j=0;j<2;j++) cout<

输出结果为:

6 6

9. #include

class Abc {public:

Abc( ){a=0; b=0; c=0;}

Abc(int i,int j,int k){a=i; b=j; c=k;}

void get(int&i,int&j,int&k){i=a; j=b; k=c;} Abc operator * (Abc obj); private:

int a,b,c; };

Abc Abc::operator * (Abc obj) {Abc tempobj;

tempobj.a=a*obj.a; tempobj.b=b*obj.b; tempobj.c=c*obj.c; return tempobj; }

void main()

{ Abc obj1(1,2,3),obj2(5,5,5),obj3; int a,b,c;

obj3=obj1*obj2; obj3.get(a,b,c);

cout<<”(obj1*obj2):\\t”<<”a=”<

cout<<”(obj2*obj3):\\t”<<”a=”<

}

(obj1*obj2): a=5 b=10 c=15 (obj2*obj3): a=25 b=50 c=75

五、编程题

1、编写程序:定义抽象基类Shape,由它派生出五个派生类:Circle(圆形)、Square(正方形)、Rectangle( 长方形)、Trapezoid (梯形)和Triangle (三角形),用虚函数分别计算各种图形的面积,并求出它们的和。要求用基类指针数组。使它的每一个元素指向一个派生类的对象。 不易理解的语句需要加适当注释。 注:主函数中定义如下对象

Circle circle(12.6); Square square(3.5); Rectangle rectangle(4.5,8.4);

Trapezoid trapezoid(2.0,4.5,3.2); Triangle triangle(4.5,8.4);

#include #include using namespace std; const double Pi=acos(-1.0); class Shape{ public: };

class Circle:public Shape{ public: };

class Square:public Shape{ public:

Square(double a):l(a){} virtual double area() {

return l*l;

Circle(double a):r(a){} virtual double area() { }

double r;

return r*r*Pi; virtual double area() { }

return 0;

private:

百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说教育文库c++ 期末练习题+答案(4)在线全文阅读。

c++ 期末练习题+答案(4).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印 下载失败或者文档不完整,请联系客服人员解决!
本文链接:https://www.77cn.com.cn/wenku/jiaoyu/1050178.html(转载请注明文章来源)
Copyright © 2008-2022 免费范文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ: 邮箱:tiandhx2@hotmail.com
苏ICP备16052595号-18
× 注册会员免费下载(下载后可以自由复制和排版)
注册会员下载
全站内容免费自由复制
注册会员下载
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: