面向对象程序设计作业

面向对象程序设计作业,第1张

23. //

24.cin

25.MAIN

26.复合

27.>>

28.#include

29.#

30.

31.a i++ i

32.0 0 2 3 x!=y x=y

33.10,20

34.class Rectangle {

private:

int width

int height

public:

Rectangle(int w,int h): width(w),height(h){}

~Rectangle(){}

int Circumference(){return width*2 + height*2;}

int Square(){return width*height}

}

35.求10个100以内的质数

第一部分 选择题 (共20分)

一、单项选择题(本大题共10小题,每小题2分,共20分)在每小题列出的四个选项中只有一个选项是符合题目要求的,请将其代码填在题后的括号内。错选或未选均无分。

1.在C++中,函数原型不能标识( D )

A.函数的返回类型 B.函数参数的个数

C.函数参数类型 D.函数的功能 p108有叙述

2.在C++程序中,对象之间的相互通信通过( B ) ?

A.继承实现 B.调用成员函数实现

C.封装实现 D.函数重载实现

3.对于任意一个类,析构函数的个数最多为( B )

A.0 B.1 C.2 D.3

P288 析构函数不接受参数,也不返回数值。类只可能有一个析构函数,不能进行析构函数的重灾。

4.下面函数模板定义中不正确的是( A )

A.template<class Q> B.template<class Q>

QF(Q x){ QF(Q x){

return Q+x return x+x

}}

C.template<class T> D.template<class T>

TF(T x){ TF(T x){

return x*x return x>1

}}

5.友元关系不能( c A )?

A.继承

B.是类与类的关系

C.是一个类的成员函数与另一个类的关系 p316

D.提高程序的运行效率

6.语句ofstream f(〃SALARY.DAT〃,ios::app|ios::binary)的功能是建立流对象f,试图打开文件SALARY.DAT并与之连接,并且( A )

A.若文件存在,将文件写指针定位于文件尾;若文件不存在,建立一个新文件

B.若文件存在,将其置为空文件;若文件不存在,打开失败

C.若文件存在,将文件写指针定位于文件首;若文件不存在,建立一个新文件

D.若文件存在,打开失败;若文件不存在,建立一个新文件

7.下面说法正确的是( B )

A.内联函数在运行时是将该函数的目标代码插入每个调用该函数的地方

B.内联函数在编译时是将该函数的目标代码插入每个调用该函数的地方

C.类的内联函数必须在类体内定义

D.类的内联函数必须在类体外通过加关键字inline定义

8.可以用p.a的形式访问派生类对象p的基类成员a,其中a是( D )

A.私有继承的公有成员 B.公有继承的私有成员

C.公有继承的保护成员 D.公有继承的公有成员

9.在公有派生情况下,有关派生类对象和基类对象的关系,不正确的叙述是( A )

A.派生类的对象可以赋给基类的对象

B.派生类的对象可以初始化基类的引用

C.派生类的对象可以直接访问基类中的成员

D.派生类的对象的地址可以赋给指向基类的指针

10.对于类定义

class A{

public:

virtual void func1( ){ }

void func2( ){ }

}

class B:public A{

public:

void func1( ){cout<<〃class B func 1〃<<end1}

virtual void func2( ){cout<<〃class B func 2〃<<end1}

}

下面正确的叙述是( C )

A. A::func2( )和B::func1( )都是虚函数

B. A::func2( )和B::func1( )都不是虚函数

C. B::func1( )是虚函数,而A::func2( )不是虚函数

D. B::func1( )不是虚函数,而A::func2( )是虚函数

第二部分 非选择题 (共80分)

二、填空题(本大题共10小题,每小题2分,共20分)不写解答过程,将正确的答案写在每小题的横线处。错填或不填均无分。

11.定义类的动态对象数组时,系统只能够自动调用该类的___拷贝?______构造函数对其进行初始化。

12.无论是什么继承方式,派生类的成员不能访问基类__private_______属性的成员。

13.表达式cout<<end1还可表示为_cout<<” /n ”__。

14.基类的公有成员在派生类中的访问权限由__继承方式_______决定。

15.C++支持的两种多态性分别是_________多态性和_________多态性。

16.C++中语句const char * const p=〃hello〃;所定义的指针p和它所指的内容都不能被_ _修改_______。

17.假定AB为一个类,则语句AB(AB&x);为该类_构造?_函数的原型说明。

18.说明常成员函数时,关键字const写在成员函数的_________和_________之间。

19.在C++中,访问一个对象的成员所用的运算符是_________,访问一个指针所指向的对象的成员所用的运算符是_________。

20.派生类从一个或多个以前定义的该类的__基类_______继承数据和函数。

三、改错题(本大题共5小题,每小题2分,共10分)

21.指出下面程序段中的错误,并说明出错原因。

class A{

int a,b

public:

A(int aa,int bb) {a=aab=bb}

}

A x(2,3),y(4) y(4,0)

22.指出并改正下面利用类模板的对象定义中的错误。

template <class T>

class Tany{

T x,y

public:

Tany(T a,T b){x=a,y=b}

T sum( ){return x+y}

}

Tany (int) obj(10,100) 改为Tany<int>

23.指出下面程序段中的错误,并说明出错原因。

class one{

private:

int a

public:

void func(two&) void func(two &)错误,不能前向申明

}

class two{

private:

int b

friend void one::func(two&)

}

void one::func(two&r)

{

a=r.b

}

24.指出下面程序段中的错误,并说明出错原因。

#include <iostream.h>

class A{

public: void fun( ){cout<<〃a.fun〃<<endl}

}

class B{

public: void fun( ){cout<<〃b.fun〃<<endl}

void gun( ){cout<<〃b.gun〃<<endl}

}

class C:public A,public B{

private:int b

public:void gun( ){cout<<〃c.gun〃<<endl}

void hun( ){fun( )}改为void hun( ){A:fun( )}或B:fun( )

}

25.指出下面程序段中的错误,并说明出错原因。

class Location {

int X,Y=20 Y=20出错,类定义中不能显式地将类的数据成员初始化

protected:

int zeroX,zeroY

int SetZero(int ZeroX,int ZeroY)

private:

int length,height

public:

float radius

void init(int initX,int initY)

int GetX( )

int GetY( )

}

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

根据题目要求,完成程序填空。

26.在下面横线处填上适当字句,完成类中成员函数的定义。

class A{

int * a

public:

A(int aa=0) {

a=_new int(aa)_//用aa初始化a所指向的动态对象

}

~A(){_delete a_}//释放动态存储空间

}

27.下面是一个函数模板,用于计算两个向量的和。在下面横线处填上适当字句,完成函数模板定义。

#include <iostream.h>

template<class T>

T* f(T* a,T* b,int n)

{

T* c=_new T[n]___

for(int i=0i<ni++)

c〔i〕=_a[i]+b[i] __

return c

}

void main()

{

int a〔5〕={1,2,3,4,5},b〔5〕={10,20,30,40},*p

p=f(a,b,5)

for(int i=0i<5i++)

cout<<p〔i〕<<endl

}

28.下面是一个用户口令检查程序,在横线处填上适当语句完成程序功能。

#include <iostream.h>

#include <string.h>

_char* PASS=”wolleh”__//定义由PASS指针所指向的口令wolleh。

void main()

{

char user〔10〕//用于存放输入的口令

cout<<〃please input your password:〃<<endl

__cin>>user_//输入口令

if((strcmp(user,PASS))==0)

cout<<〃your password is correct〃<<endl

else

cout<<〃your password is error〃<<endl

}

29.下面是类fraction(分数)的定义,其中重载的运算符<<以分数形式输出结果,例如将三分之二输出为2/3。在横线处填上适当字句。

class fraction{

int den //分子

int num //分母

friend ostream&operator<<(ostream&,fraction)

}

ostream&operator <<(ostream&os,fraction fr){

_cout<<den<<”/ ”<<num<<endl__

return ___os______

}

30.在下面程序横线处填上适当的字句,使其输出结果为0,56,56。

#include <iostream.h>

class base{

public:

____int__func( ){return 0}

}

class derived:public base{

public:

int a,b,c

____void_____ setValue(int x,int y,int z){a=xb=yc=z}

int func( ){return(a+b)*c}

}

void main()

{

base b

derived d

cout<<b.func( )<<′,′

d.setValue(3,5,7)

cout<<d.func( )<<′,′

base&pb=d

cout<<pb.func( )<<end1

}

五、程序分析题(本大题共6小题,每小题5分,共30分)

给出下面各程序的输出结果。

31.#include <iostream.h>

void main( )

{

int *a

int *&p=a

int b=10

p=&b

cout<<*a

}

输出为:

32.#include <iostream.h>

template<class T>

Tf(T*a,T*b,int n){

Ts=(T)0

for(int i=0i<ni++)

s+=a〔i〕*b〔i〕;

return s

}

void main()

{

double c 〔5〕={1.1,2.2,3.3,4.4,5.5},d〔5〕={10.0,100.0,1000.0}

cout<<f(c,d,5)<<endl

}

输出为:2531

33.#include <iomanip.h>

void main()

{

for(int i=0i<4i++)

cout<<endl<<setfill(′′)<<setw(4-i)<<′0′

<<setfill(′*′)<<setw(i+i)<<(i>0?′0′:′′)

}

输出为:

34.运行下面的程序,写出当输入25,60时的输出结果。

#include <iostream.h>

class goods{

private:

static int totalWeight

int weight

public:

goods(int w)

{

weigh=w

totalWeight+=w

}

goods(goods&gd)

{

weight=gd.weight

totalWeight+=weight

}

~goods()

{

totalWeight-=weight

}

int getwg()

{

return weight

}

static int getTotal()

{

return totalWeight

}

}

int goods::totalWeight=0

void main()

{

int w

cout<<〃The initial weight of goods:〃<<goods::getTotal()<<endl

cin>>w //输入25

goods g1(w)

cin>>w //输入60

goods g2(w)

cout<<〃The total weight of goods:〃<<goods::getTotal()<<endl

}

输出为:

35.#include <iostream.h>

class A{

public:

A( ){ }

virtual void func( ){cout<<〃Destructor A〃<<endl}

~A( ) {func()}

}

class B:public A{

public:

B( ){ }

void func(){cout<<〃Destructor B〃<<endl}

~B( ) {func()}

}

void main( )

{

B b

A&a=b

}

输出为:

36.#include <iostream.h>

class My Class {

public:

int number

void set(int i)

}

int number=3

void MyClass::set (int i)

{

number=i

}

void main()

{

MyClass my1

int number=10

my1.set(5)

cout<<my1.number<<end1

my1.set(number)

cout<<my1.number<<endl

my1.set(::number)

cout<<my1.number

}

输出为:


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/11113335.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-13
下一篇 2023-05-13

发表评论

登录后才能评论

评论列表(0条)

保存