0% found this document useful (0 votes)
9 views8 pages

Unit 3 - Half

The document covers the concepts of polymorphism and inheritance in object-oriented programming, detailing compile-time and run-time polymorphism, including function and operator overloading. It explains how polymorphism allows operations to exhibit different behaviors based on data types and provides examples of function overloading and operator overloading in C++. Additionally, it discusses the constraints and rules associated with these concepts, emphasizing their importance in enhancing code flexibility and extensibility.

Uploaded by

dang2342
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)
9 views8 pages

Unit 3 - Half

The document covers the concepts of polymorphism and inheritance in object-oriented programming, detailing compile-time and run-time polymorphism, including function and operator overloading. It explains how polymorphism allows operations to exhibit different behaviors based on data types and provides examples of function overloading and operator overloading in C++. Additionally, it discusses the constraints and rules associated with these concepts, emphasizing their importance in enhancing code flexibility and extensibility.

Uploaded by

dang2342
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/ 8

UNIT 3

Polymorphism and Inheritance:


Syllabus: Polymorphism and Inheritance: Polymorphism - compile time
polymorphism – function overloading – operator overloading -. Inheritance -
types of inheritance - constructors and destructors in inheritance –
constraints of multiple inheritance Abstract base class – pure virtual
functions - run time polymorphism - function overriding.

Polymorphism
Polymorphism is another important OOP concept. Polymorphism, a Greek
term, means the ability to take more than one form. An operation may
exhibit different behaviours in different instances. The behaviour depends
upon the types of data used in the operation. For example, consider the
operation of addition. For two numbers, the operation will generate a sum. If
the operands are strings, then the operation would produce a third string by
concatenation. The process of making an operator to exhibit different
behaviours in different instances is known as operator overloading.

Figure illustrates that a single function name can be used to handle different
number and different types of arguments. This is something similar to a
particular word having several different meanings depending on the context.
Using a single function
name to perform d types
of tasks is known
function overloading.

Polymorphism plays an
important role in allowing
objects having different
internal structures to
share the same external
interface. This means that
a general class of
operations.

Type of polymorphism - Compile time


polymorphism
Compile time polymorphism is also known as Early binding, Static
binding and this is same as concept of overloading. There are two type of
function in this concept first is calling function and second is called
function. main( ) function is calling function and class function is called
function (as in example void add(int a, int b), void add(int a, int b, int c)
is called function). Binding refers to the linking of a called function
(procedure call) to the code to be executed in response to the calling function
call.
Prepared By: Dr. Dheresh Soni VIT Bhopal University Page 1
In static binding when called function (as in example void add(int a, int b),
void add(int a, int b, int c) ) is call by calling function (main ( ) function)
then which function is to respond to calling function is known at the time of
call. So at the time of compilation the called function respond and attached
with calling function. So it is known as compile time polymorphism. As
called function respond and attached with calling function call at compile
time so it is known as Static Binding. In compile time polymorphism called
function response to calling function at the time of compilation i.e. before
program executes, so it is also known as Early Binding.

In compile time polymorphism two are more same function name is defined
in class but parameter should be different in each function (as stated in
example). When function is called appropriate prototype is matched and
when prototype matches then function get called. This is also a concept of
overloading.

#include<iostream.h>
#include<conio.h>

class test
{
public:
void sum(int a,int b)
{
cout<<"compile time polymorphism-function overloading\n\n";
cout<<" Enter the value of a:";
cin>>a;
cout<<" Enter the value of b:";
cin>>b;
cout<<" Sum is: "<< a+b<<"\n";
}
void sum(int a,int b, int c)
{
cout<<" Enter the value of a:";
cin>>a;
cout<<" Enter the value of b:";
cin>>b;
cout<<" Enter the value of c:";
cin>>c;
cout<<" Sum is: "<< a+b+c<<"\n\n";
}
};

void main()
{
int x,y,z,i,j;
clrscr();
test t;
t.sum(i,j);
t.sum(x,y,z);

Prepared By: Dr. Dheresh Soni VIT Bhopal University Page 2


getch();
}

Run time polymorphism


Run time polymorphism is also known as late binding, Dynamic Binding
and this is same as concept of overriding. There are two type of function in
this concept first is calling function and second is called function. main( )
function is calling function and class function is called function (as in
example void show( ) is called function). Binding refers to the linking of a
called function (procedure call) to the code to be executed in response to the
calling function call.

In dynamic binding when called function ( as in example void show( ) ) is


call by calling function (main ( ) function) then which function is to respond
to calling function is not known until the time of the call. It is known when
program start in execution i.e. at run-time. So it is known as runtime
polymorphism. As called function respond and attached with calling
function call at run time so it is known as Dynamic Binding. In compile
time polymorphism called function response to calling function at the time of
compilation i.e. before program executes, but in run time polymorphism
called function do not respond at compile time, it respond at the time of
program execution so it is known as Late Binding.

In run time polymorphism when same function name is defined in base class
and derived class, this is also known as overriding concept. The function of
base class is preceded with keyword virtual to differentiate the function call.
When we call function from main then we use pointer to call function. A
function associated with a polymorphic reference depends on the dynamic
type of that reference. At run-time the code matching the object under
current reference will be called.

#include<iostream.h>
#include<conio.h>

class Base
{
public:
int i,j;
virtual void show()
{
cout<<" I am class Base \n";
cout<<"enter the value of i: ";
cin>>i;
cout<<"enter the value of j: ";
cin>>j;
cout<<"Answer is: "<<i+j;
}
};

Prepared By: Dr. Dheresh Soni VIT Bhopal University Page 3


class Derived: public Base
{
public:
int i,j;
void show()
{
cout<<"I am class Derived \n";
cout<<"enter the value of i: ";
cin>>i;
cout<<"enter the value of j: ";
cin>>j;
cout<<"Answer is: "<<i*j;
}
};

void main()
{
clrscr();
Base b;
Base *p,*q;
p=&b;
p->show();
cout<<"\n\n";
Derived d;
q=&d;
q->show();
getch();
}

Function overloading
As stated earlier overloading refers to the use of the same thing for
different purposes. C++ also permits overloading of functions. This means
that we can use the same function name to create functions that perform a
variety of different tasks. This is known as function polymorphism in OOP.

Using the concept of function overloading; we can design a family of


functions with one function name but with different argument lists. The
function would perform different operations depending on the argument list
in the function call. The correct function to be invoked is determined by
checking the number and type of the arguments but not on the function
type. For example, an overloaded add( ) function handles different types of
data as shown below:

// Declarations

int add(int a, int b); // prototype 1


int add{int a. int b, int c); // prototype 2
double add(double x, double y); // prototype 3
double addlint p. double q); // prototype 4

Prepared By: Dr. Dheresh Soni VIT Bhopal University Page 4


double add(double p. int q); // prototype 5

// Function calls

cout add(5, 10); //uses prototype 1


cout add(15, 10.0); //uses prototype 4
cout add(12.5, 7.5); //uses prototype 3
cout add(5, 10, 15); //uses prototype 2
cout add(0.75, 5) //uses prototype 5

A function call first matches the prototype having the same number and type
of arguments and then calls the appropriate function for execution, A best
match must be unique. The function selection involves the following steps;

1. The compiler first tries to find an exact match in which the types of actual
arguments are the same, and use that function.

2. If an exact match is not found, the compiler uses the integral promotions
to the actual arguments, such as, char to int, float to double to find a
match.

3. When either of them fails the compiler tries to use the built-in conversions
to the actual arguments and then uses the function whose match is
unique. If the conversion is possible to have multiple matches, then the
compiler will generate an error message. Suppose we use the following two
functions;

long square (long n)


double square(double x)

A function call such as square(10) will cause error because int argument
can be converted to either long or double, thereby creating an ambiguous
situation as to which version of square( ) should be used.
4. If all of the steps fail, then the compiler will try the user-defined
conversions combination with integral promotions and built-in
conversions to find a unique match, User-defined conversions are often
used in handling objects.

Program to illustrates function overloading.

#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
#define pi 3.14

class fn
{
public:
void area(int);
void area(int,int);

Prepared By: Dr. Dheresh Soni VIT Bhopal University Page 5


void area(float ,int,int);
};

void fn::area(int a)
{
cout<<"Area of Circle:";
cout<<pi*a*a;
}
void fn::area(int a,int b)
{
cout<<"Area of rectangle:";
cout<<a*b;
}

void fn::area(float t,int a,int b)


{
cout<<"Area of triangle:" ;
cout<<t*a*b;
}

void main()
{
int a,b,r;
clrscr();
fn obj;
cout<<"Function Overloading\n\n";
cout<<"Enter Radius of the Circle:";
cin>>r;
obj.area(r);
cout<<"Enter 1st Sides of the Rectangle:";
cin>>a;
cout<<"Enter 2nd Sides of the Rectangle:";
cin>>b;
obj.area(a,b);
cout<<"Enter 1st Sides of the Triangle:";
cin>>a;
cout<<"Enter 2nd Sides of the Triangle:";
cin>>b;
obj.area(0.5,a,b);
getch();
}

Operator overloading
Overloading means assigning different meanings to an operation, depending
on the context. Operator overloading is one of the important technique that
has enhanced the power of extensibility of C++. Operator overloading
provides a flexible option for the creation of new definitions for most of the
operator. We can almost create a new language of our own by the creative

Prepared By: Dr. Dheresh Soni VIT Bhopal University Page 6


use of the function and operator overloading techniques. We can overload
(give additional meaning to) all the C++ operators except the following:

1. Class member access operators (., .*)


2. Scope resolution operator (::)
3. Size operator (sizeof)
4. Conditional operator (?:)

Although the semantics of an operator can be extended, we cannot change


its syntax, the grammatical rules that govern its use such as the number of
operands, precedence and associativity.

To define an additional task to an operator, we must specify what it means


in relation to the class to which the operator is applied. This is done with the
help of a special function, called operator function. The general form of an
operator function is:

return type classname :: operator op(arglist)


{
Function body // task defined
}

where return type is the type of value returned by the specified operation
and op is the operator being overloaded. The op is preceded by the keyword
operator. operator op is the function name.

The process of overloading involves the following steps:

1. Create a class that defines the data type that is to be used in the
overloading operation.
2. Declare the operator function operator op( ) in the public part of the
class.
3. Define the operator function to implement the required operations.

Rules for Overloading Operators

Although it looks simple to redefine the operators, there are ce rtain


restrictions and limitations in overloading them. Some of them are listed
below:

1. Only existing operators can be overloaded. New operators cannot be


created.
2. The overloaded operator must have at least one operand that is of user-
defined type.
3. We cannot change the basic meaning of an operation That is to say, we
cannot redefine the plus (+) operator to Subtract one value from the
other.
4. Overloaded operators follow the syntax rules of the original operators.
They cannot overridden.
5. There are some operators that cannot be overloaded.

Prepared By: Dr. Dheresh Soni VIT Bhopal University Page 7


6. We cannot use friend functions to overload certain operators, However,
member functions can be used to overload them.

#include<iostream.h>
#include<stdlib.h>
#include<conio.h>

class Distance
{
private:
int feet;
int inches;
public:
Distance()
{
feet = 0;
inches = 0;
}

Distance(int f, int i)
{
feet = f;
inches = i;
}

void displayDistance()
{
cout<<"Entered feet value is: " << feet<<”\n”;
cout<<"Entered inches value is: " << inches<<”\n\n”;
}

Distance operator-()
{
feet = -feet;
inches = -inches;
return Distance(feet, inches);
}
};

void main()
{
Distance D1(11, 10), D2(-5, 11);
clrscr();
-D1;
D1.displayDistance();
-D2;
D2.displayDistance();
getch();
}

Prepared By: Dr. Dheresh Soni VIT Bhopal University Page 8

You might also like