《面向对象程序设计》综合复习题集(杨永斌整理) 下载本文

内容发布更新时间 : 2024/5/6 4:58:24星期一 下面是文章的全部内容请认真阅读。

class Goods {

private:

char gd_name[20]; //商品名称 int weight; //商品重量

static int totalweight; //同类商品总重量 public:

Goods (char*str,int w){ //构造函数 strcpy(gd_name,str); weight=w;

totalweight+=weight; }

~ Goods (){totalweight -= weight;}

char* GetN(){___(1)___;} //返回商品名称 int GetW(){return weight;}

___(2)___ GetTotal_Weight() { //定义静态成员函数返回总重量 ___(3)___; } }

(1) (2) (3)

38. class Point {

private: int x, y; public:

Point(){x=y=0;}

Point(int x0,int y0) {x=x0;y=y0;} int GetX() { return x; } int GetY() { return y; }

void Print(){cout<<\ ___(1)___; //友元函数声明 ___(2)___; //友元函数声明 };

Point operator+(Point& pt,int dd)

//加号操作符重载函数,实现Point类对象与整数加法 {

Point temp=pt; temp.x+=dd; temp.y+=dd; return temp; }

Point operator+(Point& pt1,Point& pt2)

//加号操作符重载函数,实现两个Point类对象的加法

{

Point temp=pt1; temp.x+=pt2.x; ___(3)___; return temp; }

(1) (2) (3)

39. 在下面一段类定义中, Derived类是由直接基类Base 1和Base 2所派生的,Derived类包含有两个间接基类BaseBase,在初始化函数Init中,需要把x1和x2的值分别赋给属于基类Base1的x成员和属于基类Base2的x成员。 class BaseBase { protected: int x; public:

BaseBase(){ x = 1;} };

class Base1: public BaseBase { public: Base1(){} };

class Base2: public BaseBase { public: Base2(){} };

class Derived: ___(1)___ {

public:

Derived() {}

void Init(int x1, int x2) { ___(2)___; ___(3)___; }

void output() {cout<

(1) (2) (3)

40. 在下面一段类定义中, Derived类公有继承了基类Base。需要填充的函数由注释内容给出了功能。 class Base {

private:

int mem1,mem2; //基类的数据成员

public:

Base(int m1,int m2) { mem1=m1; mem2=m2; }

void output(){cout<

class Derived: public Base {

private:

int mem3; //派生类本身的数据成员 public:

//构造函数,由m1和m2分别初始化mem1和mem2,由m3初始化mem3 Derived(int m1,int m2, int m3); //输出mem1,mem2和mem3数据成员的值 void output(){

___(1)___; cout<

Derived::Derived(int m1,int m2, int m3): ___(2)___ {___(3)___;}

(1) (2) (3)

41. 在下面一段类的定义中,需要填充的函数由注释内容给出了功能。 class Point //定义坐标点类 {

public:

int x,y; //点的横坐标和纵坐标 Point(){x=0;y=0;}

Point(int x0,int y0) {x=x0; y=y0;} int X(){return x;} int Y(){return y;}

void PrintP(){cout<<\};

class Line: public Point //利用坐标点类定义直线类 {

private:

class Point pt1,pt2; //直线的两个端点 public:

Line(Point pts, Point pte); //构造函数,分别用参数初始化对应的端点 double Dx(){return pt2.x-pt1.x;} double Dy(){return pt2.y-pt1.y;} double Length(){ //计算直线的长度 return sqrt(___(1)___);

};

void PrintL(); //输出直线的两个端点和直线长度 };

Line::Line(Point pts, Point pte) ___(2)___ void Line::PrintL() {

cout<<\ pt1.PrintP(); cout<<\ pt2.PrintP();

cout<<\}

(1) (2) (3)

42. 在下面一段类的定义中,自行车类的虚基类为车辆类,机动车类的虚基类也为车辆类,摩托车类的基类为自行车类和机动车类,类之间均为公有继承。 class vehicle //车辆类 {

private:

int MaxSpeed; //最大车速 int Weight; //车重 public:

vehicle(){MaxSpeed=0; Weight=0;};

virtual void Run() {cout<<\};

class bicycle : ___(1)___ //自行车类 {

private:

int Height; //车高 public:

bicycle(){};

void Run() {cout<<\};

class motorcar : ___(2)___ //机动车类 {

private:

int SeatNum; //乘人数 public:

motorcar(){};

void Run() {cout << \};

class motorcycle: ___(3)___ //摩托车类 {

public:

motorcycle (){};

void Run() {cout<<\};

(1) (2) (3)

五、读程序写出运行结果

1. #include #include void main() {

int a[8]={25,48,32,85,64,18,48,29}; int max,min; max=min=a[0];

for(int i=0; i<8; i++) { if(x>a[i]) max=a[i]; if(x

cout<<\ cout<<\ }

2. #include void main() {

int a,b;

for(a=1,b=2; b<50;) { cout<

cout<

cout<

3. #include const int M=3, N=4; void main() {

int i,j,s=0;

for(i=1;i<=M;i++) for(j=1;j<=N;j++) s+=i*j;

cout<<”s=”<

4. #include