OOP'S _ Module -II_(Polymorphism)Notes
OOP'S _ Module -II_(Polymorphism)Notes
Polymorphism:
The word “polymorphism” means having many forms. In simple words, we can define
polymorphism as the ability of a message to be displayed in more than one form.
A real-life example of polymorphism is a person who at the same time can have different
characteristics.
A man at the same time is a father, a husband, and an employee. So the same person
exhibits different behaviour in different situations.
This is called polymorphism. Polymorphism is considered one of the important features of
Object-Oriented Programming.
Types of Polymorphism
Compile-time Polymorphism
Runtime Polymorphism
1. Compile-Time Polymorphism
This type of polymorphism is achieved by function overloading or operator overloading.
A. Function Overloading
When there are multiple functions with the same name but different parameters, then the
functions are said to be overloaded, hence this is known as Function Overloading.
Functions can be overloaded by changing the number of arguments or/and changing the
type of arguments. In simple terms, it is a feature of object-oriented programming
providing many functions that have the same name but distinct parameters when numerous
tasks are listed under one function name.
There are certain Rules of Function Overloading that should be followed while overloading
a function.
Below is the C++ program to show function overloading or compile-time polymorphism:
// Driver code
int main()
{
Geeks obj1;
value of x is 7
value of x is 9.132
value of x and y is 85, 64
B. Operator Overloading
C++ has the ability to provide the operators with a special meaning for a data type, this
ability is known as operator overloading. For example, we can make use of the addition
operator (+) for string class to concatenate two strings. We know that the task of this
operator is to add two operands. So a single operator ‘+’, when placed between integer
operands, adds them and when placed between string operands, concatenates them.
Below is the C++ program to demonstrate operator overloading:
// C++ program to demonstrate
// Operator Overloading or
// Compile-Time Polymorphism
#include <iostream>
using namespace std;
class Complex {
private:
int real, imag;
public:
Complex(int r = 0, int i = 0)
{
real = r;
imag = i;
}
// Driver code
int main()
{
Complex c1(10, 5), c2(2, 4);
Output
12 + i9
2. Runtime Polymorphism
A. Function Overriding
Function Overriding occurs when a derived class has a definition for one of the member
functions of the base class.
That base function is said to be overridden.
Function overriding Explanation
// Driver code
int main(void)
{
Animal d = Dog(); // accessing the field by reference
// variable which refers to derived
cout << d.color;
}
Output
Black
We can see that the parent class reference will always refer to the data member of the
parent class.
B. Virtual Function
A virtual function is a member function that is declared in the base class using the keyword
virtual and is re-defined (Overridden) in the derived class.
public:
// virtual function
virtual void display()
{
cout << "Called virtual Base Class function"
<< "\n\n";
}
void print()
{
cout << "Called GFG_Base print function"
<< "\n\n";
}
};
public:
void display()
{
cout << "Called GFG_Child Display Function"
<< "\n\n";
}
void print()
{
cout << "Called GFG_Child print Function"
<< "\n\n";
}
};
int main()
{
// Create a reference of class GFG_Base
GFG_Base* base;
GFG_Child child;
base = &child;
A virtual function (also known as virtual methods) is a member function that 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
method.
Virtual functions ensure that the correct function is called for an object, regardless of
the type of reference (or pointer) used for the function call.
They are mainly used to achieve Runtime polymorphism .
Functions are declared with a virtual keyword in a base class.
The resolving of a function call is done at runtime.
Compile time (early binding) VS runtime (late binding) behavior of Virtual Functions
Consider the following simple program showing the runtime behavior of virtual functions.
#include <iostream>
using namespace std;
class base {
public:
virtual void print() { cout << "print base class\n"; }
int main()
{
base* bptr;
derived d;
bptr = &d;
return 0;
}
Output
Unary Operators:
Unary Operator is an operator that operates on a single operand,
meaning it affects only one value or variable.
Unary operators are commonly used in programming languages to
perform various operations such as changing the sign of a value,
incrementing or decrementing a value, or performing logical negation.
Binary Operators:
Binary Operator is an operator that operates on two operands,
meaning it affects two values or variables.
Binary operators are commonly used in programming languages to
perform various operations such as arithmetic operations, logical
operations, and bitwise operations.
Number of
Operates on one operand Operates on two operands
Operands
Typically perform
Perform operations
operations on a single
involving two values
Operations value
C++ provides an inbuilt feature for Exception Handling. It can be done using the following
specialized keywords: try, catch, and throw with each having a different purpose.
Syntax of try-catch in C++
try {
// Code that might throw an exception
throw SomeExceptionType("Error message");
}
catch( ExceptionName e1 ) {
// catch block catches the exception that is thrown from try block
}
1. try in C++
The try keyword represents a block of code that may throw an exception placed inside the
try block. It’s followed by one or more catch blocks. If an exception occurs, try block
throws that exception.
2. catch in C++
The catch statement represents a block of code that is executed when a particular exception
is thrown from the try block. The code to handle the exception is written inside the catch
block.
3. throw in C++
An exception in C++ can be thrown using the throw keyword. When a program encounters
a throw statement, then it immediately terminates the current function and starts finding a
matching catch block to handle the thrown exception.
Note: Multiple catch statements can be used to catch different type of exceptions thrown by
try block.
The try and catch keywords come in pairs: We use the try block to test some code and If the
code throws an exception we will handle it in our catch block.
1. Separation of Error Handling Code from Normal Code: There are always if-else
conditions to handle errors in traditional error handling codes.
2. These conditions and the code to handle errors get mixed up with the normal flow. This
makes the code less readable and maintainable.
3. With try/catch blocks, the code for error handling becomes separate from the normal
flow.
2. Functions/Methods can handle only the exceptions they choose: A function can
throw many exceptions, but may choose to handle some of them.
3. The other exceptions, which are thrown but not caught, can be handled by the caller. If
the caller chooses not to catch them, then the exceptions are handled by the caller of the
caller.
In C++, a function can specify the exceptions that it throws using the throw keyword.
The caller of this function must handle the exception in some way (either by specifying
it again or catching it).
3. Grouping of Error Types: In C++, both basic types and objects can be thrown as
exceptions. We can create a hierarchy of exception objects, group exceptions in
namespaces or classes, and categorize them according to their types.
Example 1
The below example demonstrates throw exceptions in C++.
#include <iostream>
#include <stdexcept>
using namespace std;
int main()
{
// try block
try {
int numerator = 10;
int denominator = 0;
int res;
return 0;
}
Output
Exceptions may break the structure or flow of the code as multiple invisible exit points
are created in the code which makes the code hard to read and debug.
If exception handling is not done properly can lead to resource leaks as well.
It’s hard to learn how to write Exception code that is safe.
There is no C++ standard on how to use exception handling, hence many variations in
exception-handling practices exist.
File handling
File handling is used to store data permanently in a computer.
Using file handling we can store our data in secondary memory (Hard disk).
How to achieve the File Handling
For achieving file handling we need to follow the following steps:-
STEP 1-Naming a file
STEP 2-Opening a file
STEP 3-Writing data into the file
STEP 4-Reading data from the file
STEP 5-Closing a file.
Streams in C++ :-
We give input to the executing program and the execution program gives back the output.
The sequence of bytes given as input to the executing program and the sequence of bytes
that comes as output from the executing program are called stream.
In other words, streams are nothing but the flow of data in a sequence.
The input and output operation between the executing program and the devices like
keyboard and monitor are known as “console I/O operation”.
The input and output operation between the executing program and files are known as “disk
I/O operation”.
1. ios:-
ios stands for input output stream.
This class is the base class for other classes in this class hierarchy.
This class contains the necessary facilities that are used by all the other derived classes
for input and output operations.
2. istream:-
istream stands for input stream.
This class is derived from the class ‘ios’.
This class handle input stream.
The extraction operator(>>) is overloaded in this class to handle input streams from
files to the program execution.
This class declares input functions such as get(), getline() and read().
3. ostream:-
ostream stands for output stream.
This class is derived from the class ‘ios’.
This class handle output stream.
The insertion operator(<<) is overloaded in this class to handle output streams to files
from the program execution.
This class declares output functions such as put() and write().
4. streambuf:-
This class contains a pointer which points to the buffer which is used to manage the
input and output streams.
5. fstreambase:-
This class provides operations common to the file streams. Serves as a base for fstream,
ifstream and ofstream class.
In C++, files are mainly dealt by using three classes fstream, ifstream, ofstream available in
fstream headerfile.
Difference:
r mode r+ mode
fopen Returns if
NULL Create New File
FILE doesn’t exists
fopen returns if FILE Returns a pointer to the New data is written at the
exist FILE object. start of existing data
file pointer position at the first char of the file at the first char of the file