Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Output questions for oop

The document contains a series of C++ code snippets demonstrating various Object-Oriented Programming (OOP) concepts such as inheritance, polymorphism, constructors, destructors, and operator overloading. Each question presents a different scenario with accompanying code, showcasing how these OOP principles are implemented in C++. The examples include both base and derived classes, virtual functions, and the use of constructors and destructors in class hierarchies.

Uploaded by

f23ba111
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Output questions for oop

The document contains a series of C++ code snippets demonstrating various Object-Oriented Programming (OOP) concepts such as inheritance, polymorphism, constructors, destructors, and operator overloading. Each question presents a different scenario with accompanying code, showcasing how these OOP principles are implemented in C++. The examples include both base and derived classes, virtual functions, and the use of constructors and destructors in class hierarchies.

Uploaded by

f23ba111
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 30

Output questions for oop

Question no:1
#include <iostream>
using namespace std;

class Base {
public:
virtual void display() {
cout << "Base Class" << endl;
}
};

class Derived : public Base {


public:
void display() override {
cout << "Derived 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
};

class Circle : public Shape {


public:
void draw() override {
cout << "Drawing Circle" << endl;
}
};

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;
}
};

class Dog : public Animal {


public:
void sound() override {
cout << "Bark" << 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;
}
};

class Derived : public Base {


public:
~Derived() {
cout << "Derived 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;
};

class Dog : public Animal {


public:
void sound() {
cout << "Bark" << endl;
}
};

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;
}
};

class Derived : public Base {


public:
void show() override {
cout << "Derived 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;
}
};

class C : public A, public B {


public:
void show() {
cout << "C 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;
}
};

class Derived : public Base {


public:
Derived(int x) : Base(x) {
cout << "Derived 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;
}
};

class Derived : public Base {


public:
void speak() override {
cout << "Derived 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;
}
};

class Derived : public Base {};

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
};

class Derived : public Base {


public:
void print() override {
cout << "Derived Print" << endl;
}
};

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;
}
};

class C : public A, public B {


public:
void show() {
cout << "C 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;
}
};

class Derived : public Base {


public:
void display() {
cout << "Derived 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;
}
};

class Derived : public Base {


public:
void display() {
cout << "Derived 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;
}
};

class Derived : public Base {


public:
Derived() {
cout << "Derived Constructor" << endl;
}
~Derived() {
cout << "Derived 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; }
};

class C : public A, public B {


public:
C() { cout << "C 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; }
};

class B : virtual public A {


public:
B() { cout << "B Constructor" << endl; }
~B() { cout << "B Destructor" << endl; }
};

class C : virtual 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 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; }
};

class C : public A, public B {


public:
C() { cout << "C Constructor" << endl; }
};
int main() {
C c;
return 0;
}

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; }
};

class D : public B, public C {


public:
D() { cout << "D Constructor" << endl; }
~D() { cout << "D Destructor" << endl; }
};

int main() {
D d;
return 0;
}

You might also like