面向对象试题

 阅读以下程序(或函数)并简要叙述其功能 

1、

#include <fstream>

using namespace std;

int main( )

{int a[10];

 ofstream outfile(″f1.dat″,ios::out);

 if(!outfile)                        

  {cerr<<″open error!″<<endl;

   exit(1);

  }

 cout<<″enter 10 integer numbers:″<<endl;

 for(int i=0;i<10;i++)

  {cin>>a[i];

   outfile<<a[i]<<″ ″;}            

 outfile.close();                 

 return 0;

}

功能:

2、

#include <iostream>

using namespace std;

class Box

{

public:

Box(int,int,int);       

int volume( );          

private:

int height;

int width;

int length;

};

Box∷Box(int h,int w,int len)   

{

height=h;

width=w;

length=len;

}

int Box∷volume( )                

{return(height*width*length);

}

int main( )

{Box box1(12,25,30);  

cout<<″The volume of box1 is ″<<box1.volume( )<<endl;

Box box2(15,30,21);            

cout<<″The volume of box2 is ″<<box2.volume( )<<endl;

return 0;

}

运行结果:

功能:

3、

#include<iostream.h>

#include<string.h>

class String

{

public:

  String(){Length=0;Buffer=0;}

  String(const char* str);

  void Setc(int index,char newchar);

  char Getc(int index) ;

  int GetLength() {return Length;}

  void Print()

  {

        if(Buffer==0) 

                cout<<"empty.\n";

        else

                cout<<Buffer<<endl;

  }

  void Append(const char* Tail);

  ~String(){delete[] Buffer;}

private:

  int Length;

  char* Buffer;

};

String::String(const char*str)

{

  Length=strlen(str);

  Buffer=new char[Length+1];

  strcpy(Buffer,str);

}

void String::Setc(int index,char newchar)

{

  if(index>0&&index<=Length)

    Buffer[index-1]=newchar;

}

char String::Getc(int index)

{

  if(index>0&&index<=Length)

     return Buffer[index-1];

  else

     return 0;

}

void String::Append(const char* Tail)

{

  char* temp;

  Length+=strlen(Tail);

  temp=new char[Length+1];

  strcpy(temp,Buffer);

  strcat(temp,Tail);

  delete[] Buffer;

  Buffer=temp;

}

简述String类中Setc、Getc和Append三个函数的功能:

4、

#include <iostream.h>

class Point

{

public:

Point() { X = Y = 0; }

Point( unsigned x, unsigned y ) { X = x; Y = y; }

unsigned x() { return X; }

unsigned y() { return Y; }

void Print() { cout << "Point(" << X << ", " << Y << ")" << endl; }

friend Point operator+( Point& pt, int nOffset );

private:

unsigned X;

unsigned Y;

};

Point operator+( Point& pt, int nOffset )

{

Point ptTemp = pt;

ptTemp.X += nOffset;

ptTemp.Y += nOffset;

return ptTemp;

}

请叙述Point类的功能:

 阅读以下程序并填空(填上正确的语法成分),使其成为完整的程序 

1、下列程序在构造函数和析构函数中申请和释放类的私有成员,请完成该类的实现。

class MyClass

{

public:

MyClass(int a);

~MyClass();

private:

int* X;

};

MyClass::MyClass(int a)

{

    ____(1)_____;

}

MyClass::~MyClassO

{

____(2)______;

}

2、请根据提示完成该类的定义

#include<iostream>

#include<cstring>

using namespace std;

class Student{

   char *name;

   int age;   

   int score;

public:

   char * Getname(){

       (3)   //返回学生姓名

   }

   int Getage(){  

        return age;   

   }

   int Getscore(){

       return score;

   }

   void display(){  //显示学生相关信息

(4) 

}

Student(char* n,int a,int s){ //分别用n,a和s初始化学生的姓名,年龄和成绩

name=new char[strlen(n)+1];

    strcpy(name,n);

    age=a;

    score=s;

   }

Student(const Student& s){  //拷贝构造函数

    name=new char[strlen(s.name)+1+6];

    strcpy(name,"copyof");

    strcat(name,s.name);

    age=s.age;    

    score=s.score;

   }

~Student(){ 

       (5)

   }

};

 编程题 

1、下图描述了A、B、C三个类的关系以及包含成员,定义这三个类。

2、设计并测试复数类(Complex)

  1. 设计一个复数类(Complex)包含两个数据成员:实部(real),虚部(imagin);

包含如下主要成员函数:

  • 构造函数(用来初始化一个复数对象,默认实部、虚部均为0);
  • 重载加、减法运算符(+、-)实现复数类的加、减法运算;
  • 显示复数对象,按a+bi(a为实部、b为虚部)格式输出一个复数对象。
  1. 请在主函数中使用所设计的复数类定义两个复数对象,求其和、差并输出。

答案

 阅读以下程序(或函数)并简要叙述其功能 

1、

有一个整型数组,含10个元素,从键盘输入10个整数给数组,将此数组送到磁盘文件中存放。

2、

运行结果:

The volume of box1 is 9000

The volume of box2 is 9450

功能:有两个长方柱,其长、宽、高分别为: (1)12,20,25;(2)10,14,20。求它们的体积。

3、

Setc( ): 将Buffer中第 index 个元素的值用newchar替换

Getc( ): 返回Buffer中第 index个元素的值。

Append( ): 将字符串Tail连接到 Buffer的末尾。

4、

定义了Point类,实现点之间的重载了运算符。

 阅读以下程序并填空(填上正确的语法成分),使其成为完整的程序 

(1) X=new int(a)

(2) delete X

(3) return name;

(4) cout<<"name:"<<name<<","<<"age:"<<age<<","<<"score:"<<score<<endl;

(5) delete[] name;

 编程题 

1、

class A

{public:

  int a;

void display( );

};

class B

{public:

  int a;

  void display( );

};

class C :public A,public B

{public :

  int b;

  void show();

};

2、

#include <iostream.h>

class complex

{

public:

        complex(double r=0.0,double i=0.0)

        {      real=r;             image=i;  }

        friend complex operator +(complex c1,complex c2);

        friend complex operator -(complex c1,complex c2);

        void display();

private:

        double real;

        double image;

};

complex  operator +(complex c1,complex c2)

{

        return complex(c1.real+c2.real,c1.image+c2.image);

}

complex  operator -(complex c1,complex c2)

{

        return complex(c1.real-c2.real,c1.image-c2.image);

}

void complex::display()

{

        if(image>0)

                cout<<real<<"+"<<image<<"i"<<endl;

        else if(image<0)

                        cout<<real<<image<<"i"<<endl;

                 else cout<<real<<endl;

}

int 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.display();

        c3=c1+c2;

        cout<<"c3=c1+c2=";      c3.display();

        return 1;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值