Unit-5 (Polymorphism)
Unit-5 (Polymorphism)
Polymorphism
The term "Polymorphism" is the combination of "poly" + "morphs"
which means many forms. It is a greek word. In object-oriented
programming, we use 3 main concepts: inheritance, encapsulation, and
polymorphism.
Operator overloading
Method Overloading
Function Overloading is defined as the process of
having two or more function with the same name,
but different in parameters is known as function
overloading in C++. In function overloading, the
function is redefined by using either different types
of arguments or a different number of arguments. It
is only through these differences compiler can
differentiate between the functions.
#include <iostream>
using namespace std;
class Cal {
public:
static int add(int a,int b){
return a + b;
}
static int add(int a, int b, int c)
{
return a + b + c;
}
};
int main(void) {
Cal C; // class object declaration.
cout<<C.add(10, 20)<<endl;
cout<<C.add(12, 20, 23);
return 0;
}
Output:
30
55
Operator Overloading
Where the return type is the type of value returned by the function.
#include <iostream>
using namespace std;
class Test
{
private:
int num;
public:
Test(): num(8){}
void operator ++() {
num = num+2;
}
void Print() {
cout<<"The Count is: "<<num;
}
};
int main()
{
Test tt;
++tt; // calling of a function "void operator ++()"
tt.Print();
return 0;
}
The Count is: 10
Method Overriding
If derived class defines same function as defined in
its base class, it is known as function overriding in
C++. It is used to achieve runtime polymorphism. It
enables you to provide specific implementation of
the function which is already provided by its base
class.
#include <iostream>
using namespace std;
class Animal {
public:
void eat(){
cout<<"Eating...";
}
};
class Dog: public Animal
{
public:
void eat()
{
cout<<"Eating bread...";
}
};
int main(void) {
Dog d = Dog();
d.eat();
return 0;
}
Output:
Eating bread...
#include <iostream>
using namespace std;
class Animal {
public:
void eat(){
cout<<"Eating...";
}
};
class Dog: public Animal
{
public:
void eat()
{ cout<<"Eating bread...";
}
};
int main() {
Dog d = Dog();
d.eat();
return 0;
}
Output:
Eating bread...
#include <iostream>
using namespace std;
class Shape { // base class
public:
virtual void draw(){ // virtual function
cout<<"drawing..."<<endl;
}
};
class Rectangle: public Shape // inheriting Shape class.
{
public:
void draw()
{
cout<<"drawing rectangle..."<<endl;
}
};
class Circle: public Shape // inheriting Shape class.
{
public:
void draw()
{
cout<<"drawing circle..."<<endl;
}
};
int main() {
Shape *s; // base class pointer.
Shape sh; // base class object.
Rectangle rec;
Circle cir;
s=&sh;
s->draw();
s=&rec;
s->draw();
s=○
s->draw();
}
drawing...
drawing rectangle...
drawing circle...
o When the function has no definition, such function is known as "do-nothing" function.
o The "do-nothing" function is known as a pure virtual function. A pure virtual function is a
function declared in the base class that has no definition relative to the base class.
#include <iostream>
using namespace std;
class Base
{
public:
virtual void show() = 0;
};
class Derived : public Base
{
public:
void show()
{
cout << "Derived class is derived from the base class." << std::endl;
}
};
int main()
{
Base *bptr;
//Base b;
Derived d;
bptr = &d;
bptr->show();
return 0;
}
Exception Handling
Exception Handling in C++ is a process to handle runtime errors. We
perform exception handling so the normal flow of the application can be
maintained even after runtime errors.
In C++, exception is an event or object which is thrown at runtime. All
exceptions are derived from std::exception class. It is a runtime error
which can be handled. If we don't handle the exception, it prints
exception message and terminates the program.
Try
Catch
throw
The try statement allows you to define a block of code to be tested for errors while it is
being executed.
The throw keyword throws an exception when a problem is detected, which lets us create
a custom error.
The catch statement allows you to define a block of code to be executed, if an error occurs
in the try block.
try
{
// Block of code to try
throw exception; // Throw an exception when a problem
arise
}
catch () {
// Block of code to handle errors
}
o try
o catch, and
o throw
#include <iostream>
using namespace std;
float division(int x, int y)
{
if( y == 0 )
{
throw "Attempted to divide by zero!";
}
return (x/y);
}
int main () {
int i = 25;
int j = 0;
float k = 0;
try {
k = division(i, j);
cout << k << endl;
}
catch (const char* e) {
cerr << e << endl;
}
return 0;
}
Output:
Attempted to divide by zero!
#include <iostream>
using namespace std;
int main() {
try {
int age = 15;
if (age >= 18) {
cout << "Access granted - you are old enough.";
} else {
throw (age);
}
}
catch (int myNum) {
cout << "Access denied - You must be at least 18 years old.\n";
cout << "Age is: " << myNum;
}
return 0;
}
Multiple try and catch statements
#include <iostream>
int main()
try {
try{
throw 20;
catch (int n) {
throw;//Re-throwing an exception
catch (int n) {
return 0;
To read and write from a file we are using the standard C++ library
called fstream. Let us see the data types define in fstream library is:
fstream It is used to create files, write information to files, and read information from files.
#include <iostream>
#include <fstream>
using namespace std;
int main () {
ofstream filestream("a.txt");
if (filestream.is_open())
{
filestream << "Welcome \n";
filestream << "C++ \n";
filestream.close();
}
else cout <<"File opening is fail.";
return 0;
}