0% found this document useful (0 votes)
25 views3 pages

CSE Final Theory

Uploaded by

Tanvir Tareq
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)
25 views3 pages

CSE Final Theory

Uploaded by

Tanvir Tareq
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/ 3

1. What is inheritance? Describe the multiple types of inheritance.

Inheritance is a fundamental concept in object-oriented programming (OOP) where


one class can inherit properties and behavior from another class. The child class
inherits the attributes and methods of the parent class and can also add new
attributes and methods or override the ones inherited from the parent class.

There are two main types of inheritance:

Single inheritance: In this type, a child class inherits from a single parent class.

Multiple inheritance: In this type, a child class can inherit from multiple parent
classes.

Multilevel inheritance: In this type, a child class inherits from a parent class that
itself inherits from another parent class.

Hierarchical inheritance: In this type, a child class inherits from a parent class, and
multiple child classes can inherit from the same parent class.

Hybrid inheritance: This type combines multiple inheritance with other types of
inheritance.

2. What is polymorphism? How does polymorphism work?

Polymorphism is the ability of an object to take on multiple forms. In OOP, this means
that an object of a particular class can behave like an object of a different class.
Polymorphism is achieved through method overriding or method overloading.

Method overriding: When a child class provides a specific implementation for a


method that is already defined in its parent class.

Method overloading: When multiple methods with the same name but different
parameter lists are defined in a class.
3. How virtual function achieve run-time polymorphism?

Virtual functions are used to achieve run-time polymorphism. A virtual function is


declared in the base class and overridden in the derived classes. When a virtual
function is called on a base class pointer, the correct implementation is called based
on the actual object being pointed to, which is determined at runtime.

4. Multiple inheritance cannot be supported in C++ why?

C++ does not support multiple inheritance directly because of the diamond problem.
This problem arises when two classes (A and B) inherit from a common base class
(C) and then another class (D) inherits from both A and B. The common base class
(C) would be inherited twice, which can lead to ambiguity and confusion.

5. Describe pure virtual and virtual function

Pure Virtual Function: A pure virtual function is a function that is declared in an


abstract class and has no implementation. It is declared using the = 0 syntax. Any
non-abstract class that inherits from an abstract class must provide an
implementation for all pure virtual functions.

Virtual Function: A virtual function is a function that is declared in a base class and
can be overridden in derived classes. Virtual functions are declared using the virtual
keyword.

6. Define exception handling. Describe the importance of exception handling

Exception handling is a mechanism that allows a program to handle unexpected


errors or exceptions that occur during execution. It provides a way to catch and
handle exceptions, preventing the program from crashing or terminating
abnormally.
Importance of exception handling:
Improved reliability: Exception handling helps to prevent errors from propagating to
higher levels of the program.
Improved maintainability: Exception handling makes it easier to debug and
maintain code by providing a way to handle exceptions explicitly.
Improved performance: Exception handling can reduce the overhead of error
handling by allowing exceptions to be handled locally.

7. Advantage of template function

The advantages of template functions are:


Generality: Template functions can be used with different data types, making them
more flexible and reusable.
Efficiency: Template functions can be optimized for specific data types, resulting in
better performance.
Type safety: Template functions ensure type safety by checking the types of
variables at compile-time.

Here's an example C++ program that uses template functions to add two numbers:

#include <iostream>
template <typename T>
T add(T a, T b) {
return a + b;
}
int main() {
int x = 5;
int y = 3;
std::cout << "Addition of " << x << " and " << y << " = " << add(x, y) << std::endl;

float x1 = 3.5;
float y1 = 2.2;
std::cout << "Addition of " << x1 << " and " << y1 << " = " << add(x1, y1) << std::endl;

return 0;
}
This program uses a template function add that takes two arguments of type T and
returns their sum. The main function demonstrates how to use this template function
with integers and floats.

You might also like