程序题答案之C++面向对象程序设计

程序设计

1实现一个类A,在A中有两个私有的整型变量a和b,定义构造函数对a和b进行初始化,并实现成员函数geta()取得a的值和getb()取b的值。实现类B从A继承,覆盖geta(),使其返回a的2倍。

答案:

#include
using namespace std;

class A
{
int a;
int b;
public:
A(int m,int n):a(m),b(n) {}
int geta() {return a; }
int getb() {return b; }
};

class B :public A
{
public:
B(int m,int n):A(m,n) {}
int geta() {return A::geta()*2; }
};

void main()
{
B b(2,2);
b.geta();
cout<<b.geta()<<endl;
}

 

2.写一个函数,找出给定字符串中小写字母字符的个数。函数的原型为:

int CalcAlpha(char *str);

函数参数: str为所要处理的字符串;

函数返回值:所给字符串中小写字母字符的个数

答案:

int CalcAlpha(char *str)

{

if(str == NULL) return 0;

int number=0;

for (int i=0;*(str+i)!=’\0′;i++)

{

if (*(str+i)>=’a'&&*(str+i)<=’z')

{

number++;

};

 

}

return number;

}

3定义一个图形Shape类并用继承方法定义圆形Circle类.

答案:

class shape
{
public:
shape(){};
};
class circle:public shape
{
public:
circle();
};

4用函数重载的方法定义两个重名函数,分别找出两实型数中的最小值和三个实型数的最小值.

答案:

float min(float x1, float x2)
{
return x1>x2?x2:x1;
}
float min(float x1, float x2, float x3)
{
return min( min(x1, x2), x3);
}

 

5..实现一个Point类(数据成员为一个点在两维直角坐标系内的坐标);派生一个圆Circle类;要求圆能计算周长,能够画出一个圆。

6.定义一个复数类Complex,要求复数类重载“*”;能够将复数对象转换为一个整数。

#include<iostream>

using namespace std;

class complex

{

public:

complex(int r=0,int i=0,int me=0){real=r;imag=i;m=me;}

complex operator * (complex c2);

void display();

void display1();

private:

int real;

int imag;

int m;

};

complex complex::operator *(complex c2)

{

complex c;

c.real=c2.real*real;

c.imag=c2.imag*imag;

c.m=c.real-c.imag;

return complex(c.real,c.imag,c.m);

}

void complex::display()

{   cout<<”(“<<real<<”,”<<imag<<”)”<<endl; }

void complex::display1()

{   cout<<m<<endl; }

void main()

{ complex c1(5,4),c2(2,10),c3;

cout<<”c1=”; c1.display();

cout<<”c2=”; c2.display();

c3=c1*c2;

cout<<”c3=c1*c2=”;

c3.display1();

}

7假设已定义了一个栈类Stack,编写入栈和出栈的成员函数代码。

#define  STACKSIZE 20

class Stack{

long buffer [STACKSIZE];

long *SP;

public ;

Stack( ){sp=buffer+STACKSIZE;}//sp指向栈底

~Stack( ){}

void push(long );//入栈操作

long pop( );//出栈操作

};

 

4下面的文件queue.h是一个队列类模板Queue的完整实现。在这个文件中首先定义了一个队列元素类模板QueueItem,然后在这个类的基础上定义了队列类模板Queue。在Queue中使用链表存放队列的各个元素,front指针指向链表的第一个节点元素,back指针指向链表的最后一个节点元素,成员函数add()将一个新节点元素加入到队列结尾,remove()从队列开头删除一个节点元素。为方便起见,程序中加上了行号。阅读程序,根据程序后面的问题作出相应解答。

 

/*———————————————————/

/********************* 文件queue.h ***********************/

/*————————————————————/

 

template

class Queue;

/***************** 定义模板类QueueItem ************/

template

class QueueItem

{

public:

QueueItem(const Type & elem):item(elem) {}

QueueItem() {}

private:

Type item;

QueueItem * nextItem;

friend  class Queue;

};

/***************** 定义模板类Queue ************/

template

class Queue {

public:

Queue():front( NULL), ____(A)_____ {}

~Queue();

Type remove();

void add(const Type &);

bool is_empty() const { return ____(B)______ ; }

private:

QueueItem *front;

QueueItem *back;

};

//模板类Queue的函数成员remove()的实现

//从队列头取出一个节点,并返回该节点的值

template

Type  Queue::remove()

{

QueueItem *pFront; //指向头节点的临时指针

Type retVal; //返回值

______(C)_________;

retVal = front->item;

front = front->nextItem;

delete pFront;

return retVal;

}

//模板类Queue的函数成员add()的实现

template

void Queue::add(const Type & newItem)

{

QueueItem *pNew = new QueueItem;

pNew->item = newItem;

______(D)____________;

if (front == NULL)

front = back = pNew;

else

{

back->nextItem = pNew;

_____(E)__________;

}

}

 

template

Queue::~Queue()

{

QueueItem *p  = front, *q;

while(p != NULL)

{

q = p->nextItem;

delete p;

p = q;

}

}

 

问题1: 程序中有几处填空,将它们完成。

(A)back(NULL)

(B)front == NULL back == NULL

(C)pFront = front

(D)pNew->nextItem = NULL

(E)back = pNew

 

问题2:()题中程序第1,2行为什么要说明一下类模板Queue?如果没有这两行语句,程序还正确吗?

答:不正确。因为在类QueueItem模板类的定义中用到了模板类Queue,而此时Queue还没有定义,所以要先声明一下,告诉编译程序Queue是一个模板类,它将在程序的其他地方定义。如果没有这个说明,编译程序就不知道标识符Queue代表什么样的含义了。

 

问题3:程序第22,23行各有一个const,它们各自表示什么含义:

答:第22行的const修饰的是函数的参数,表示在这个函数体中不能改它所修饰的参数所对应的实际参数的值。

第23行的const修饰的是模板类Queue的成员函数is_empty(),它表示在函数is_empty()的函数体中不能改变任何数据成员的值。

 

问题4:程序中模板类Queue的析构函数主要做了什么事情?为什么要这么做?

答:析构函数中主要是释放链表中存放的各个节点的空间。因为Queue对象在其生存期间可能加入了很多节点,从堆中申请了一些内存空间,这些空间应该随着对象的消亡而释放掉,所以需要在析构函数中来释放这些空间。

 

问题5:下面的程序使用了queue.h文件中定义的类模板,说明程序中哪些定义队列对象的语句是不正确的,哪些是正确的。

#include “queue.h”

void main()

{

Queue  q1; // 1

Queue q2; // 2

Queue q3(100); // 3

Queue q4[100]; // 4

Queue *q5 = new Queue; // 5

//….

delete q5;

}

语句号

1

2

3

4

5

 

 

6下面的程序定义了三个类,Base是图形对象基类,Point表示屏幕上的一个点,ColorPoint表示带颜色的点,基类中利用posx和posy记录图形对象的位置。先阅读三个类的定义,然后回答后面的问题(假定程序中调用的SetPixel()和SetColor()是系统函数库中预定义的函数,可以直接使用)。

/*****************************************************************/

class Base{

public:

Base():posz(0),posy(0){ };

Base(int px,int py);posz(px),posy(py){ };

virtual void draw()=0;

protected:

int posx,posy; //图形对象的位置

};

class Point:public Base{

public:

Point(int px,int py):Base(px,py){ }

void draw(){ SetPixel(posx,posy); }

};

class Colorpoint:public Point{

public;

ColorPoint(int px,int py):Point(px,py){color=0;}

ColorPoint(int px,int py,int c):Point(px,py),color(c){ }

void draw()

{

SetColor(color);

Point::draw();

}

protected:

int color;

};

/**************************************************************************/

问题1下面的主程序使用了上面定义的三个类,程序中定义对象语句哪些是正确的,哪些是错误的?

void main()

{

Base b; //1

Point p1; //2

Point p2(3,4); //3

ColorPoint cp1; //4

ColorPoint cp2(5,6); //5

ColorPoint cp3(7,8,1); //6

答:

 

 

 

问题2:将类ColorPoint的两个构造函数合并为一个构造函数,写出该函数的实现(提示:使用函数的默认参数)。

答:合并后的构造函数定义为:

ColorPoint( int px , int py , int c=0 ) : Point( px , py ) , color( c ){ }

 

问题3:下面的程序段

Base * p;

p=new ColorPoint(5,6);

p—>draw()

运行后将调用:

(A)Base::draw()

(B)Point::draw()

(C)ColorPoint::draw()

(D)::draw()

答:运行后将调用 (C),即ColorPoint::draw( )

Leave a Reply

:wink: :-| :-x :twisted: :) 8-O :( :roll: :-P :oops: :-o :mrgreen: :lol: :idea: :-D :evil: :cry: 8) :arrow: :-? :?: :!:


正在读取数据……