-
What is the output of the following snippet?
#include <iostream>
using namespace std;
class A {
public:
A() : val(0) {}
int val;
int inc() { ++val; return val--; }
};
void Do(A *a) {
a-> val = a->inc() ;
}
int main(void)
{
A a;
Do(&a);
cout << a.inc();
return 0;
}
-
What is the output of the following snippet?
#include <iostream>
using namespace std;
class A {
friend class B;
private:
int field;
public:
int set(int x) { return field = ++x; }
int get() { return ++field; }
};
class B {
public:
void kill(A &a) { a.field = 0; }
};
int main(void)
{
A a; B b;
a.set(1);
b.kill(a);
cout << a.get();
return 0;
}
-
What is the output of the following snippet?
#include <iostream>
using namespace std;
class X {
private:
int v;
};
class Y : public X {
Y() : v(0) {}
}
int main()
{
Y y;
cout << y.v;
return 0;
}
-
What is the output of the following snippet?
#include <iostream>
using namespace std;
class X {
public:
virtual void shout() { cout << "X"; }
};
class Y : public X {
public:
void shout() { cout << "Y"; }
};
class Z : public Y {
public:
void shout() { cout << "Z"; }
};
int main()
{
Z *z = new Z();
static_cast<Y *>(z) -> shout();
return 0;
}
-
What is the output of the following snippet?
#include <iostream>
using namespace std;
class X {
public:
void shout() { cout << "X"; }
};
class Y : public X {
public:
virtual void shout() { cout << "Y"; }
};
class Z : public Y {
public:
void shout() { cout << "Z"; }
};
int main()
{
Y *y = new Z();
dynamic_cast<X *>(y) -> shout();
return 0;
}
-
What is the output of the following snippet?
#include <iostream>
using namespace std;
class A {
friend void f();
private:
int field;
public:
int set(int x) { return field = ++x; }
int get() { return ++field; }
};
void f(A &a) {a.field /=2;}
int main()
{
A a;
a.set(2);
f(a);
cout << a.get();
return 0;
}
-
What is the output of the following snippet?
#include <iostream>
using namespace std;
class X {
protected:
int v;
};
class Y : protected X {
Y() : v(0) {}
}
int main()
{
Y *y = new Y();
cout << y->v;
delete y;
return 0;
}
-
What is the output of the following snippet?
#include <iostream>
using namespace std;
class A {
public:
A() : val(0) {}
int val;
void inc() { ++val; }
};
void Do(A a) {
a.inc();
}
int main(void)
{
A a;
Do(a);
a.inc();
cout << a.val;
return 0;
}
-
What is the output of the following snippet?
#include <iostream>
using namespace std;
class X {
public:
virtual void shout() { cout << "X"; }
};
class Y : public X {
public:
void shout() { cout << "Y"; }
};
class Z : public Y {
public:
void shout() { cout << "Z"; }
};
int main(void)
{
Y *y = new Z();
dynamic_cast<X *>(y) -> shout();
return 0;
}
-
What is the output of the following snippet?
#include <iostream>
using namespace std;
class A {
int *val;
public:
A() { val = new int; *val = 0; }
int get() { return ++(*val); }
};
int main(void)
{
A a,b = a;
cout << a.get() << b.get();
return 0;
}
22
20
12
- Compilation fails
-
What is the output of the following snippet?
#include <iostream>
using namespace std;
class X {
public:
void shout() { cout << "X"; }
};
class Y : public X {
};
class Z : public Y {
public:
void shout() { cout << "Z"; }
};
int main()
{
Z *z = new Z();
static_cast<Y *>(z) -> shout();
return 0;
}
-
What is the output of the following snippet?
#include <iostream>
using namespace std;
class A {
public:
A() : val(0) {}
int val;
virtual void run() { cout << val; }
};
class B : public A {
};
class C : public B {
public:
void run() { cout << val + 2; }
};
void Do(A *a) {
B *b;
C *c;
if(b = dynamic_cast<B *>(a))
b->run();
if(c = dynamic_cast<C *>(a))
c->run();
a->run();
}
int main()
{
A *a = new C();;
Do(a);
return 0;
}
210
222
- Compilation fails
212
-
What is the output of the following snippet?
#include <iostream>
using namespace std;
class A {
public:
int v;
A(int x) : v(x + 1) {}
int get() const { return v; }
};
int main()
{
A a(2);
A b(a);
cout << a.get() << b.get();
return 0;
}
23
13
33
- Compilation fails
-
What is the output of the following snippet?
#include <iostream>
using namespace std;
class X { };
class Y : public X { };
class Z : public X { };
int main()
{
Z *z = new Z();
X *x = new X();
x = z;
cout << (x == z);
return 0;
}
-
What is the output of the following snippet?
#include <iostream>
using namespace std;
class X {
public:
void shout() { cout << "X"; }
};
class Y : public X {
public:
void shout() { cout << "Y"; }
};
int main()
{
X *x = new Y();
static_cast<Y *>(x) -> shout();
return 0;
}
- Compilation fails
X
Y
- It prints nothing
-
What is the output of the following snippet?
#include <iostream>
using namespace std;
class X { };
class Y : public X { };
class Z : public X { };
int main()
{
Z *z = new Z();
Y *y = new Y();
z = y;
cout << (z == y);
return 0;
}
-
What is the output of the following snippet?
#include <iostream>
using namespace std;
class X {
public:
int v;
void put(int x) { v =x; }
int get(void) { return v; }
};
class Y : public X {
public:
Y() { put(0); }
void write(int x) { put(x + 1); }
int read(void) { return get() - 1; }
};
int main()
{
Y *y = new Y();
y ->write(1);
cout << y->read();
delete y;
return 0;
}
-
What is the output of the following snippet?
#include <iostream>
using namespace std;
class A {
public:
const int v;
A(int x) : v(x + 1) {}
int get() { return ++v; }
};
int main()
{
A a(2);
cout << a.get();
return 0;
}
-
What is the output of the following snippet?
#include <iostream>
using namespace std;
class A {
int *val;
public:
A() { val = new int; *val = 0; }
A(A &a) { val = new int; *val = a.get(); }
int get() { return ++(*val); }
};
int main()
{
A a,b = a;
cout << a.get() << b.get();
return 0;
}
20
- Compilation fails
22
21
-
What is the output of the following snippet?
#include <iostream>
using namespace std;
class A {
public:
A() : val(0) {}
int val;
void run() { cout << val; }
};
class B : public A {
public:
virtual void run() { cout << val + 2; }
};
class C: public B {
};
void Do(A *a) {
B *b;
C *c;
if(b = static_cast<B *>(a))
b->run();
if(c = dynamic_cast<C *>(b))
c->run();
a->run();
}
int main(void)
{
A *a =new C();;
Do(a);
return 0;
}
- Compilation fails
220
222
221