notes
notes
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.
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.
Runtime Polymorphism
This type of polymorphism is achieved by Function Overriding. Late binding
and dynamic polymorphism are other names for runtime polymorphism. The
function call is resolved at runtime in runtime polymorphism. In contrast,
with compile time polymorphism, the compiler determines which function
call to bind to the object after deducing it at runtime.
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.
There are two forms of switch statements. The switch statement in C++ is
unstructured as compared to the structured switches in modern languages
like Pascal. In a structured switch statement, it takes one branch, the
unstructured switch functions as the go-to type. The main keywords in switch
statements are the case, inspect, select, etc.
The switch statement in C++ improves the clarity in C++ programming and
reduces the bulkiness of repetitive coding. It ensures easy compiler
optimization and quick execution
C++ Switch Statement Syntax
The switch statement that compares the expression’s value with every single
case, for example, cases x and y is mentioned below.
Switch(expression) {
Case x:
// code block
break;
case y:
// code block
break;
default;
// code block
}
Output-
Enter a string:saas
The string is Palindrome
Output:
Enter three numbers: 2.3
8.3
-4.2
Largest number: 8.3
return 0;
}
Output
Enter marks of 5 subjects
97 89 78 87 68
Grade : B
// If y == 2
case 2:
cout << "Choice is 2";
break;
// If y == 3
case 3:
cout << "Choice is 3";
break;
}
break;
// If x == 4
case 4:
cout << "Choice is 4";
break;
// If x == 5
case 5:
cout << "Choice is 5";
break;
default:
cout << "Choice is other than 1, 2 3, 4, or 5";
}
return 0;
}
Output:
Choice is 2
#include <iostream>
using namespace std;
int main() {
int number;
if (number >= 0) {
cout << "You entered a positive integer: " << number << endl;
}
else {
cout << "You entered a negative integer: " << number << endl;
}
return 0;
}
Output:
Enter an integer: 4
You entered a positive integer: 4.
This line is always printed.