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分)
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分)
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分)
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分)
write the output of the code below.
#include<iostream>
using namespace std;
class A{
public:
A& operator=(const A& r)
{
cout << 1 << endl;
return *this;
}
};
class B{
public:
B& operator=(const B& r)
{
cout << 2 << endl;
return *this;
}
};
class C{
private:
B b;
A a;
int c;
};
int main()
{
C m,n;
m = n;
return 0;
}
One for each line:
(1分)
(1分)
write the output of the code below.
#include <iostream>
using namespace std;
class MYCLASS{
public:
MYCLASS(int x):val(x){}
void print() const
{
cout << val << endl;
}
void print()
{
cout << val << endl;
}
private:
int val;
};
int main()
{
MYCLASS ob1(1);
const MYCLASS ob2(2);
ob2.print();
ob1.print();
return 0;
}
One for each line.
line 1: (1分) line 2: (1分)
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分)
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分)
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分)
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;
}
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分);
}
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;
}
#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;
}
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;
}