Skip to main content

Command Palette

Search for a command to run...

contructer based problems

Published
•2 min read•View as Markdown

✅ Output-Based Problem 1

#include <iostream>
using namespace std;

class A {
public:
    A() { cout << "A "; }
};

int main() {
    A obj1, obj2;
}

Output:

A A


✅ Output-Based Problem 2

class Test {
public:
    Test() { cout << "1 "; }
    Test(int x) { cout << x << " "; }
};

int main() {
    Test a;
    Test b(5);
    Test c;
}

Output:

1 5 1



✅ Output-Based Problem 4

class A {
public:
    A(int x = 10) {
        cout << x << " ";
    }
};

int main() {
    A a(5);
    A b;
}

Output:

5 10


✅ Output-Based Problem 5

class Demo {
public:
    Demo() { cout << "D1 "; }
    Demo(const Demo &d) { cout << "Copy "; }
};

int main() {
    Demo a;
    Demo b = a;
}

Output:

D1 Copy


✅ Output-Based Problem 6

class A {
public:
    A() { cout << "A "; }
};

class B : public A {
public:
    B() { cout << "B "; }
};

int main() {
    B obj;
}

Output:

A B


✅ Output-Based Problem 7

class Base {
public:
    Base() { cout << "Base "; }
};

class Derived : public Base {
public:
    Derived(int x) { cout << x; }
};

int main() {
    Derived d(7);
}

Output:

Base 7


✅ Output-Based Problem 8

class A {
public:
    A() { cout << "A "; }
};

void fun() {
    A temp;
}

int main() {
    fun();
    cout << "End ";
}

Output:

A End

(Destructor ka output nahi diya, so sirf constructor print hoga)


✅ Output-Based Problem 9

class A {
public:
    int x;
    A(int n) : x(n) {
        cout << x << " ";
    }
};

int main() {
    A a(2), b(3), c(4);
}

Output:

2 3 4


✅ Output-Based Problem 10

class A {
public:
    A() { cout << "C1 "; }
};

class B {
    A obj;
public:
    B() { cout << "C2 "; }
};

int main() {
    B b;
}

Output:

C1 C2

More from this blog

Amit singh's blog

235 posts