OOP Paper Solution Demo
OOP Paper Solution Demo
Output : 12 + i9
2) Simplicity: Reduces the need for verbose function calls (e.g., add (a, b)
can be written as a + b).
---------------------------------------------------------------------
6) Use of const: If the operator does not modify the object, it should be
declared as const.
#include <iostream>
using namespace std;
class Increment {
private:
int ab;
public:
Increment()
ab(0)
{
}
// Overloading the post-increment operator ++
Increment operator++(int)
{
Increment dp(*this);
ab++;
return dp;
}
int get() const { return ab; }
};
int main()
{
Increment i1;
cout << "Before Increment- " << i1.get() << "\n";
Increment i2 = i1++; // Post-incrementing i1
cout << "After Increment- " << i1.get() << "\n";
cout << "i2 is- " << i2.get() << "\n";
return 0;
}
class A {
public:
void show() {
std::cout << "Class A" << std::endl;
}
};
--
class B : virtual public A {
};
Output
print derived class
show base class
Explain polymorphism and types of polymorphism in C++.
Differentiate between compile time polymorphism and run time
3
polymorphism. or
Abstract Class.
Polymorphism is a core concept in object-oriented programming that allows
objects of different classes to be treated as objects of a common base class.
- It refers to ability to take more than one form in the programming.
- Polymorphism can be compile time polymorphism and run time
polymorphism.
- Compile time polymorphism is possible with function & operator overl-
oading whereas runtime polymorphism is possible with virtual functions.
Function Overloading –
When there are multiple functions with same name but different parameters
then these functions are said to be overloaded. Functions can be overloaded by
change in number of arguments or/and change in type of arguments.
Examples
class Print {
public:
void show(int i) {
std::cout << "Integer: " << i << std::endl;
}
void show(double d) {
std::cout << "Double: " << d << std::endl;
}
};
Operator Overloading –
It allows developers to redefine the behavior of standard operators (like +, -, *,
etc.) for user-defined types, such as classes and structs. This enables these
Function Overriding-
It allows a derived class to provide a specific implementation of a function that
is already defined in its base class. This mechanism is fundamental to
achieving runtime polymorphism, where the program decides which function
to invoke at runtime based on the actual object type.
Example
class Base {
public:
virtual void show() {
std::cout << "Base class show function" << std::endl;
}
};
class Derived : public Base {
public:
void show() override {
Abstract Class
An abstract class is a class that cannot be instantiated on its own and is
designed to be a base class for other classes. It contains at least one pure
virtual function, which is a virtual function declared with = 0. Abstract classes
are used to define an interface for derived classes.
#include <iostream>
class Shape {
public:
virtual void draw() = 0; // Pure virtual function
};
class Circle : public Shape {
public:
void draw() override {
std::cout << "Drawing a Circle" << std::endl;
}
};
class Square : public Shape {
public:
void draw() override {
std::cout << "Drawing a Square" << std::endl;
}
};
int main() {
Shape* shape1 = new Circle();
Shape* shape2 = new Square();
shape1->draw(); // Output: Drawing a Circle
shape2->draw(); // Output: Drawing a Square
delete shape1;
delete shape2;
return 0;
}
Mechanism of Implementation
1. Virtual Table (Vtable):
o Each class with virtual functions has a vtable that contains pointers
to the virtual functions that can be called on instances of that class.
o The vtable is created at compile time and is linked to each instance
of the class.
2. Virtual Table Pointer (Vptr):
o Each object of a class with virtual functions contains a hidden
pointer (vptr) that points to the vtable of its class.
o This vptr is set during the construction of the object and enables
the program to determine which function to invoke at runtime.
3. Dynamic Dispatch:
o When a virtual function is called on an object through a base class
Example
#include <iostream>
class Animal {
public:
virtual void sound() { // Virtual function
std::cout << "Animal makes a sound" << std::endl;
}
};
class Dog : public Animal {
public:
void sound() override { // Override the base class function
std::cout << "Dog barks" << std::endl;
}
};
class Cat : public Animal {
public:
void sound() override { // Override the base class function
std::cout << "Cat meows" << std::endl;
}
};
int main() {
Animal* animal1 = new Dog(); // Base class pointer to derived class object
Animal* animal2 = new Cat();
animal1->sound(); // Output: Dog barks
animal2->sound(); // Output: Cat meows
delete animal1; // Clean up
delete animal2;
return 0;
}
class Counter {
private:
int value;
public:
// Constructor to initialize the counter
Counter(int v = 0) : value(v) {}
// Overloading the prefix increment operator (member function)
Counter& operator++() {
++value; // Increment the value
return *this; // Return the updated object
}
// Overloading the prefix decrement operator (member function)
Counter& operator--() {
--value; // Decrement the value
--
return *this; // Return the updated object
}
// Overloading the prefix decrement operator (friend function)
friend Counter operator--(Counter& counter, int) {
Counter temp = counter; // Save current state
--counter.value; // Decrement the value
return temp; // Return the original value before decrement
}
// Function to display the current value
void display() const {
std::cout << "Counter value: " << value << std::endl;
}
};
int main() {
Counter counter(5);
std::cout << "Initial: ";
▪ Instruction
1. All Unit Wise Solution available on telegram channel
2. Notes – Handwritten Notes / Textbook notes available
3. Join the telegram channel to get access to all.
4. Prepare above answer for 55+ marks in OOP Subject.
5. Free Project guides & Expert Help Available for Collaboration.
6. For the Customized Micro PDFs Solution of All Paper contact us on telegram.
7. Feel free to message us on Each single doubt, we will definitely help you regarding.….!!
---------------------------------------------------------------------------------------------------------------------------------
• Upto 9 CGPA
JOIN TELEGRAM CHANNEL : https://t.me/sppuengineersss
- All repeated Question paper | Last year PYQ With Solutions
- Each required Study materials with Best Notes / Handwritten
- Grab this all study materials by joining telegram channels and get
• Upto 9 CGPA