6. Function
Overloading and Inheritance.
It is the way to implementing
the OOP concept polymorphism. They are static and dynamic polymorphism.
Function overloading is static polymorphism it takes place during compile
time. Virtual function is dynamic polymorphism which is takes place
at run time.
Function Overloading
The
term ‘polymorphism’ means ‘one name having many forms’ or ‘different behavior
depending upon the situations’. Normally a function is designed to perform
single task. But, if a function is assigned with more than one task, we will
say that the function is overloaded.
Function
overloading is the process of defining functions with the same name but
different argument list. Here the formal and actual arguments should match its
type and number while invoking at the time of compiling. It is called compile
time polymorphism or early binding or static binding or static linking.
Declaration, Definition and Calling of Overloaded function.
The argument list
of a function is also known as function signature. The following definitions of
the function ‘area( )’ perform different task with different signatures.
I) The function definition to find the
area of a rectangle.
float area (float
a, float b)
{
return a*b;
}
II) The function definition to find the
area of a circle.
Float area (float
r)
{
return 3.14*r*r;
}
III) The function definition to find the
area of a triangle.
float area (int a,
int b, int c)
{
float
s=(a+b+c)/2.0;
return
sqrt(s*(s-a)*(s-b)*(s-c));
}
IV) The function definition to find the
area of a square.
int area (int n)
{
return n*n;
}
The above functions
are called with the following statements.
I) cout<<area(2.5, 3);
II) cout<<area(4.3);
III) cout<<area(3,6,5);
IV) cout<<area(3);
Finding the Best Match
A
call to an overloaded function is resolved through a process called argument
matching or process of disambiguation. Here the actual arguments are comparing
the formal arguments. The compiler finds best match through the following
steps.
i) Search for an exact match: If the
number and types of the actual arguments exactly matches with one function
definition, the compiler invokes that function.
ii) A match though promotion: If no exact
match found, an attempt is made to achieve a match through the the type
promotion of the actual arguments with its precedence.
iii) A match through conversion: If the
above two steps fail, an attempt is made to achieve a match through a standard
conversion of the actual argument.
iv) A match through user defined
conversion: If the above all steps fail the compiler will try the user defined
conversion to find a unique match.
The following program illustrates an
overload function namely ‘area()’ to find the area of various geometric figures
such as circle, triangle, rectangle and square.
#include<iostream.h>
#include<conio.h>
#include<math.h>
float area(float);
float area(float,float,float);
float area(float,float);
int area(int);
void main()
{
int choice;
do
{
clrscr();
cout<<"\t MAIN
MENU\n";
cout<<"1. AREA OF A
CIRCLE\n";
cout<<"2. AREA OF A
TRIANGLE\n";
cout<<"3. AREA OF A
RECTANGLE\n";
cout<<"4. AREA OF A
SQUARE\n";
cout<<"5. EXIT\n";
cout<<"ENTER YOUR
CHOICE\n";
cin>>choice;
switch(choice)
{
case 1:
float r;
cout<<"Enter the
radius:";
cin>>r;
cout<<"Area="<<area(r);
break;
case 2:
float a,b,c;
cout<<"Enter the 3
sides:";
cin>>a>>b>>c;
cout<<"Area="<<area(a,b,c);
break;
case 3:
float len,br;
cout<<"Enter lenth and
breadth:";
cin>>len>>br;
cout<<"Area="<<area(len,br);
break;
case 4:
int n;
cout<<"Enter the length of
aside:";
cin>>n;
cout<<"Area="<<area(n);
break;
case 5:
return;
default: cout<<"Invalid
choice";
}
getch();
}while(1);
}
float area(float r)
{
return(3.14*r*r);
}
float area(float a,float
b,float c)
{
float s,x;
s=(a+b+c)/2.0;
x=sqrt(s*(s-a)*(s-b)*(s-c));
return(x);
}
float area(float len, float br)
{
return(len*br);
}int area(int n)
{
return(n*n);
}
Constructor overloading
Constructor is a
function, it can also be overloaded. A constructor function which behaves
differently in different places called constructor overloading. But it has no
return type. If a class has an overloaded constructor, objects can be
initialized with different data values by providing necessary arguments.
class over
{
_______
_______
over(int x, int y)
{
_______
_______
}
over(int x, int y, float z)
{
_______
_______
}
};
void main()
{
over ob1(8,6);
over ob2(1,2,3.5);
}
Eg:
float fun(int a, float b=2.0,int c=5)
{
Return a+b+c;
}
The following calls returns to the
function
fun(25)// returns 32.0 ie 25+2.0+5
fun(25,4.0)//returns 34.0 ie 25+4.0+5
fun(25,4.0,10)//returns 39.0 ie
25+4.0+10
Constructor overloading
|
Function overloading
|
A constructor function which behaves
differently in different places called constructor overloading.
|
A Function which behaves differently in
different places called Function overloading.
|
It has no return type.
|
It has return type.
|
Same name that of class.
|
Different name of that of class.
|
Automatically invoke when objects are created.
|
User must call the function separately.
|
Functions with Default arguments V/s
Overloading
Default argument function
|
Overloaded function
|
It allows calling the function without
specifying all its arguments.
|
It does not allow calling the function without
specifying all its arguments.
|
If the argument missing it takes default value.
|
If the argument missing it produces an error.
|
It has only one function definition.
|
It may be have number of function definitions.
|
Inheritance
It
is the capability of deriving the properties of one class in to another, so it
allows creating new class from older one. The technique of building new classes
from the existing classes is called inheritance.
Base Class and Derived Class
The
class from which the properties are derived (i.e. the existing class) is called
base class. The class to which the properties are inherited (i.e. the new
classes) is called derived class.
The derived class inherits the
features of the base class (A, B and C), and adds its own features (D). The
arrow direction towards the base class represents that the derived class
accesses features of the base class and not vice versa.
A
base class is also called the ancestor or parent or super class. The derived
class is also called descendent or child or sub class.
Advantages of inheritance
It very closes to real life models.
It ensures the reusability of code.
It possesses transitive nature.
Defining Derived class
Syntax for defining a derived class
from a base class is as follows.
class derivedclassname :
visibilitymode baseclassname
{
Declaring data members and member
functions;
};
Here it posses all properties of
ordinary class.
Visibility Modes
They are keywords used as access
labels and they decide the accessibility of the inherited members within the
derived class. They are Private, Public, and Protected.
Impact of visibility modes
Private
|
Public
|
Protected
|
The public and protected members of the base
class become private members of derived class.
The inherited members can only be accessed
outside world through public member functions of the derived class
|
The public and protected members of the base
class become public and protected members of derived class respectively.
The accessibility of these members in the
derived class is same as that in the base class.
|
The public and protected members of the base
class become protected members of derived class.
The inherited members can only be accessed
outside world through public member functions of the derived class but can be
inherited further.
|
The above summarized as
Access specifier of members
|
Accessible from own class
|
Accessible from derived class
|
Accessible from objects outside class
|
Private
|
Yes
|
No
|
No
|
Public
|
Yes
|
Yes
|
Yes
|
Protected
|
Yes
|
Yes
|
No
|
Friendship V/S Derivation
Friendship
|
Derivation
|
Provide access to protected members.
|
Private members cannot be derived into another
class.
|
A non member function can make friendship to a
class.
|
Derivation shares the features of base class
and adds some more attributes.
|
Friend function acts as a bridge between two
independent classes.
|
Derived class is a special instance of base
class.
|
Friendship is not derivation.
|
Derivation is a kind of friendship.
|
Types of Inheritance
They are:
1. Single Inheritance
2. Multiple Inheritance
3. Hierarchical Inheritance
4. Multilevel Inheritance
5. Hybrid or Multipath Inheritance
Single Inheritance
Derivation
of one class from only one class is called single inheritance. In the figure
class Y is derived from class X.
Syntax for single inheritance.
class derivedclassname :
visibilitymode baseclassname
{
Declaring data members and member
functions;
};
Multiple Inheritances
1. Derivation of class from several (two or more) base classes is
called Multiple Inheritance. In the figure the class Z is derived from both the
class X and Y.
Syntax for Multiple inheritances.
class derivedclassname :
visibilitymode baseclassname1, visibilitymode baseclassname2
{
Declaring data members and member
functions;
};
Hierarchical Inheritance
Derivation of several classes from a
single base class is called Hierarchical Inheritance. In the figure the classes
Y and Z derived from class X.
Syntax for Hierarchical inheritances.
class derivedclassname1 :
visibilitymode baseclassname
{
Declaring data members and member
functions;
};
class derivedclassname2 :
visibilitymode baseclassname
{
Declaring data members and member
functions;
};
Multilevel Inheritance
When a
sub class is derived from abase class which itself is derived from another
class. In the figure the class Z is derived from the class Y and it is derived
from the class X.
Syntax for Multilevel inheritances.
class derivedclassname1 :
visibilitymode baseclassname
{
Declaring data members and member
functions;
};
class derivedclassname2 :
visibilitymode derivedclassname1
{
Declaring data members and member
functions;
};
Hybrid or Multipath Inheritance
Derivation
of class involving more than one form of inheritance is known as hybrid
inheritance. Figure shows an example
Syntax for Hybrid or Multipath Inheritance
class derivedclassname1 :
visibilitymode baseclassname
{
Declaring data members and member
functions;
};
class derivedclassname2 :
visibilitymode derivedclassname
{
Declaring data members and member
functions;
};
class derivedclassname3 :
visibilitymode derivedclassname1, visibilitymode derivedclassname2
{
Declaring data members and member
functions;
};
Abstract Class and Virtual Base Class
Virtual
function is a function that does not really exist but it appears real in some
part of program. A function declared with the use of keyword virtual is called
virtual function.
Syntax:
Virtual returntype
functionname(arguments)
A
virtual function equated to zero is called pure virtual function. It is a
function declared in base class that has no function body. A class containing
such pure virtual function is called abstract class
Virtual returntype functionname()=0
Containership V/S Inheritance
Containership
|
Inheritance
|
A class containing objects of another class as its member is
called container ship.
|
A class is derived from another class is called inheritance.
|
It has a relationship.
|
It is a kind of relationship.
|
Cannot share the properties
|
Can share the properties.
|
Cannot add new features.
|
Can add new features.
|
Constructors are invoked in the order of derived to base.
|
Constructors are invoked in the order of base to derive.
|
Destructors are invoked in the order of base to derive.
|
Destructors are invoked in the order of derived to base.
|
No comments:
Post a Comment