#include <iostream>
struct T {
char c;
T(char x) : c(x) { std::cout << c; }
~T() { std::cout << (char)(c - 32); }
};
int main() {
T a('a');
{
T b('b');
T c('c');
}
T d('d');
std::cout << '|';
return 0;
}
AabcCBd|DA
BabcBCd|AD
Cabcd|DCBA
DabcCBd|AD
Correct Answer: Option A
Constructors print lowercase, destructors uppercase. b and c die at the inner brace in reverse order (C, B). At the end of main, d and then a are destroyed after the |.
#include <iostream>
class Session {
public:
static inline int live = 0;
Session() { ++live; }
~Session() { --live; }
};
int main() {
Session a;
{
Session b, c;
std::cout << Session::live << ' ';
}
Session* d = new Session;
std::cout << Session::live << ' ';
delete d;
std::cout << a.live << '\n';
return 0;
}
A3 2 1
B3 4 1
C3 2 2
D1 1 1
Correct Answer: Option A
Inside the block three sessions exist. After it, a and the heap object make 2; deleting d leaves 1. A static member can also be accessed through an object (a.live).
The non-virtual hello is chosen from the pointer's static type (Base). The virtual greet uses the actual object type. Called on d directly, Derived::hello hides Base::hello.
Members are constructed in declaration order (engine, front, back) before the body of Bike's constructor; destruction runs the destructor body first, then members in reverse order.
copy is a real Animal object built from the Animal part of c – sliced. A reference and a pointer still refer to the Cat, so the virtual call reaches Cat::speak.
We use Google Analytics cookies to see how LearnFrenzy is used and make it better, and Google AdSense to keep it free.
No analytics or ad cookies are set until you accept. Privacy Policy
·