Output questions for oop
Output questions for oop
Question no:1
#include <iostream>
using namespace std;
class Base {
public:
virtual void display() {
cout << "Base Class" << endl;
}
};
int main() {
Base* ptr = new Derived();
ptr->display();
return 0;
}
Question no: 2
#include <iostream>
using namespace std;
class Shape {
public:
virtual void draw() = 0; // Pure virtual function
};
int main() {
Shape* shape = new Circle();
shape->draw();
return 0;
}
Question no3:
#include <iostream>
using namespace std;
class Animal {
public:
void sound() {
cout << "Animal sound" << endl;
}
};
class Dog : public Animal {
public:
void sound() {
cout << "Bark" << endl;
}
};
int main() {
Dog d;
Animal& a = d;
a.sound();
return 0;
}
Question no: 4
#include <iostream>
using namespace std;
class Animal {
public:
virtual void sound() {
cout << "Animal sound" << endl;
}
};
int main() {
Animal* a = new Dog();
a->sound();
return 0;
}
Question no5:
#include <iostream>
using namespace std;
class Base {
public:
virtual ~Base() {
cout << "Base Destructor" << endl;
}
};
int main() {
Base* ptr = new Derived();
delete ptr;
return 0;
}
Question no:6
#include <iostream>
using namespace std;
class Animal {
public:
virtual void sound() = 0;
};
int main() {
// Animal a; // Error
Dog d;
d.sound();
return 0;
}
Question no: 7
#include <iostream>
using namespace std;
class A {
public:
A() {
cout << "A Constructor" << endl;
}
};
class B : public A {
public:
B() {
cout << "B Constructor" << endl;
}
};
int main() {
B b;
return 0;
}
Question no 8:
#include <iostream>
using namespace std;
class Base {
public:
virtual void show() {
cout << "Base Class Show" << endl;
}
};
int main() {
Base* b = new Derived();
b->show();
return 0;
}
Question no9:
#include <iostream>
using namespace std;
class A {
public:
void display() {
cout << "Class A Display" << endl;
}
};
class B {
public:
void display() {
cout << "Class B Display" << endl;
}
};
class C : public A, public B {};
int main() {
C c;
c.display();
return 0;
}
Question no 10:
#include <iostream>
using namespace std;
class A {
public:
void show() {
cout << "A show" << endl;
}
};
class B {
public:
void show() {
cout << "B show" << endl;
}
};
int main() {
C c;
c.show();
return 0;
}
Question no 11:
#include <iostream>
using namespace std;
class Base {
public:
Base(int x) {
cout << "Base Constructor, x = " << x << endl;
}
};
int main() {
Derived d(10);
return 0;
}
Question no: 12
#include <iostream>
using namespace std;
class Abstract {
public:
virtual void show() = 0;
};
int main() {
Abstract a; // Error
return 0;
}
Question no13:
#include <iostream>
using namespace std;
class Base {
public:
virtual void speak() {
cout << "Base Speaking" << endl;
}
};
int main() {
Base* b = new Base();
Base* d = new Derived();
b->speak();
d->speak();
return 0;
}
Question no14:
#include <iostream>
using namespace std;
class Base {
public:
virtual void display() {
cout << "Base Display" << endl;
}
};
int main() {
Derived d;
d.display();
return 0;
}
Question no15:
#include <iostream>
using namespace std;
class Base {
public:
Base() {
cout << "Base Constructor" << endl;
}
virtual void print() = 0; // Pure virtual function
};
int main() {
Derived d;
d.print();
return 0;
}
Question no 16:
#include <iostream>
using namespace std;
class A {
public:
void display() {
cout << "A Display" << endl;
}
};
class B : public A {
public:
void display() {
cout << "B Display" << endl;
}
};
int main() {
B b;
A a = b;
a.display();
return 0;
}
Question no17:
#include <iostream>
using namespace std;
class A {
public:
virtual void show() {
cout << "A Show" << endl;
}
};
class B {
public:
virtual void show() {
cout << "B Show" << endl;
}
};
int main() {
C c;
c.show();
return 0;
}
Question no18:
#include <iostream>
using namespace std;
class A {
public:
A() {
cout << "Constructor A" << endl;
}
};
class B : public A {
public:
B() {
cout << "Constructor B" << endl;
}
};
int main() {
B b;
return 0;
}
Question no 19:
#include <iostream>
using namespace std;
class Base {
public:
void display() {
cout << "Base Display" << endl;
}
};
int main() {
Derived d;
d.display();
return 0;
}
Question no20:
#include <iostream>
using namespace std;
class Base {
public:
virtual void display() {
cout << "Base Display" << endl;
}
};
int main() {
Base* b = new Base();
Base* d = new Derived();
b->display();
d->display();
return 0;
}
Question no 21:
#include <iostream>
using namespace std;
class Counter {
int value;
public:
Counter() : value(0) {}
int operator++(int) {
int temp = value;
value++;
return temp;
}
void display() {
cout << value << endl;
}
};
int main() {
Counter c;
cout << c++ << endl; // Post-increment
c.display();
return 0;
}
Question no22:
#include <iostream>
using namespace std;
class Counter {
int value;
public:
Counter() : value(0) {}
int operator++() {
++value;
return value;
}
void display() {
cout << value << endl;
}
};
int main() {
Counter c;
cout << ++c << endl; // Pre-increment
c.display();
return 0;
}
Question no 23:
#include <iostream>
using namespace std;
class Point {
int x, y;
public:
Point(int a, int b) : x(a), y(b) {}
Point operator+(Point p) {
return Point(x + p.x, y + p.y);
}
void display() {
cout << "(" << x << ", " << y << ")" << endl;
}
};
int main() {
Point p1(1, 2), p2(3, 4);
Point p3 = p1 + p2;
p3.display();
return 0;
}
Question no 24:
#include <iostream>
using namespace std;
class Point {
int x, y;
public:
Point(int a, int b) : x(a), y(b) {}
Point operator-(Point p) {
return Point(x - p.x, y - p.y);
}
void display() {
cout << "(" << x << ", " << y << ")" << endl;
}
};
int main() {
Point p1(5, 6), p2(3, 2);
Point p3 = p1 - p2;
p3.display();
return 0;
}
Question no 25:
#include <iostream>
using namespace std;
class Point {
int x, y;
public:
Point(int a, int b) : x(a), y(b) {}
Point& operator=(const Point& p) {
x = p.x;
y = p.y;
return *this;
}
void display() {
cout << "(" << x << ", " << y << ")" << endl;
}
};
int main() {
Point p1(1, 2), p2(3, 4);
p1 = p2;
p1.display();
return 0;
}
Question no26:
#include <iostream>
using namespace std;
class Compare {
int value;
public:
Compare(int v) : value(v) {}
bool operator>(Compare c) {
return value > c.value;
}
};
int main() {
Compare c1(10), c2(5);
if (c1 > c2)
cout << "c1 is greater than c2" << endl;
else
cout << "c2 is greater than c1" << endl;
return 0;
}
Question no 27:
#include <iostream>
using namespace std;
class Compare {
int value;
public:
Compare(int v) : value(v) {}
bool operator<(Compare c) {
return value < c.value;
}
};
int main() {
Compare c1(10), c2(5);
if (c1 < c2)
cout << "c1 is less than c2" << endl;
else
cout << "c2 is less than c1" << endl;
return 0;
}
Question no28:
#include <iostream>
using namespace std;
class Base {
public:
Base() {
cout << "Base Constructor" << endl;
}
~Base() {
cout << "Base Destructor" << endl;
}
};
int main() {
Derived d;
return 0;
}
Question no 29:
#include <iostream>
using namespace std;
class A {
public:
A() { cout << "A Constructor" << endl; }
~A() { cout << "A Destructor" << endl; }
};
class B : public A {
public:
B() { cout << "B Constructor" << endl; }
~B() { cout << "B Destructor" << endl; }
};
class C : public B {
public:
C() { cout << "C Constructor" << endl; }
~C() { cout << "C Destructor" << endl; }
};
int main() {
C c;
return 0;
}
Question no 30:
#include <iostream>
using namespace std;
class A {
public:
A() { cout << "A Constructor" << endl; }
};
class B {
public:
B() { cout << "B Constructor" << endl; }
};
int main() {
C c;
return 0;
}
Question no 31:
#include <iostream>
using namespace std;
class A {
public:
A() { cout << "A Constructor" << endl; }
~A() { cout << "A Destructor" << endl; }
};
class B : public A {
public:
B() { cout << "B Constructor" << endl; }
~B() { cout << "B Destructor" << endl; }
};
class C : public A {
public:
C() { cout << "C Constructor" << endl; }
~C() { cout << "C Destructor" << endl; }
};
class D : public B, public C {
public:
D() { cout << "D Constructor" << endl; }
~D() { cout << "D Destructor" << endl; }
};
int main() {
D d;
return 0;
}
Question no 32:
#include <iostream>
using namespace std;
class A {
public:
A() { cout << "A Constructor" << endl; }
~A() { cout << "A Destructor" << endl; }
};
int main() {
D d;
return 0;
}
Question no 33:
#include <iostream>
using namespace std;
class A {
public:
A() { cout << "A Constructor" << endl; }
~A() { cout << "A Destructor" << endl; }
};
class B : public A {
public:
B() { cout << "B Constructor" << endl; }
~B() { cout << "B Destructor" << endl; }
};
class C : public B {
public:
C() { cout << "C Constructor" << endl; }
~C() { cout << "C Destructor" << endl; }
};
int main() {
C c;
return 0;
}
Question no 34:
#include <iostream>
using namespace std;
class A {
public:
A() { cout << "A Constructor" << endl; }
};
class B {
public:
B() { cout << "B Constructor" << endl; }
};
Question no 35:
#include <iostream>
using namespace std;
class A {
public:
A() { cout << "A Constructor" << endl; }
~A() { cout << "A Destructor" << endl; }
};
class B : public A {
public:
B() { cout << "B Constructor" << endl; }
~B() { cout << "B Destructor" << endl; }
};
class C : public A {
public:
C() { cout << "C Constructor" << endl; }
~C() { cout << "C Destructor" << endl; }
};
int main() {
D d;
return 0;
}