自考C++试题及答案(9套合集). 下载本文

内容发布更新时间 : 2024/5/18 19:03:36星期一 下面是文章的全部内容请认真阅读。

.

四、完成程序题(本大题共5小题,每小题4分,共20分)

1. 在下面程序横线处填上适当内容,使程序执行结果为:\。

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

char * pdata; mystring(int len)

{pdata=new char[len+1]; }

~mystring() {delete pdata;}

void show(){cout<

void fun(mystring** array,int len) {mystring*old=*array;

_*array=new mystring(len)______; memcpy(*array, old, len); }

void main()

{mystring str(20); mystring*pstr=&str; mystring**ppstr=&pstr;

strcpy(str.pdata,\fun(ppstr, 20); str.show();_______ }

2. 在下面程序横线处填上适当字句,完成类的定义。

class line; class box { private: int color; int upx, upy; int lowx, lowy; public:

friend int same_color(line l, box b); void set_color (int c){color=c;}

void define_box (int x1, int y1, int x2, int y2) {upx=x1;upy=y1;lowx=x2;lowy=y2;} };

class line { private:

.

.

int color;

int startx, starty; int endx, endy; public:

friend int same_color(line l,box b); void set_color (int c) {color=c;}

void define_line (int x1,int y1,int x2,int y2) {startx=x1;starty=y1;endx=x2;endy=y2;} };

int same_color(line l, box b) {if (l.color==b.color) return 1; return 0; }

3. 下面程序用来求直角三角形斜边长度。

#include #include class Point {private: double x,y;

friend Line;__________ public:

Point(double i=0,double j=0) {x=i;y=j;}

Point(Point &p) {x=p.x;y=p.y;} };

class Line {private: Point p1,p2; public:

Line(Point &xp1,Point &xp2):p1(xp1),p2(xp2)________{} double GetLength(); };

double Line::GetLength() {double dx=p2.x-p1.x; double dy=p2.y-p1.y;

return sqrt(dx*dx+dy*dy); }

void main()

{ Point p1,p2(6,8); Line L1(p1,p2);

cout<

.

.

4. 在下面程序的底画线处填上适当的字句,使该程序执行结果为40。

#include class Test { public:

static int x______; Test (int i=0) {x=i+x;} int Getnum()

{return Test::x+7;} };

int Test::x=33_______; void main() {Test test;

cout<

5. 在下列程序的空格处填上适当的字句,使输出为:0,2,10。

#include #include class Magic {double x; public:

Magic(double d=0.00):x(fabs(d)) {}

Magic operator+(Magic &c______) {

return Magic(sqrt(x*x+c.x*c.x)); }

friend ostream& operator<<(ostream & stream,Magic & c) { stream<

void main() {Magic ma;

cout<

五、程序分析题(本大题共2小题,每小题5分,共10分) 1. 运行程序,写出程序执行的结果。

#include

.

.

void main() {int a,b,c; char ch;

cin>>a>>ch>>b>>c;//从键盘上输入1.5×c×10×20,×表示一个空格 cout<

0

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

#include class A {public: A()

{cout<<\virtual ~A()

{cout<<\virtual void f()

{cout<<\void g() {f();} };

class B:public A {public: B()

{f();cout<<\~B()

{cout<<\};

class C:public B {public: C()

{cout<<\~C()

{cout<<\void f()

{cout<<\};

void main() {A *a=new C; a->g(); delete a; }

As cons. As f().

.

.

Bs cons. Cs cons. Cs f(). Cs des. Bs des. As des.

六、程序设计题(本大题共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<<\车轮数:\重量:\

.