C++ Cross-Concept Questions Explained: Scope, Dispatch and Object Lifetime
Trace one well-defined C++ program from construction to destruction, then change one line at a time to see which language rule controls each output token.
KnowledgeGate Team
Exam prep & CS education

You may understand scope, inheritance, virtual functions and destructors separately, yet still lose the output when one C++ program combines all four. A reliable method is to record names in scope, static type, dynamic type, stored values, selected default arguments and lifetime events before evaluating anything. The following trace derives B7 D8 17 ~D8 ~B7 11, then change one line at a time to expose the rule behind each value.
The Coding & Skills hub places this trace inside the broader programming curriculum.
What a C++ cross-concept question is really testing
Cross-concept material is not a separate C++ feature. It is a question whose answer depends on two or more valid rules working together, such as name lookup with construction, or static default-argument binding with runtime virtual dispatch. The challenge is coordinating rules that are easy in isolation.
Start every such question with a five-entry fact sheet:
Names currently in scope.
Static type of each relevant expression.
Dynamic type of each polymorphic object.
Object and member values after construction.
Ordered output tokens or other side effects.
Resolve these facts before doing arithmetic. If classes, overrides, pointers and destructors are not yet comfortable in isolation, follow the C++ Tutorial: Complete Learning Path in 12 Weeks before attempting mixed-rule traces. Otherwise, continue directly with the program below.
Separate the exact rules before combining them
The worked program needs six language rules:
A local
xhides a globalx, while::xexplicitly names the global.A base subobject is constructed before the derived constructor body runs.
Data members initialise in declaration order after the base subobject.
A virtual call selects the final overrider from the object's dynamic type.
Default arguments come from the declaration visible through the expression's static type.
A virtual destructor destroys the derived part before the base part.
The fifth rule is the common trap. In a call through Base*, the omitted argument can come from Base::score as k = 2, while virtual dispatch selects the body of Derived::score. The C++ Overloading and Default Arguments: Rules and Examples guide develops that compile-time rule separately. Here, keep default selection and virtual body selection in different columns.
These are language guarantees, not compiler habits. There is no need to guess memory addresses or say what a compiler will "usually" do. Every token in this example has a defined cause.
Worked program: record every value before tracing output
Use this exact C++17-compatible program:
#include <iostream>
using namespace std;
int x = 4;
struct Base {
int n;
Base(int v) : n(v) { cout << "B" << n << " "; }
virtual int score(int k = 2) { return n + k; }
virtual ~Base() { cout << "~B" << n << " "; }
};
struct Derived : Base {
int bonus;
Derived(int v) : Base(v), bonus(v + 1) { cout << "D" << bonus << " "; }
int score(int k = 5) override { return n + bonus + k; }
~Derived() override { cout << "~D" << bonus << " "; }
};
int main() {
int x = 7;
Base* p = new Derived(x);
cout << p->score() << " ";
delete p;
cout << x + ::x;
}Before predicting output, record ::x = 4, main::x = 7, static type Base* for p, and dynamic type Derived for the allocated object. Construction gives Base::n = 7 and Derived::bonus = 7 + 1 = 8. The omitted argument is k = 2, but the selected function body is Derived::score.
Now make a provisional ledger with four rows: construction events, call resolution, destruction events and the final scope expression. Do not write the answer string yet. This prevents the visible k = 5 in the override from contaminating a call made through Base*.
Compute the output step by step

First, new Derived(7) constructs its base subobject. Base(7) stores n = 7 and prints B7 . The program then initialises bonus = 7 + 1 = 8, enters the Derived constructor body and prints D8 .
Next, resolve p->score() in two stages. The static type Base* supplies the omitted default k = 2. The dynamic type Derived selects Derived::score. Its result is:
n + bonus + k = 7 + 8 + 2 = 17
The stream therefore prints 17 . Because Base::~Base is virtual, delete p runs the derived destructor before the base destructor, printing ~D8 followed by ~B7 . Finally, unqualified x means the local value 7, while ::x means the global value 4. Their sum is 7 + 4 = 11.
The exact output is B7 D8 17 ~D8 ~B7 11. The program does not print a newline.

Change one input to discover which rule owns the answer
Variation A: Change only the call to p->score(5). An explicit argument removes default selection, but the virtual target remains Derived::score. The score becomes 7 + 8 + 5 = 20, and the complete output is B7 D8 20 ~D8 ~B7 11.
Variation B: Change only the declaration to Derived* p = new Derived(x); and keep p->score(). Static type Derived* now supplies its default k = 5. The same derived body returns 20, producing B7 D8 20 ~D8 ~B7 11. The output matches Variation A, but here a changed static type, not an explicit argument, supplies the value.
Variation C: Restore the original pointer and call, then change local x from 7 to 3. Now n = 3, bonus = 3 + 1 = 4, and the score is 3 + 4 + 2 = 9. Destruction prints the new member values, and the last sum is 3 + 4 = 7. The complete output is B3 D4 9 ~D4 ~B3 7.
Traps that break an otherwise correct trace
Use these corrections as a quick check:
Derived::scoredisplaysk = 5, so the answer is20->Base*selects omittedk = 2, so the answer is17.Virtual means everything is dynamic -> only the function body is dynamically selected here.
deletecalls the base destructor first -> virtual cleanup runs~D8before~B7.x + ::xuses4twice -> unqualifiedxis local7, while::xis global4.Start arithmetic immediately -> first record constructor values and call-resolution facts.
Static type and dynamic type are the same fact -> record
Base*andDerivedseparately.
Do not make a defined trace undefined. If a variation removes the virtual base destructor but still deletes a Derived object through Base*, there is no portable destructor-output sequence to predict. Reject that variation as undefined behaviour.
How exams and practice sets combine the concepts
Mixed questions may ask you to choose the output of a constructor, virtual-call and destructor trace; identify a default argument through a base pointer or reference; distinguish name hiding from runtime dispatch; repair a declaration so polymorphic destruction is defined; or compare two one-line variations.
For speed, circle static type Base*, box dynamic type Derived, write n = 7 and bonus = 8, then annotate the call as default 2, body Derived. Append the six output tokens only after that ledger is complete. This is quicker and safer than mentally executing the entire program in one pass.
A broad mixed-concept practice pool builds fluency; for a named exam, confirm the current syllabus and instructions on the official website.
The short version and the next study step
Use five moves: resolve scope; record static and dynamic types; compute construction values; separate default-argument lookup from virtual body selection; and trace destruction in reverse construction order. Here, Base* contributes k = 2, while the Derived object contributes the override, n = 7 and bonus = 8. The score is 17, and the full output is B7 D8 17 ~D8 ~B7 11.
Continue with C++ Programming for focused study, or Coding for Placements if you want C++, other languages and coding-round preparation together. Reproduce Variation C without looking, then open the mixed-question practice pool.
Keep learning
Data Link Layer Flow Control: Stop-and-Wait, Sliding Windows and Worked Numericals
Build a reliable method for DLL flow-control problems by tracing one link from frame timing to windows, receiver credit, sequence-space limits and ACK policy.
Pipelining Cross-Concept Problems: CPI, Hazards and Cache Stalls
Build one reliable cycle ledger for a five-stage pipeline, then use it to combine load-use stalls, branch penalties and cache misses without mixing units.
Ethernet Cross-Concept MCQs: 12 Solved Data Link Layer Questions
Solve 12 Ethernet questions spanning devices, MAC structure, CSMA/CD, STP, VLANs, collision domains, switch port accounting and frame payload conversion.
Multi-Topic General Hindi for Teaching Exams: Cross-Concept Notes and Worked Set
Learn to separate meaning, word formation, usage, and comprehension when one Hindi passage supports several question types. Then practise the method on a fully solved set.





