0% found this document useful (0 votes)
5 views17 pages

OOP Paper Solution Demo

This document is a question bank for the S.E. (Computer Engineering) course focusing on Object Oriented Programming, specifically covering polymorphism, operator overloading, and virtual functions in C++. It provides key concepts, examples, and rules related to these topics, along with sample code snippets to illustrate the implementation in C++. The document serves as a study guide for students preparing for exams in this subject area.

Uploaded by

omkarkale1206
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views17 pages

OOP Paper Solution Demo

This document is a question bank for the S.E. (Computer Engineering) course focusing on Object Oriented Programming, specifically covering polymorphism, operator overloading, and virtual functions in C++. It provides key concepts, examples, and rules related to these topics, along with sample code snippets to illustrate the implementation in C++. The document serves as a study guide for students preparing for exams in this subject area.

Uploaded by

omkarkale1206
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

S.E.

(Computer Engineering) (Semester – III)


SPPU EXAM MOST IMP QUESTIONS (2019 Pattern)
Question Bank [Solved all Question Paper]

210243 : OBJECT ORIENTED PROGRAMMING


(2019 Pattern) (Semester - III) (210243)

• Chapter Wise Most IMP Answer Key –

UNIT – 3 Polymorphism Marks


Polymorphism- Introduction to Polymorphism, Types of
Polymorphism, Operator Overloading-concept of overloading,
operator overloading, Overloading Unary Operators,
Overloading Binary Operators, Data Conversion, Type casting
(implicit and explicit), Pitfalls of Operator Overloading and
17 Marks
Conversion, Keywords explicit and mutable. Function
overloading, Run Time Polymorphism- Pointers to Base class,
virtual function and its significance in C++, pure virtual
function and virtual table, virtual destructor,
abstract base class.

Sr. Questions & Answers


What is operator overloading? Explain need of operator overloading.
1
What are the rules for over loading operators.
Operator Overloading -
Operator overloading in C++ is a feature that allows developers to redefine the
way operators (like +, -, *, etc.) work with user-defined types (classes and
structs).
-- This enables the use of these operators in a manner similar to how they are
used with built-in types, enhancing code readability and expressiveness.

With operator overloading we can extend the facility of C++


In C++, operator overloading can also implemented by defining special
member functions or friend functions that specify the behavior of the operator

Join Telegram Channel - https://t.me/sppuengineersss | ©Vishal Ghuge 1


when applied to instances of a class.

//C++ program to illustrate Operator Overloading


#include<iostream>
using namespace std;
class Complex {
private:
int real, imag;
public:
Complex(int r = 0, int i = 0) {real = r; imag = i;}
//This is automatically called when'+' is used with between two Complex
objects
Complex operator + (Complex const &obj) {
Complex res;
res.real = real + obj.real;
res.imag = imag + obj.imag;
return res;
}
void print() { cout << real << “+ i” <<imag<<endl; }
};
int main()
{
Complex c1(10, 5), c2(2, 4);
Complex c3 = cl + c2;
// An example call to "operator+"
c3.print();
}

Output : 12 + i9

Operators which cannot be overloaded are as follows:-


1) Scope resolution operator (::)
2) Size of operator
3) Conditional operator (?:)
4) Class access operator (.)

Join Telegram Channel - https://t.me/sppuengineersss | ©Vishal Ghuge 2


---------------------------------------------------------------------
• Explain need of operator overloading –
1) Readability: It allows you to use operators in a way that is consistent
with their use in mathematical notation, making the code easier to
understand.

2) Simplicity: Reduces the need for verbose function calls (e.g., add (a, b)
can be written as a + b).

3) Consistency: Allows user-defined types to behave like built-in types,


promoting a consistent interface.

4) Natural Expressions: You can express operations naturally, such as


adding two vectors or concatenating strings.

---------------------------------------------------------------------

• What are the rules for over loading operators –


1) Defined as Member Functions or Friend Functions: Operators can be
overloaded as member functions or as friend functions. A member
function must take one argument, while a friend function can take two.

2) Cannot Change the Number of Operands: The number of operands for an


operator cannot be changed. For instance, you cannot create a new
operator that takes three operands.

3) Some Operators Cannot Be Overloaded: Certain operators, such as ::


(scope resolution), .* (pointer to member), and . (member access),
cannot be overloaded.

4) Preserve Logical Meaning: The behavior of the overloaded operator


should be intuitive and consistent with its usual meaning.

5) Return Type: Most overloaded operators should return a value, which


can be a new instance of the class or a modified instance.

6) Use of const: If the operator does not modify the object, it should be
declared as const.

Join Telegram Channel - https://t.me/sppuengineersss | ©Vishal Ghuge 3


Example –

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

Join Telegram Channel - https://t.me/sppuengineersss | ©Vishal Ghuge 4


Explain virtual base class & virtual function with example?
2
How virtual functions are implemented in C++?
Virtual Base Class –
A virtual base class is used in C++ to prevent multiple instances of a base class
when using multiple inheritance.
It helps to ensure that only one copy of the base class is present in the
hierarchy, which is particularly useful in cases of diamond inheritance.
The purpose of using a virtual base class is to ensure that only one instance of
the base class is included in the derived class. This makes it easier to manage
and access base class members without ambiguity

class A {
public:
void show() {
std::cout << "Class A" << std::endl;
}
};
--
class B : virtual public A {
};

class C : virtual public A {


};

class D : public B, public C {


};
-------------------------------------------------------------------------------
Virtual function –
A virtual function is a member function which is declared within a base class
and is re-defined, (Overridden) by a derived class. When you refer to a derived
class object using a pointer or a reference to the base class, you can call a
virtual function for that object and execute the derived class's version of the
function.
Virtual functions ensure that the correct function. is called for an object,

Join Telegram Channel - https://t.me/sppuengineersss | ©Vishal Ghuge 5


regardless of the type of reference (or pointer) used for function call.
- They are mainly used to achieve Runtime polymorphism
- Functions are declared with a virtual keyword in base class.
- The resolving of function call is done at Run-time

Rules for Virtual Functions


- Virtual functions cannot be static and also cannot be a friend function. of
another class.
- Virtual functions should be accessed using pointer or reference of base
class type to achieve run time polymorphism.
- The prototype of virtual functions should be same in base as well as
derived class.
- They are always defined in base class and overridden in derived class. It
is not mandatory for derived class to override (or re-define the virtual
function), in that case base class version of function is used.
- A class may have virtual destructor but it cannot have a virtual
constructor.
- Consider the following Program showing run-time behaviour of virtual
functions.

//C++ program to illustrate concept of Virtual Functions


#include<iostream>
using namespace std;
class base (
public:
virtual void print()
cout <<"print base class" <<endl;
}
void show()
cout<<"show base class" <<endl;

Join Telegram Channel - https://t.me/sppuengineersss | ©Vishal Ghuge 6


};
class derived: public base {
public:
void print()
{
cout<<"print derived class" <<endl;
void show()
{
cout<<"show derived class" <<endl;
};
int main()
{
base *bptr;
derived d;
bptr = &d;
//virtual function, binded at runtime
bptr->print();
// Non-virtual function, binded at compile time
bptr->show();
}

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.

Join Telegram Channel - https://t.me/sppuengineersss | ©Vishal Ghuge 7


Types of Polymorphism
Polymorphism is a feature of OOPs that allows the object to behave differently
in different conditions. In C++ we have two types of polymorphism:
1. Compile time Polymorphism - This is also known as static (or early)
binding.
2. Runtime Polymorphism - This is also known as dynamic (or late) binding.

Compile-time Polymorphism (Static Polymorphism):


- Achieved through function overloading and operator overloading.
- The decision about which function to invoke is made at compile time.
- It allows multiple functions with the same name but different parameters

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

Join Telegram Channel - https://t.me/sppuengineersss | ©Vishal Ghuge 8


operators to work with objects in a way that is intuitive and similar to how
they are used with built-in types, enhancing code readability and
expressiveness.
Examples
class Complex {
public:
double real, imag;
Complex operator+(const Complex& other) {
return Complex{real + other.real, imag + other.imag};
}
};

Run-time Polymorphism (Dynamic Polymorphism):


- Achieved through function overriding and virtual functions.
- The decision about which function to invoke is made at runtime
based on the actual object type.
- This allows a base class reference or pointer to refer to
derived class objects.

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 {

Join Telegram Channel - https://t.me/sppuengineersss | ©Vishal Ghuge 9


std::cout << "Derived class show function" << std::endl;
}
};
void display(Base* obj) {
obj->show(); // Calls the appropriate show function based on the actual
object type
}

Compile-time Polymorphism Run-time Polymorphism


Resolved during program execution
Resolved during compilation.
(runtime).
Achieved through function
Achieved through function
overloading and operator
overriding (using virtual functions).
overloading.
Function to be called is determined at Function to be called is determined at
compile time. runtime based on the object type.
Slightly slower due to dynamic
Generally faster due to static binding.
binding overhead.
Requires the same function name and
Requires the same function name but
parameters in the base and derived
different parameters or operators.
classes.
No inheritance required; can occur Requires a class hierarchy (base and
within the same class. derived classes).
Provides better performance and is Provides more flexibility and
less flexible. adaptability in code.

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.

Join Telegram Channel - https://t.me/sppuengineersss | ©Vishal Ghuge 10


Characteristics of Abstract Classes
1) Cannot Be Instantiated: You cannot create objects of an abstract class.
2) Contains Pure Virtual Functions: At least one member function must be
pure virtual.
3) Provides a Template: Derived classes must implement the pure virtual
functions to become concrete (non-abstract) 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;
}

Join Telegram Channel - https://t.me/sppuengineersss | ©Vishal Ghuge 11


What is Pure virtual function? or
4
How virtual functions are implemented in C++?
Pure Virtual Function
A pure virtual function is a virtual function in a base class that has no
implementation in that class and is declared by assigning 0 in its declaration.
This concept is used to define an interface that must be implemented by
derived classes. When a class contains at least one pure virtual function, it
becomes an abstract class, meaning it cannot be instantiated directly.
- It is type of virtual function which is not defined in the base class.
- Only derived class can define the function.
- It has notation =0 during the declaration and no definition for
function.then that class is called as abstract class.
- All derive classes should define the functions until it is abstract.
- Declaration of pure virtual function is as follows:
virtual return typefun_name()=0;

Virtual Functions Implementation in C++


-- n C++, virtual functions enable dynamic polymorphism and are implemented
using a mechanism that involves a virtual table (vtable) and a virtual table
pointer (vptr).

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

Join Telegram Channel - https://t.me/sppuengineersss | ©Vishal Ghuge 12


pointer or reference, the program looks up the function in the
vtable using the vptr to call the correct function corresponding to
the actual object type.

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

Join Telegram Channel - https://t.me/sppuengineersss | ©Vishal Ghuge 13


Write a C++ program for unary increment (++) and decrement (--)
5
operator overloading. (or using friend function)
#include <iostream>

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: ";

Join Telegram Channel - https://t.me/sppuengineersss | ©Vishal Ghuge 14


counter.display();
++counter; // Increment using member function
std::cout << "After increment: ";
counter.display();

--counter; // Decrement using member function


std::cout << "After decrement (member): ";
counter.display();
counter--; // Decrement using friend function
std::cout << "After decrement (friend): ";
counter.display();
return 0;
}

▪ 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.….!!

---------------------------------------------------------------------------------------------------------------------------------

• Free Question Paper Set Available


• Free Hand Written Notes / PDF Format Books Available
• Model Answer / Question Paper Solution Available
• College Level / Industrial Level Project Guides
• Joined Telegram Channel - https://t.me/sppuengineersss

Join Telegram Channel - https://t.me/sppuengineersss | ©Vishal Ghuge 15


JOIN TELEGRAM CHANNEL : https://t.me/sppuengineersss
- Access all OOP Study Materials | All Subjects Study Materials
- 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
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

You might also like