Let's break down these C++ object-oriented programming questions from Unit-6.
Category - I (Easy)
1. Define inheritance in Object-oriented programming.
○ Inheritance is a mechanism where a new class (derived or child class) inherits
properties and behaviors from an existing class (base or parent class). It promotes
code reusability and establishes an "is-a" relationship between classes.
2. What are the different types of inheritance?
○ Single Inheritance: A class inherits from only one base class.
○ Multiple Inheritance: A class inherits from multiple base classes.
○ Hierarchical Inheritance: Multiple classes inherit from one base class.
○ Multilevel Inheritance: A class inherits from a class, which in turn inherits from
another class (a chain of inheritance).
○ Hybrid Inheritance: A combination of two or more types of inheritance.
3. What is a destructor?
○ A destructor is a special member function that is automatically called when an
object of a class goes out of scope or is explicitly deleted. Its primary purpose is to
release any resources (memory, files, etc.) that the object may have acquired
during its lifetime.
4. What is the difference between function overloading and operator overloading?
○ Function Overloading: Defining multiple functions with the same name but
different parameters (different number or types of arguments) within the same
scope. The correct function is called based on the arguments provided.
○ Operator Overloading: Defining how standard operators (like +, -, *, /, etc.) should
behave when applied to objects of a user-defined class. It allows you to use
operators with your classes as you would with built-in types.
5. What is Compile-time polymorphism?
○ Compile-time polymorphism (also known as static polymorphism or early binding) is
a type of polymorphism where the method or function call is resolved at compile
time. Function overloading and operator overloading are examples of compile-time
polymorphism.
Category - II (Moderate)
1. What is single inheritance and give an example.
○ Single inheritance is when a class inherits from only one base class.
class Animal {
public:
void eat() { cout << "Animal is eating" << endl; }
};
class Dog : public Animal {
public:
void bark() { cout << "Dog is barking" << endl; }
};
int main() {
Dog d;
d.eat(); // Inherited from Animal
d.bark(); // Specific to Dog
return 0;
}
2. What is multilevel inheritance? Provide an example.
○ Multilevel inheritance is when a class inherits from a class, which in turn inherits
from another class, forming a hierarchy.
class Grandparent {
public:
void grandparentFunc() { cout << "Grandparent's function" <<
endl; }
};
class Parent : public Grandparent {
public:
void parentFunc() { cout << "Parent's function" << endl; }
};
class Child : public Parent {
public:
void childFunc() { cout << "Child's function" << endl; }
};
int main() {
Child c;
c.grandparentFunc(); // Inherited from Grandparent
c.parentFunc(); // Inherited from Parent
c.childFunc(); // Specific to Child
return 0;
}
3. What is function overloading? Give an example.
○ Function overloading is defining multiple functions with the same name but different
parameters (different number or types of arguments).
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
int add(int a, int b, int c) { return a + b + c; }
int main() {
cout << add(2, 3) << endl; // Calls the first version
cout << add(2.5, 3.7) << endl; // Calls the second version
cout << add(2, 3, 4) << endl; // Calls the third version
return 0;
}
4. Differentiate between compile-time and run-time polymorphism.
Feature Compile-time Polymorphism Run-time Polymorphism (Late
(Early Binding) Binding)
Binding Time Resolved at compile time Resolved at runtime
Feature Compile-time Polymorphism Run-time Polymorphism (Late
(Early Binding) Binding)
Mechanism Function/Operator Virtual Functions
Overloading
Flexibility Less flexible More flexible
Speed Faster Slower
5. What is operator overloading?
○ Operator overloading allows you to redefine how standard operators work with
objects of your classes. For example, you can overload the + operator to add two
objects of your class together.
Category - III (Difficult/Standard)
1. How is hybrid inheritance implemented in C++? Discuss any issues associated with
it.
○ Hybrid inheritance is a combination of two or more types of inheritance. It's typically
implemented using a combination of single, multiple, and hierarchical inheritance.
Example:class A {};
class B : public A {};
class C : public A {};
class D : public B, public C {}; // Hybrid (both multilevel
and multiple)
Issues:
○ Ambiguity (Diamond Problem): When a class inherits from two classes that have
a common ancestor, there can be ambiguity if both parent classes have a member
with the same name. This is known as the "diamond problem." It can be resolved
using virtual inheritance.
○ Complexity: Hybrid inheritance can make the class hierarchy complex and harder
to understand and maintain.
2. Explain operator overloading using both member functions and friend functions
with examples.Member Function:
class Complex {
private:
int real, imag;
public:
Complex(int r = 0, int i = 0) { real = r; imag = i; }
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 = c1 + c2; // Operator overloading using member
function
c3.print();
return 0;
}
Friend Function:
class Complex { // ... (same as above)
friend Complex operator + (Complex const& c1, Complex const& c2);
};
Complex operator + (Complex const& c1, Complex const& c2) {
Complex res;
res.real = c1.real + c2.real;
res.imag = c1.imag + c2.imag;
return res;
} // ... (main function remains the same)
3. Discuss constructor and destructor calls with examples.
○ Constructors and destructors are automatically called during object creation and
destruction.
class MyClass {
public:
MyClass() { cout << "Constructor called" << endl; }
~MyClass() { cout << "Destructor called" << endl; }
};
int main() {
MyClass obj; // Constructor called
{
MyClass anotherObj; // Constructor called
} // Destructor called for anotherObj (goes out of scope)
return 0; // Destructor called for obj (end of main)
}
4. What is Hybrid inheritance? Explain with a diagram.
○ (Explained in point 1 of this category).
Diagram: A
/ \
B C
/ \
D E
In this diagram, D inherits from B, and E inherits from C. Both B and C inherit from A. This
combines multiple and hierarchical inheritance, making it a hybrid inheritance.
5. Explain polymorphism with a real-world example.
○ Polymorphism means "many forms." A real-world example is a "Shape" class with a
"draw()" method. You can have different shapes like "Circle," "Square," and
"Triangle" inheriting from the "Shape" class. Each shape overrides the "draw()"
method to draw itself specifically. When you have a collection of "Shape" objects,
you can call the "draw()" method on each object, and the correct version of the
method will be executed based on the actual shape of the object (even though they
are all treated as "Shape" objects).