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

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

write the output of the code below.

#include <iostream>
using namespace std;
int& f(int &i )
{
    i += 10;
    return i ;
}
int main()
{
    int k = 0;
    int& m = f(k);
    cout << k << "#";
    f(m)++;
    cout << k << endl;
    return 0;
}

(2分)


4-2

write the output of the code below.

#include <iostream>
using namespace std;

class counter{
private:
    int value;
public: 
    counter():value(0) {}
    counter& operator++();  
    int operator++(int);    
    void reset()
    {
        value = 0;
    }
    operator int() const  
    {
        return value;
    }    
};

counter& counter::operator++()    
{     
    if (3 == value)
          value = 0;
    else
        value += 1;
    return *this;
}

int counter::operator++(int) 
{        
    int t = value;
    if (3 == value)
         value = 0;
    else
        value += 1;
    return t;
}

int main()
{
    counter a;   
    while (++a)  
         cout << "***\n";
    cout << a << endl;
    while (a++)  
         cout << "***\n";
    cout << a << endl;
    return 0;
}

One for each line:

(1分)
(1分)
(1分)
(1分)
(1分)


4-3

write the output of the code below.

#include <iostream>
using namespace std;

class Sample{
    friend long fun(Sample s);  
public:
    Sample(long a)
    { 
        x = a;
    }    
private:
    long x; 
};     
long fun(Sample s)
{
    if (s.x < 2) return 1; 
    return s.x * fun(Sample(s.x-1));  
}
int main()
{
    int sum = 0; 
    for(int i=0;i<6;i++)
    {
       sum += fun(Sample(i));
    }
    cout << sum;
    return 0;
}

(1分)