Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

THEORY FOR CSE FINAL

#What is Inheritance? How many types of inheritance and what are they?
Ans) When members of base class are inherited by a derived class it is called inheritance.
a) Single/Simple Inheritance: When one class inherits another class, it is known as
single/simple inheritance.

Class Child is inheriting Class Parent.


So, we are calling it single inheritance.
Parent

Child
An object “c”, of Class Child is created
in the main function. With that object,
both the int variable “money” in Parent
class and “cash_out” function in Child
class is being accessed in the main
function.

b) Multi Level inheritance: When one class is inherited by a second class and the second
class is further inherited by the third class.

Grand_Parent

Parent

Child
Grand_Parent class is inherited by Parent
class which is further inherited by Child
class. Object “c” of child class is accessing
int old_money of Grand_Parent class,
new_money of Parent class and
“cash_out” function of Child class.
c) Multiple Inheritance: When one class inherits multiple classes then it is known as
multiple inheritance.

Father Mother

Child
Father class and Mother class is inherited
by Child class. Object “c” of child class is
accessing int f_money of Father class,
m_money of Mother class and “cash_out”
function of Child class.

d) Hierarchical Inheritance: When multiple classes inherit only one class, it is known as
hierarchical inheritance.
Parent

sister brother
Parent class is inherited by sister class and brother
class.
Object “s” of sister class is accessing int money
from Parent class, “sister_money” function of sister
class.
Again,
object “b” of brother class is accessing int money
from Parent class, “brother_money” function of
brother class.
e) Hybrid Inheritance: Hybrid inheritance is a combination of other types of inheritance.

⁠#) What is polymorphism? How does polymorphism work?


Ans) Polymorphism is the ability of something to take on multiple forms. Polymorphism
works in two ways. Compile Time and Run Time.
a) Compile Time Polymorphism: Polymorphism that is done at compile time. For example,
Function Overloading and Operator Overloading.
b) Run Time Polymorphism: Polymorphism that is done at run time. For example,
Virtual Functions.

Operator Overloading: The ability to change


the behavior of an operator is called operator
overloading. We use operator overloading to
do operations between objects.

Type 1: Adding numbers with objects. (Binary operations)

Note:
You MUST include an empty constructor!
Here, Area( ) { }; is an empty constructor.
In total two constructors, one empty/default
and one parameterized constructor is
declared. Then the overloading function,
Area operator + ( int a ) is declared. You
must create an object called temp.
Here a number is being added to an object.
Output:
Length: 14
Breadth: 16
Type 2: Adding objects with objects. (Binary operations)
Note:
You MUST include an empty constructor!
Here, Area( ) { }; is an empty constructor.
In total two constructors, one
empty/default and one parameterized
constructor is declared. Then the
overloading function,
Area operator + ( Area obj ) is declared.
You must create an object called temp.
Here an object is being added to another
object.
Output:
Length: 12
Breadth: 16

Type 3: Pre-Incrementing/Decrementing or Unary operations.

Note:
Empty constructor is not needed in
Type 3.
In total one constructor. Then the
overloading function,
void operator ++ ( ) is declared.
Here an object is being incremented.
Output:
Length: 14
Function Overloading: When two functions in a same class or scope have the same
name but different parameters it is known as Function overloading.

Function Overriding: When two functions have the same name in parent and child
class, if the function is called through an object of the child class, the program executes
the function from the child class, overriding the function from the parent class.

Here, the display function from the


parent class is being overridden by
the display function of the child
class. Function overriding is an
example of Early binding or Static
binding.
#) What is virtual function?
Ans) A virtual function (also known as virtual method) is a member function that is
declared within a base class and is re-defined (overridden) by a derived class.

#) What is pure virtual function/ Do nothing function?


Ans) A pure virtual function/ Do nothing function is a function that must be overridden
in a derived class and need not be defined.
Both Virtual function and Pure Virtual functions are examples of Late/Dynamic binding.

Virtual Function Pure Virtual Function

#) How does virtual functions achieve run time polymorphism?


Ans) If two functions in base class and derived class have the same name,
polymorphism is achieved. Normally the compiler decides which function to call at
compile time. But in case of virtual functions, polymorphism takes place and the
compiler cannot decide at compile time and has to call the function at run time.

#) Why is Multiple inheritance not supported in C++?


Ans) Multiple inheritance is not supported due to ambiguity. If there are multiple base
classes and one derived class and if all the base classes have variables and functions
using the same name, the compiler gets confused and cannot decide which one to call.
This can cause errors.
#) What is Early Binding? (Compile Time Polymorphism)
Ans) Early binding, also known as static binding, occurs when the compiler can
determine the exact function to be called at compile time. Example: operator
overloading, function overloading.

#) What is Late Binding? (Run Time Polymorphism)


Ans) Late binding, also known as dynamic binding, occurs when the specific function to
be called is determined at runtime. Example: Virtual function.

#) What is exception handling?


Ans) Exception handling is a mechanism in C++ that allows us to handle runtime errors
or unexpected situations in our program. There are two types of exceptions.

Synchronous: Exceptions caused by logical errors or wrong inputs.


Asynchronous: Exceptions caused by problems beyond the scope of the program.
Example: Disc failure, keyboard problems etc.

Important Keywords: Try, Throw and Catch.

#) Why is Exception Handling important?


Ans) Our code might have correct syntax but still be wrong due to logical errors. To
solve this problem, we will use exception handling in our code. In this way, when an
exception arises, our code will simply give an error message and wont crash.
#) What is Template?
Ans) Template is a blueprint or a formula used for creating Generic/ Template Class or
Template Function. Templates are independent of any data type.

Below are two programs which add two numbers using either Template/Generic Class
or Template Function.

Template Function

Template Class

#) What are the advantages of Template Class or Template Functions?


Ans) When we use a normal class or function, we have to use particular data types. For
example, when we use a function to add two numbers, we have to define which data
types to use beforehand. (int, double, float etc.). We cannot use other data types
without changing the code. By using template functions, we can use any data type
without changing the code.

You might also like