最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 正文

面向对象程序设计复习题

来源:动视网 责编:小OO 时间:2025-09-25 05:04:30
文档

面向对象程序设计复习题

面向对象程序设计复习题概念填空题1.运算符能够用来访问与局部变量同名的全局变量。2.运算符动态分配一个对象。3.类的成员只能被该类的成员函数或友元函数访问。4.类成员的默认访问模式是的。5.类的数据成员是该类的所有对象共享的信息。6.关键字指定了不可修改的对象或变量。7.要在类的对象上使用运算符,除了运算符和外,其它的必须都要被重载。8.重载不能改变原运算符的、、和对内部类型对象的原有含义。9.类的对象可作为类的对象处理。10.友元函数中可以直接访问类的和成员。1l.公有成员函数的集合常称为类
推荐度:
导读面向对象程序设计复习题概念填空题1.运算符能够用来访问与局部变量同名的全局变量。2.运算符动态分配一个对象。3.类的成员只能被该类的成员函数或友元函数访问。4.类成员的默认访问模式是的。5.类的数据成员是该类的所有对象共享的信息。6.关键字指定了不可修改的对象或变量。7.要在类的对象上使用运算符,除了运算符和外,其它的必须都要被重载。8.重载不能改变原运算符的、、和对内部类型对象的原有含义。9.类的对象可作为类的对象处理。10.友元函数中可以直接访问类的和成员。1l.公有成员函数的集合常称为类
面向对象程序设计复习题

概念填空题

1.运算符           能够用来访问与局部变量同名的全局变量。

2.运算符           动态分配一个对象。

3.类的        成员只能被该类的成员函数或友元函数访问。

4.类成员的默认访问模式是               的。

5.类的        数据成员是该类的所有对象共享的信息。

6.关键字         指定了不可修改的对象或变量。

7.要在类的对象上使用运算符,除了运算符        和       外,其它的必须都要被重载。

8.重载不能改变原运算符的         、        、        和对内部类型对象的原有含义。

9.         类的对象可作为         类的对象处理。

10.友元函数中可以直接访问类的         和        成员。

1l.公有成员函数的集合常称为类的           函数。私有成员函数的集合常称为类的         函数。

12.为了访问某个类的私有数据成员,必须在该类中声明该类的          。

13.           提供了一种描述通用类的方法。

14.运算new分配的内存要用运算符            回收。

15.参数           表示重载后缀 ++ 运算符函数。

16.当用受保护的继承从基类派生一个类时,基类的公有成员成为派生类的

          的成员,基类的受保护成员成为派生类的           成员。

17.在C++中,关键字            、            和            用来建立新的数据类型。

18.           限定符用来声明只读变量。

19.函数          能够定义一个在不同数据类型基础上完成同一任务的函数。

20.指向基类对象的指针可以指向其      派生类的对象,但是不允许指向其      派生类的对象。

答案:

1::      2 new      3私有和保护     4私有    5静态 

6const   7=&       8  优先级 结合性 操作数个数  9派生类 基类  

10 私有 受保护     11 接口 工具    12 友元     13 类模板  

14  delete          15 int            16受保护 受保护  17class struct union  18 const            19模板          20公有 私有和保护 

阅读程序写结果

1. #include

void main()

{

   int a(6),b(8),c; 

c=(a>b?++a:b-=2);

cout<   c=(a-b?a+b:a-6?b:a-6);

cout<}

输出结果:

答案:输出结果:

6,6,6

6,6,0

2. #include

void main()

{

   int i,j;  

for(i=11; i<=20 ; i+=2)

   {

for(j=2;j             if(i%j==0)  

                   break;

         if(j!=i)

cout<     }

cout<}

输出结果:

答案:12    14    15    16    18

3.#include

#include

int  f(int p);

void main()

{

   int a[]={1,2,3,4,5};

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

cout< cout<}

int f(int  p)

{

    static int s=1;

    s*=p;

    return s;

}

输出结果:

答案:

1  2  6  24  120

4.#include

int  f(int *p);

void main()

{

   int a[]={1,2,3,4,5}

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

cout<cout<}

int f(int *p)

{

    static int s=0;

    s+=*p;

return s;

}

输出结果:

答案:

1    3    6    10    15

4.

#include

void main()

{

   int a[5],x,i=0;  

   do

   {

cin>>x;

        if(x%2==0) 

        {

            a[i]=x;

            i++;

        }

}while(i<5);

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

     cout< cout<}

输入:

1 6 5 8 2 0 7 10

输出结果:

答案:6 8 2 0 10

5. #include

class myclass 

{

   private:

      int a,b;

      static int s;

   public:    

      myclass(int i, int j) 

      {

          a=i;         b=j;

cout<<"Constructor.\\n";

       }      

     ~myclass()

     { 

cout<<"Destructor.\\n";

     } 

      void  sum()

      {    s+=a*b;   }

      void print()

       {   

cout<       }

};

int myclass::s =0;

void main()

{  

    myclass *a1, *a2;

    a1=new myclass(1,1);

    a2=new myclass(10,20);

    (*a1).sum();

    (*a2).sum();

a1->print();

    a2->print();

    delete a1;

    delete a2;

}

输出结果:

答案:

Constructor.

Constructor.

1,1  sum=201

10,20  sum=201

Destructor.

Destructor.

6.

#include

void main()

 

  

 

 

  

  

 

 

}

输出结果:

答案:

a=1b=11

7. #include

class A 

{

   private:

      int a,b;

   public:    

      A(int i, int j) 

      { a=i;  b=j; }      

      void move(int m,int n)

      {

          a+=m;

          b+=n;

      }

      void show()

       {   

cout<<"("<       }

};

class B: public A 

{

   private:

      int x,y;

   public:    

       B(int i, int j,int k,int l):A(i,j) 

       { x=k;  y=l; }      

       void show()

{ cout<       void fun(){move(30,50);}

       void f1(){A::show();}

};

void main()

{  

    A e(10,20);

    e.show ();

    B b(30,40,50,60);

    b.fun ();

    b.show();

    b.f1();

}

输出结果:

答案:

(10,20)

50,60

(60,90)

8. #include

  class B{ 

private:

      int Y;

public: 

B(int y=0) { Y=y; cout<<″B(″<~B() { cout<<″~B()\\n″;} 

void print() { cout <} ;

  class D: public B{

private:

     int Z;

public:

     D (int y, int z):B(y)

     {  Z=z;

cout<<″D(″<     } 

~D() { cout<<″~D()\\n″; }

void print() {

B∶∶print();

cout <}

}; 

void main()

{   D d(11,22);

d.print(); 

}

输出结果:

答案:

B(11)

D(11,22)

112

~D()

~B()

9.

#include

#include

void main()

    控制输出行

  每行前的空格

   cout<<" "; //输出四个空格

 

  

 

  

 

}

输出结果:

答案:

                   1   2   1

               1   2   3   2   1

10. #include

class A

{

private:

    double X,Y;

public: 

A(double xx=0, double yy=0) 

{   X=xx;   Y=yy;

cout<<″构造函数被调用(″<}

A(A &p) {   X=p.X;   Y=p.Y;   }

};

A f()

{   A a(1,2);

return a;

}

void main()

{   A a(4,5);  A b(a);

b = f();

}

输出结果:

答案:

构造函数被调用(4,5)

构造函数被调用(1,2)

11. #include

class Areaclass

{

    public: 

        Areaclass(double x=0,double y=0)

        {

            height=x; 

            width=y;

        }

    protected: 

        double height;

        double width;

};

class Box: public Areaclass

    public: 

        Box(double h,double w):Areaclass (h,w){ }

        double Area();

};

class Triangle:public Areaclass

    public: 

        Triangle(double h,double w):Areaclass(h,w){ }

        double Area( );

};

double Box::Area(){ return height*width; }

double Triangle::Area(){ return width *height *0.5; }

void main()

    Box obj1(2.5,4.0);

    Triangle obj2(4.0,3.5);

cout<<"Box="< cout<<"Triangle="<}

输出结果:

答案:

Box=10

Triangle=7

12. #include

void main() 

{     int a[9]={1,2,3,4,5,6,7,8,9}; 

for(int i=0; i<9; i++) {

cout << setw(4) << a[i]; 

if(i%3==2) 

            cout<

}

输出结果:

答案:

1    2    3

4    5    6

7    8    9

13. #include

template

void print(T a[],int n )

{ for(int i=0; i{     cout<if (i%5==4)

        cout<}

cout<}

void main()

{  

int a[]={1, 2, 3, 4, 5, 6, 7};

double b[4]={8, 9, 10, 11 };

print(a,sizeof(a)/sizeof(int));

print(b,4);

}

输出结果:

答案:

1    2    3    4    5

6    7    

8    9    10    11

14. #include

class A

{

private:

static int n;

int X;

public:

A(int x=0) { X=x; n++; }

~A() { n-- ; }

static int GetNum(){ return n; }

void print();

};

void A∶∶print() { cout << ″n=″ << n << ″, X=″ << X<< endl; }

int A∶∶n = 0;

void main()

{  A *p=new A(12); 

p->print();

A a(34); 

a.print(); 

delete p; 

cout << ″n=″ << A::GetNum() << endl;

}

输出结果:

答案:

n=1,X=12

n=2,X=34

n=1

15. #include

void main(void)

{  int n=6, k;

cout << n << " Factors ";

for (k=2; k < n; k++)

if (n % k == 0)

cout << k << " ";

cout << endl;

}

输出结果:

答案:

6 Factors 2 3

16. #include

class myclass

    private: 

        int x,y;

        static long sum;

    public: 

        myclass(int a,int b) { x=a;y=b;}

        void getxy()

        { 

            sum*=x*y;

         cout<<"sum="<        }

};

long myclass::sum=1;

void main()

    myclass ob1(1,3);

    ob1.getxy();

    myclass ob2(2,4);

    ob2.getxy();

    myclass ob3(5,6);

    ob3.getxy();

}

输出结果:

答案:

sum=3

sum=24

sum=720

17.#include

#include

void main()

{

    int i;

    char *max, str[3][10]={"Wang

    max=str[0];

for(i=0;i<3;i++)

     if(strcmp(max,str[i])<0)

            max=str[i];

cout<<"The max string is: "<}

输出结果:

答案:

The max string is: Zhang

三、程序填空

1.菲波纳齐数列为:1,1,2,3,5,8,13,……;下面是实现计算该数列前10项的程序,请在空白处填入合适的内容。

#include

long fa(int m) //递归函数

long f;

  if(m>2)

 ____________     

  else 

_____________    

  return f;

void main()

{

for(int i=1;i<11;i++)

cout<<______ <<“  ”;   

cout<}

答案:f=fa(n-1)+fa(n-2);      

f=1;     

fa(i)

2.下面是实现字符在字符串中出现频率的程序,请在空白处填入合适的内容。

#include

int nchar(char *s,char c) 

    int n(0);

    for(int i=0;____________;i++)

        if(s[i]==c)

            n++;   

    _________________

void main()

{

    char str[80],ch;

cout<<"输入字符串:";

cin>>str;

cout<<"输入一个字符:" ;

cin>>ch ;

cout<}

答案:

s[i]  

return n;  

nchar(str,ch)

3.下面是将一个一维数组及各元素乘积写入文本文件data.txt的程序,请在空白处填入合适的内容。

_________________      

#include

void main(){

   int a[10]={1,2,3,4,5,6,7,8,9,10},cum=0;

   _____________________            

   if(!fout){     //打开文件失败

cout<<"Cannot open the file!"<      exit(1);

   }

   int i;

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

      cum*=a[i];

   _________________   //将数组元素写入文件    

  }

   _________________ //将元素的乘积写入文件

   fout.close();    

}

答案:#include

ofstream fout("data.txt");  

fout<fout<4.下面是一个判断一个整数是否素数的程序,请在空白处填入合适的内容。

#include

class integer{

    int number;

  public:

    integer(int n){________________ }

    int getnum(){return number;}

    bool isprime(){

     for(int i=2;i            if(number%i==0)

                break;

        if(______________)

             return true;

        else

            return false;

    }

};

void main(){

   integer n1(19);

   if(_______________)

     cout<    else

     cout<}

答案:

number = n; 

number==i;  

n1.isprime() 

5.下面是利用运算符重载实现字符串赋值(=)的程序,请在空白处填入合适的内容。

#include

#include

class Cstring

{char *s;

public:

    Cstring(char *s1=0){

    s=new char[strlen(s1)+1]; 

    strcpy(s,s1);  

    }

    __________________________   

void list(){cout<};

Cstring Cstring::operator=(Cstring &s1)

{

    delete s;

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

    strcpy(s,s1.s);

    ____________      

}

void main()

{

    Cstring cstr1("abcd"),cstr2("1234");

    __________________   

    cstr1.list();

}

答案:

Cstring operator=(Cstring &s1);   

return *this;    

cstr1=cstr2;

6.为使下面程序输出结果为:

1*2

3+4 

请在横线处填上适当的字句,以使程序完整。

#include

class A 

private: 

int R1, R2;

public:

A(int r1, int r2) { R1=r1; R2=r2; } 

void print(); 

void print() const;

}; 

void A::print() 

{

cout<}

 

void A::print() const 

cout<

void main() 

{  

A a(1, 2); 

const A b(3, 4); 

a.print(); 

b.print(); 

}

答案:

    “*”                    “+”

7下面是利用运算符重载实现两个一维等长数组相加(+)的程序,请在空白处填入合适的内容。

#include

class Array

{

    int  *a;

    int n;

  public:

    Array (int n1){

        __________________ 

        n=n1;

    }

    void input(){

     for(int i=0;i         cin>>a[i];

    }

    __________________________ 

    void list(){

     for(int i=0;i         cout<     cout<    }

};

Array Array::operator+( Array &a1)

{

    Array a2(n);

for(int i=0;i        ______________________

    return  __________; 

}

void main()

{

    Array a1(5),a2(5);

    a1.input();

    a2.input();

    a1.list();

    a2.list();

    (a1+a2).list();

}

答案:a=new int[n1];   

Array operator+( Array &a1);    

a2.a[i]=a[i]+a1.a[i];  

a2

8.以下的程序实现求分数序列2/1,3/2,5/3,8/5,13/8,21/13,……前10项的和。在横线处填写适当内容。

#include

#define N  10

void main( ){

int i, m, n, k;

      (1)       ;

m=2, n=1;

for( i=1;_____(2)______; i++){

s=s+1.0*m/n;

k=m;

m= _    (2)  __ ;

n=      (3) ___   ;

}

cout<<”s=”<}

答案:

(1)double s=0

(2)i<=N

(3)m+n

(4)k

9.以下程序将从data1.dat中读出的内容显示在屏幕上,并将其中的字母写到文件data2.dat中去。请在空白处填入合适的内容。

#include

#include

int main()

{ char ch;

fstream infile,outfile;

infile.open( ____(5) ___ __ , ios::in);

if(!infile)

{ cout<<″文件1不能打开!\\n″;  return 0; }

outfile.open( ____ (6) _______ , ios::out);

if(!outfile)

{ cout<<″文件2不能打开!\\n″;  return 0; }

while(infile.get(ch))

{ cout<if(ch>=’a’&&ch<=’z’ || ch>=’A’&&ch<=’Z’)

____  __ (7) _________ ;

 }

cout<infile.close();

outfile.close();

return 1;

}

答案:

(5)” data1.dat”

(6)” data2.dat”

(7)ouffile.put(ch)或output<10.为使下面的程序执行后输出矩形的面积,请在空白处填入合适的内容。

#include

class Point {                                //定义点类

     public:

         Point(double i, double j)

           { x=i;  y=j;   }

             (8)      double Area() const 

           { return 0.0;}

     private:

          double x,y;

};

class Rectangle:public Point {                //定义矩形类(点类的公有派生类)

   public:

      Rectangle(double i,double j,double k,double l):Point(i,j) 

           {  w=k ; h=l;   }

      _______(9)_________

           { return w*h;}

    private:

          double w, h;

};

void main() 

{

     Point *p;

     Rectangle rec(3.5,1.2,5.2,7.5);

             (10)      ;

cout<<"矩形的面积是:"<Area()<}

答案:

(8)virtual

(9)virtual double Area() const 或 double Area() const

(10)p=&rec

四、把下列各程序运行时各输出行写到相应横线上

1. #include

//继承中的析构函数的调用顺序

    class Point{

    protected:

      float x,y;

    public:

      Point(float  a= 0.0,float  b=0.0){ x=a;y=b; }  //构造函数

      ~Point( ){cout<<"Point destructor:("<    };

    class Circle:public Point{

    public:

      Circle(float r=0.0,float a=0,float b=0):Point(a,b) {Rds=r;}  //构造函数

~Circle( )  //析构函数

{cout<<"Circle destructor: Rds="<private:

  float  Rds;

    };

    void main( ){  //析构函数的调用顺序

      Circle  c1(4,7,3),c2(10,5,5);

    }

输出结果为:

                                                           

                                                             

                                                              

答案:m<1 || m>12

MONTH !=2

YEAR%400==0

YEAR%4==0&&

d>=d2

2. #include

    //子对象及继承中的构造函数的调用顺序

class DAT{

    public:

DAT(int i){a=i ; cout<<"class DAT \\n"; }

    private:

      int  a;

    };

    class A{

    public:

      A(int x):d(x){cout<<"class A\\n"; }

    private:

      DAT d;

    };

    class B:public A{

    public:

B(int x, int y):A(x), d(y){cout<<"class B\\n"; }

private:

  DAT d;

    };

    void main(  ){ B bb(6,5); }

输出结果为:

                                  

答案:  int[n]

delete [ ] 

i<0 || i>=size

!= r.size

buf[i] != r.buf [i]

3. #include

    //数组模板类

    template

    class  Array{

    public:

      Array(int n){elems=new T[n];nums=n;}

      ~Array( ){delete []elems;}

      void SetElem(int i,T v);

      T GetElem(int i);  

    private:

      T * elems;int nums;

    };

template

void Array::SetElem(int i,T v){

if(i>=0 && i    }

template

T Array::GetElem(int i){

if(i>=0 && i   else {cout<<"下标越界"; return -9999;}

    }

    void main( ){

Array m(6);

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

{  m.SetElem(i , i+98);   cout<cout<Array s(4);

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

{  s.SetElem(i, i* 3+2);cout<cout<      }

输出结果为:

答案:

                                                      

  new  TYPE[size]

delete[]

top==size-1 或者top>=size-1

++top     

top ! = -1   

top-- 

4. # include

    //虚函数与动态多态性

    class Student{

      int exams;  int * marks;char * name;

    public:

      Student(int e,int * m,char * n) {exams=e;marks=m;name=n;}

      int  total_mark( );

      virtual void format( );

      void  print( );

    };

    int Student::total_mark( ){

        int  sum=0 ;

for(int i=0;i             sum+=marks[i];

        return  sum ;

    }

    void Student::format(){ cout<       format( );

cout <    }

    class gradStudent:public Student {

       char  * supervisor;

    public:

       gradStudent(int e,int  * m,char *n,char * superv)

         :Student(e,m,n){supervisor=superv;}

       virtual void format( );

    };

    void gradStudent::format( ){

       Student::format( );

       cout<<"\\n\(graduate,supervised by "

           << supervisor << ")\\n" ;

     }

int s[ ]={90,70,80,60};

int g[ ]={60,88,72,92,80};

void main( ){

  Student  st(4,s,"LiNa");st.print( );

  gradStudent  gt(5,g,"GaoMing

}

输出结果为:

答案:

top = -1

top == -1

top == size - 1

!isFull()

!isEmpty()

5. #include

//子对象及多级继承中的构造函数的调用顺序

class  ALFA{

       int  x;

    public:

      ALFA(int y){x=y;cout<<"class ALFA\\n";}

    };

    class BETA{

    public:

BETA(int x):al(x){ cout<<"class BETA\\n"; }

    private:

      ALFA  al;

    };

    class B:public BETA{

    public:

B(int x):BETA(x),a2(x){ cout<<"class B\\n";}

    private:

      int a2;

    };

    class C:  public  B{

    public:

C(int i):B(i){ cout<<"class C\\n";}

    };

    void main( ){ C  Ob(10); }

输出结果为: 

                                           

                                           

                                           

答案:

new int[size]

   Buff[i]=0

   delete[]Buff  

i<0||i>=size

   Buff[i]

   This != &r     

   delete[]Buff

   new int[size]  

   return *this 

   new int[size]  

   Buff[i]=a.Buff[i]

五、编程题

1.键盘输入10个整数,计算其和并输出到屏幕上。

#include

void main()

{

   int x, sum=0;               

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

   {                         

cin>>x;

      sum+=x;                

   }

cout<}

2.编写一个求n的阶乘的递归函数,函数原型为:long int fact(int n);编写主函数,调用fact函数计算 (即求1!+2!+3!+……+10!),并在屏幕上输出结果。

{

 

 

 

 

   

}

void main()

{

 

 阶乘的和是:

}

2.编写求n个整数中最大值的函数,函数原型为:int maxi(int x[],int n);并编写主函数,调用该函数求键盘输入的十个整数之和,并在屏幕上输出。

#include

int maxi(int x[],int n)

{

int ma=x[0];             

for(int i=1;iif(mama=x[i];

        return ma;              

}

void main()

{

   int a[10];                

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

cin>>a[i];

cout<<”The max value is:”<}

4将键盘输入的一串字符中的大写字母挑出并输出到屏幕上。说明:字符串中不含空格,长度不超过40个字符。

#include                          

void main()                                 

{

    char str[40];                            

cout<<"输入字符串:";

cin>>str;                                

    for(int i=0;str[i]!='\\0';i++)                

        if(str[i]>='A'&&str[i]<='Z')            

         cout< cout<}

5. 编写程序从键盘输入3×3二维数组的9个整数,输出行元素的平均值最大的那一行的行号及元素值。

#include

void main()

 

 

 

 

  

  

 

 

 

 

 

 

  

  

 

 行平均值最大的行的行号是:"< 行平均值最大的行的元素是:";

 

}

文档

面向对象程序设计复习题

面向对象程序设计复习题概念填空题1.运算符能够用来访问与局部变量同名的全局变量。2.运算符动态分配一个对象。3.类的成员只能被该类的成员函数或友元函数访问。4.类成员的默认访问模式是的。5.类的数据成员是该类的所有对象共享的信息。6.关键字指定了不可修改的对象或变量。7.要在类的对象上使用运算符,除了运算符和外,其它的必须都要被重载。8.重载不能改变原运算符的、、和对内部类型对象的原有含义。9.类的对象可作为类的对象处理。10.友元函数中可以直接访问类的和成员。1l.公有成员函数的集合常称为类
推荐度:
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top