面向对象程序设计2022-2023秋冬习题集4
开始时间2022/12/09 24:47:00
结束时间2023/01/31 02:47:00
答题时长76440分钟
答卷类型标准答案
总分81

填空题得分:暂无总分:23
4-1

write the output of the code below.

1.the output at //1 is (1分)

2.the output at //2 is (1分)

3.the output at //3 is (1分)

4.the output at //4 is (1分)

5.the output at //5 is (1分)

#include <iostream>
#include <string>
using namespace std ;

class Testing
{
private:
    string words; 
    int number ;
public:
    Testing(const string & s = "Testing")
    {
        words = s ;
        number = words.length();
        if (words.compare("Testing")==0)
            cout << 1;
        else if (words.compare("Heap1")==0)
            cout << 2;
        else
            cout << 3;
    }
    ~Testing()
    {
        cout << 0;
    }
    void show() const
    {
        cout << number;
    }
};
int main()
{
    Testing *pc1 , *pc2;
    pc1 = new Testing ;          //1
    pc2 = new Testing("Heap1");  //2
    pc1->show();   //3
    delete pc1 ;   //4
    delete pc2 ;   //5
    return 0;
}

4-2

write the output of the code below.

#include<iostream>
#include<string>
using namespace std;

class Pet {
public:
    virtual string speak() const { return "pet!"; }
};
class Dog : public Pet {
public:
    string speak() const { return "dog!"; }
};
int main() {
    Dog ralph;
    Pet* p1 = &ralph;
    Pet& p2 = ralph;
    Pet p3;
    cout << p1->speak() <<endl;
    cout << p2.speak() << endl;
    cout << p3.speak() << endl;
    return 0;
}

(1分)

(1分)

(1分)


4-3

write the output of the code below.

#include<iostream>
using namespace std;

class INCREMENT 
{
public:
   INCREMENT( int v = 0, int i = 1 ); 
   void addIncrement() 
   { 
      v += increment; 
   } 
   void print() const; 
   int get() const
   {
       return v;
   }
private:
   int v;
   const int increment; 
}; 

INCREMENT::INCREMENT( int v, int i ) : v( v ), increment( i )    
{ 
} 

void INCREMENT::print() const
{
   cout << v << endl;
} 
int main()
{
   INCREMENT value( 1, 2);
   value.print();

   for ( int j = 1; j <= 2; j++ ) 
   {
      value.addIncrement();
      value.print();
   } 
   return 0;
}

One for each line:

line 1:(1分)
line 2:(1分)
line 3:(1分)


4-4

write the output of the code below.

#include<iostream>
using namespace std;

enum NOTE { middleC, Csharp, Cflat }; 
class Instrument {
public:
  virtual void play(NOTE) const = 0;
  virtual char* what() const = 0;
  virtual void adjust(int) = 0;
};

class Wind : public Instrument {
public:
  void play(NOTE) const {
    cout << 1 << endl;
  }
  char* what() const { return "Wind"; }
  void adjust(int) {}
};

class Percussion : public Instrument {
public:
  void play(NOTE) const {
    cout << 2 << endl;
  }
  char* what() const { return "Percussion"; }
  void adjust(int) {}
};

class Stringed : public Instrument {
public:
  void play(NOTE) const {
    cout << 3 << endl;
  }
  char* what() const { return "Stringed"; }
  void adjust(int) {}
};

class Brass : public Wind {
public:
  void play(NOTE) const {
    cout << 11 << endl;
  }
  char* what() const { return "Brass"; }
};

class Woodwind : public Wind {
public:
  void play(NOTE) const {
    cout << 12 << endl;
  }
  char* what() const { return "Woodwind"; }
};

void tune(Instrument& i) {
  i.play(middleC);
}

void f(Instrument& i) { i.adjust(1); }

int main() {
  Wind flute;
  Percussion drum;
  Stringed violin;
  Brass flugelhorn;
  Woodwind recorder;
  tune(flute);
  tune(drum);
  tune(violin);
  tune(flugelhorn);
  tune(recorder);
  f(flugelhorn);
  return 0;
}

One for each line:

line 1:(1分)
line 2:(1分)
line 3:(1分)
line 4:(1分)
line 5:(1分)


4-5

write the output of the code below.

#include<iostream>
using namespace std;
class TEST
{ 
    int num; 
public:
    TEST( int num=0); 
    void increment( ) ;
    ~TEST( );
}; 
TEST::TEST(int num) : num(num)
{
    cout << num  << endl; 
} 
void TEST::increment() 
{
    num++;
}
TEST::~TEST( )
{
    cout << num  << endl;
} 
int main( ) 
{
    TEST array[2]; 
    array[0].increment();
    array[1].increment();
    return 0;
}

One for each line:

line 1:(1分)
line 2:(1分)
line 3:(1分)
line 4:(1分)


4-6

write the output of the code below.

#include<iostream>
using namespace std;
class Base{
protected:
    int x;
public:
    Base(int b=0): x(b) { }
    virtual void display() const {cout << x << endl;}
};
class Derived: public Base{
    int y;
public:
    Derived(int d=0): y(d) { }
    void display() {cout << x << "," << y << endl;}
};
int main()
{
  Base b(1);
  Derived d(2);
  Base *p = &d;
  b.display();
  d.display();
  p->display();
  return 0;
}

One for each line:

line 1:(1分)

line 2:(1分)

line 3:(1分)


程序填空题得分:暂无总分:38
5-1

Run the following program, the output is: B::f()

#include <iostream>
using namespace std;
class A{ 
public: 
    (1分){ cout<<"A::f()\n"; } 
 }; 
class B:public A{ 
 public:
      void f() {cout<<"B::f()\n"; } 
 }; 
int main() 
{ 
   B b;
   A &p (1分);
   (1分)f(); 
   return 0;
}

5-3

Run the following program, Enter: 1, the output is:
S1 == S2
HfLLO
HFLLO

#include <iostream>
using namespace std;
class ERROR{};
class STRING
{
   char *m_pStr;
   int m_len;
public:
   STRING(char *str=NULL){
      if (str != NULL) {   
           m_len = strlen(str);
         m_pStr = (1分);  
         strcpy((1分));   
      }
      else {
         m_len = 0;
         m_pStr = NULL;
      }
   }  
   (1分) operator=(char *str)  
   {
      (1分) m_pStr ;   
      m_len = strlen(str)+1;
      m_pStr = new char[m_len]; 
      strcpy((1分)); 
      return (1分);  
   }
 
   bool operator==(STRING str) (1分)
   {
      return ((1分)(m_pStr, str.m_pStr)== 0);  
   }
   char operator [] (int i) (1分)
   {
      if (i<m_len && i>=0) return m_pStr[i];
      throw (1分);
   }
   char& operator[](int i) (1分)
   { 
       if (i<m_len && i>=0) return m_pStr[i];
       ERROR e;
       (1分);  
   }
  (1分) ostream& operator<<(ostream& out ,STRING s); 
};
ostream& operator<<(ostream& out ,STRING s)     
{
   out << s.m_pStr;
   return out;  
}

int main()
{
   STRING s1,s2("HeLLO");
   int i;
   cin >> i;
   s1 = s2;
   if (s1 == s2) cout << "S1 == S2\n";
   s1[1] = s1[1] + 1;
   cout << s1 << endl;;

   (1分){  
       if(s1[i]>='a' && s1[i]<='z') s1[i] =  s1[i] - 32;
       cout << s1 << endl;
   }
   (1分)(   ERROR& e)  
   {
      cout << "upperbound overflow";
   }
   return 0;
}

5-4
#include <iostream>
using namespace std;
class IndexError{};
template (2分)
class ARRAY
{
    size_t m_size;
    T *m_ptr;
public:
    ARRAY(size_t size) : m_size(size)
    {    
        m_ptr = new T[size];    
        memset(m_ptr, 0, size*sizeof(int));
    }
    ~ARRAY()
    {    
        delete[] m_ptr;    
    }
    T& at(int index);
};

template <typename T>
(2分)::at(int index)  
{
    if(index<0||(2分))  
    {
        (2分) IndexError();  
    }
    return m_ptr[index];
}

int main()
{
    ARRAY<int> a(50);
    int i;
    cin >> i;
    (2分)  
    {
        for(int j=0;j<i;j++)
            a.at(i) = j;
    }
    catch(IndexError e)
    {
        return 0;
    }
    return 0;
}

5-5

Run the following program, Enter: 1, the output is: 55 34 21 13 8 5 3 2 1 1

#include <iostream>
using namespace std;

enum ERROR{UnderFlow,OverFlow};
template<typename T>
class StackTemplate {
    enum { ssize = 100 };
    T stack[ssize];
    int top;
public:
    StackTemplate() : top(0) {}
    void push(const T& i) {
        if (top >= ssize)  (1分);
        stack[top++] = i;
    }
    T pop() {
        if ((1分)) throw UnderFlow;
        return (1分);
    }
    int size() const
    { return top; }
};
int fibonacci(int n);

int main() {
    (1分) {
        (1分) is;
        for(int i = 0; i < 20; i++)
            is.push(fibonacci(i));
        for(int k = 0; k < 20; k++)
            cout << is.pop() << "\t";
    }
    catch( ERROR e ) {
        switch((1分))
        {
        case OverFlow:
            exit;
        case UnderFlow:
            exit;
        }
    }
    catch(...)
    {
        exit;
    }
    return 0;
}

int fibonacci(int n) 
{
    (1分) int sz = 100;
    int i;
    static int f[sz]; 
    if (n >= sz) (1分);
       f[0] = f[1] = 1;
    for(i = 0; i < sz; i++)
        if(f[i] == 0) break;
    while(i <= n) {
        (1分) = f[i-1] + f[i-2];
        i++;
    }
    return (1分);
}

函数题得分:暂无总分:20
6-2
表彰优秀学生(多态)
(20分)

学期结束,班主任决定表彰一批学生,已知该班学生数在6至50人之间,有三类学生:普通生,特招运动员,学科专长生,其中学科专长生不超过5人。

主函数根据输入的信息,相应建立GroupA, GroupB, GroupC类对象。

GroupA类是普通生,有2门课程的成绩(均为不超过100的非负整数);

GroupB类是特招运动员,有2门课程的成绩(均为不超过100的非负整数),1次运动会的表现分,表现分有:A、B、C、D共4等。

GroupC类是学科专长生,有5门课程的成绩(均为不超过100的非负整数)。

表彰人员至少符合以下3个条件中的一个:

(1)2门课程平均分在普通生和特招运动员中,名列第一者。

a.该平均分称为获奖线。

b.存在成绩并列时,则全部表彰,例如某次考试有2人并列第1,则他们全部表彰。

(2)5门课程平均分达到或超过获奖线90%的学科专长生,给予表彰。

(3)2门课程平均分达到或超过获奖线70%的特招运动员,如果其运动会表现分为A,给予表彰。

输入格式:每个测试用例占一行,第一项为类型,1为普通生,2为特招运动员,3为学科专长生, 输入0表示输入的结束。第二项是学号,第三项是姓名。对于普通生来说,共输入5项,第4、5项是课程成绩。对于特招运动员来说,共输入6项,第4、5项是课程成绩,第6项是运动会表现。对于学科专长生来说,共输入8项,第4、5、6、7、8项是课程成绩。

输出时,打印要表彰的学生的学号和姓名。(输出顺序与要表彰学生的输入前后次序一致)

函数接口定义:

以Student为基类,构建GroupA, GroupB和GroupC三个类

裁判测试程序样例:

#include<iostream>
#include <string>
using namespace std;

/* 请在这里填写答案 */

int main()
{
    const int Size=50;
    string num, name;
    int i,ty,s1,s2,s3,s4,s5;
    char gs;
    Student *pS[Size];
    int count=0;
    for(i=0;i<Size;i++){
        cin>>ty;
        if(ty==0) break;
        cin>>num>>name>>s1>>s2;
        switch(ty){
             case 1:pS[count++]=new GroupA(num, name, s1, s2); break;
             case 2:cin>>gs; pS[count++]=new GroupB(num, name, s1,s2, gs); break;
             case 3:cin>>s3>>s4>>s5; pS[count++]=new GroupC(num, name, s1,s2,s3,s4,s5); break;
        }            
    }
    for(i=0;i<count;i++) {
        pS[i]->display();
        delete pS[i];
    }
    return 0;
}

输入样例:

1 001 AAAA 96 80
2 009 BBB 82 75 A
1 007 CC 100 99
3 012 CCCC 97 95 90 99 93
1 003 DDD 62 50
1 022 ABCE 78 92
2 010 FFF 45 40 A
3 019 AAA 93 97 94 82 80
0

输出样例:

009 BBB
007 CC
012 CCCC