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

自考C++试题及答案(9套合集)-3-4.

来源:用户分享 时间:2025/8/8 5:57:23 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

#include <> class Test {int a,b; int getmin()

{return (a

void setValue(int x1,int x2,int x3) {a=x1;b=x2;c=x3;} int GetMin(); };

int Test::GetMin() {int d=getmin(); return (d=d

void main() {Test t1; (34,6,2);

cout<< ()<

5. 实现数值、字符串的交换。

#include #include using namespace std; template void Swap(T& a,T& b) {T temp;

temp=a,a=b,b=temp; }

void main() {int a=5,b=9;

char s1[]=\[]=\ Swap(a,b); Swap(s1,s2);

cout<<\ cout<<\ }

四、完成程序题(本大题共5小题,每小题4分,共20分) 1. 在下划线处填上缺少的部分。

#include <> class A {int a,b; public:

A(int i=0,int j=0)_____;在下面程序横线处填上适当内容,使程序执行结果为: S=2 S=5 S=9

#include <> void sum(int i) {static int s;

_s=s+i+2________; cout<<\ }

void main (void) {int i;

for (i=0;i<3;i++________) sum(i); }

3. 下面程序运行的结果是:5+10=15。

#include <> class Test { private: int x,y; public:

Test() {x=y=0;}

void Setxy(int x,int y) {(*this).x=x;(*this).y=y______}

void show(){cout<

void main() {Test ptr; (5,10); (); }

4. 完成下面类中成员函数的定义。

#include <> #include <> class Arr {protected: float *p;

int n;请在下面程序的横线处填上适当内容,以使程序完整,并使程序的输出为: 11,10 13,12

#include <> class A {int a; public:

A(int i=0){a=i;}

int Geta(){return a;}

void show(){cout<

class B {A a; int b; public:

B(int i,int j):a(j),b(i)_________ {}

void show(){cout<<()<<\ };

void main()

{B b[2]={B(10,11),B(12,13)}; for(int i=0;i<2;i++)

b[i].show()__________ }

五、程序分析题(本大题共4小题,每小题5分,共20分) 1. 给出下面程序输出结果。

#include<> class a {public:

a(int i=10){x=i;cout<<\ int x; };

class b:public a {public:

b(int i):A(i){x=i;cout<<\ private: a A; int x; };

void main() {b B(5); }

答案:a:10 a:5 b:5,10

[解析]定义对象B,先调用基类构造函数,在b构造函数中使用的是A(i),注意大小写,不 是a(i),也就是说调用基类的构造函数时没有实参值,所以采用默认值;在初始化类成员A,即 A(i),i=5,所以输入为a:5;最后是b类的构造函数,x=5,来自基类x=10,输出b:5,10。 2. 运行程序,写出程序执行的结果。 #include<> class Location {public: int X,Y;

void init(int initX,int initY); int GetX(); int GetY(); };

void Location::init (int initX,int initY) {X=initX; Y=initY; }

int Location::GetX() {return X; }

int Location::GetY() {return Y; }

void display(Location& rL) {cout<<()<<\\n; }

void main()

{Location A[5]={{5,5},{3,3},{1,1},{2,2},{4,4}}; Location *rA=A; A[3].init(7,3); rA->init(7,8);

for (int i=0;i<5;i++) display(*(rA++)); }

3. 给出下面程序输出结果。

#include <>

int a[8]={1,2,3,4,5,6,7}; void fun(int *pa,int n); void main() {int m=8; fun(a,m);

cout<

void fun(int *pa,int n) {for (int i=0;i

28

4. 给出下面程序输出结果。

#include <> class A {int *a; public:

A(int x=0):a(new int(x)){} ~A() {delete a;}

int getA() {return *a;} void setA(int x) {*a=x;} };

void main() {A x1,x2(3); A *p=&x2; (*p).setA()+5); (10+());

cout<<()<<\ }

108;

六、程序设计题(本大题共1小题,共10分) 1. 已知交通工具类定义如下。

要求:(1)实现这个类;(2)定义并实现一个小车类car,是它的公有派生类,小车本身的私有 属性有载人数,小车的函数有init(设置车轮数,重量和载人数),getpassenger(获取载人数 ),print(打印车轮数,重量和载人数)。

class vehicle {protected:

int wheels;//车轮数 float weight;//重量 public:

void init(int wheels,float weight); int get_wheels(); float get_weight(); void print(); };

void vehicle::init(int wheels,float weight) {this->wheels=wheels; this->weight=weight; cout<

int vehicle::get_wheels() {return wheels; }

float vehicle::get_weight() {return weight;} void vehicle::print()

{cout<<\车轮数:\重量:\

搜索更多关于: 自考C++试题及答案(9套合集)-3-4. 的文档
自考C++试题及答案(9套合集)-3-4..doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c8ub9f1rbpt7z7sh75m1a072ie1yi3600n0v_4.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top