Monday, January 5, 2026

C++ Classes and Objects: Predict Output

C++ Classes and Objects
Predict Output Questions


1. Constructor Initialization

#include <iostream>
using namespace std;

class Test {
    int x;
public:
    Test(int a) {
        x = a;
    }
    void display() {
        cout << x;
    }
};

int main() {
    Test obj(10);
    obj.display();
    return 0;
}

Answer:
10

Explanation:
  • Constructor assigns x = 10. The display() function prints the value of x.
2. Default Constructor
#include <iostream>
using namespace std;

class Demo {
    int x;
public:
    Demo() {
        x = 5;
    }
    void show() {
        cout << x;
    }
};

int main() {
    Demo d1;
    d1.show();
    return 0;
}

Answer:
5

Explanation:
Default constructor automatically sets x = 5 when object d1 is created.

3. Multiple Objects
#include <iostream>
using namespace std;

class Sample {
    int x;
public:
    Sample(int a) {
        x = a;
    }
    void display() {
        cout << x << " ";
    }
};

int main() {
    Sample s1(2), s2(4);
    s1.display();
    s2.display();
    return 0;
}

Answer:
2 4

Explanation:
  • Two objects are created with different values. Each object stores its own copy of x.
4. Static Data Member
#include <iostream>
using namespace std;

class Count {
    static int x;
public:
    Count() {
        x++;
    }
    static void show() {
        cout << x;
    }
};

int Count::x = 0;

int main() {
    Count c1, c2, c3;
    Count::show();
    return 0;
}

Answer:
3

Explanation:
  • x is static; hence shared among all objects.
  • Each constructor call increments x. Three objects; hence x = 3.

5. Function Returning Object
#include <iostream>
using namespace std;

class Test {
    int x;
public:
    Test(int a) {
        x = a;
    }
    Test add(Test t) {
        return Test(x + t.x);
    }
    void display() {
        cout << x;
    }
};

int main() {
    Test t1(5), t2(7);
    Test t3 = t1.add(t2);
    t3.display();
    return 0;
}

Answer:
12

Explanation:
  • t1.x = 5, t2.x = 7
  • New object created with value 5 + 7 = 12.

6. Constructor Called Multiple Times
#include <iostream>
using namespace std;

class Demo {
public:
    Demo() {
        cout << "C ";
    }
};

int main() {
    Demo d1, d2;
    return 0;
}

Answer:
C C

Explanation:
  • Constructor runs once per object. Two objects → prints twice.

7. Private Data Access
#include <iostream>
using namespace std;

class Test {
    int x;
public:
    void set(int a) {
        x = a;
    }
    void show() {
        cout << x;
    }
};

int main() {
    Test t;
    t.set(15);
    t.show();
    return 0;
}

Answer:
15

Explanation:
  • Private variable x is accessed using public member function set().

8. Object as Function Argument
#include <iostream>
using namespace std;

class Sample {
    int x;
public:
    Sample(int a) { x = a; }
    void add(Sample s) {
        cout << x + s.x;
    }
};

int main() {
    Sample s1(3), s2(6);
    s1.add(s2);
    return 0;
}

Answer:
9

Explanation: s1.x = 3, s2.x = 6 → prints 3 + 6 = 9.

9. Copy Constructor (Default)
#include <iostream>
using namespace std;

class Test {
    int x;
public:
    Test(int a) { x = a; }
    void show() { cout << x; }
};

int main() {
    Test t1(8);
    Test t2 = t1;
    t2.show();
    return 0;
}

Answer:
8

Explanation: Default copy constructor copies the value of x from t1 to t2.

10. Destructor Execution Order
#include <iostream>
using namespace std;

class Demo {
public:
    ~Demo() {
        cout << "D ";
    }
};

int main() {
    Demo d1, d2;
    return 0;
}

Answer:
D D

Explanation:
  • Destructor is called automatically when objects go out of scope (reverse order of creation).
  • Two objects → destructor runs twice.

Back to C++ Home Page >

* * * * *

No comments:

Post a Comment