Monday, 22 January 2018

Object Oriented Programming: Number: 3

No: 03


1. Experiment vision: To write a program on the concept of function overloading and
operator overloading.
2. OBJECTIVE: Program to implement function overloading and operator overloading in C++.
3.1 THEORY:
Function overloading in C++:
  • C++ allows you to specify more than one definition for a function name or an operator in the same scope, which is called function overloading and operator overloading respectively.
  • An overloaded declaration is a declaration that had been declared with the same name as a previously declared declaration in the same scope, except that both declarations have different arguments and obviously different definition (implementation).
  • You can have multiple definitions for the same function name in the same scope. The definition of the function must differ from each other by the types and/or the number of arguments in the argument list. You can not overload function declarations that differ only by return type.
  • Following is the example where same function area() is being used to find area of circle,rectangle,triangle types:

3.2 PROGRAM:

1) Following is an example which makes use of Function Overloading:

#include <iostream>using namespace std;
class fn{public:void area(int);void area(int,int);void area(float,int,int);};void fn::area(int a){cout<<"Area of circle:"<<3.14*a*a;}void fn::area(int a,int b){cout<<"Area of rectangle:"<<a*b;}void fn::area(float t,int a,int b){cout<<"Area of triangle:"<<t*a*b;}int main(){int ch;int a,b,r;fn obj;cout<<"\n\t Function Overloading";cout<<"\n1.Area of circle\n2.Area of rectangle\n3.Area of triangle\n4.Exit\n:";cout<<"Enter your choice:";cin>>ch;switch(ch){case 1:cout<<"Enter radius of the circle:";cin>>r;obj.area(r);break;case 2:cout<<"Enter sides of the Rectangle:";cin>>a>>b;obj.area(a,b);break;case 3:cout<<"Enter sides of the triangle:";cin>>a>>b;obj.area(0.5,a,b);break;case 4:cout<<"Invalid case";}return 0;}

3.3 OUTPUT:
4.1 THEORY:
  • Operator overloading in C++:
  • C++ allows you to specify more than one definition for an operator in the same scope, which is called operator overloading respectively.
  • Only exiting operator can be overloaded new can not be created.
  • Overloaded operator must have atleast one operand..
  • Overloaded operartor follows syntax rules of original operator.
Unary operator:-
C++ Unary operator overloaded by means of normal member function take no explicit argument but overloaded by means of friend function take 1 explicit argument.
Overloadable/Non-overloadableOperators:
Following is the list of operators which can be overloaded:
+
-
*
/
%
^
&
|
~
!
,
=
<
>
<=
>=
++
--
<<
>>
==
!=
&&
||
+=
-=
/=
%=
^=
&=
|=
*=
<<=
>>=
[]
()
->
->*
New
new []
Delete
delete []
Following is the list of operators which can not be overloaded:
::
.*
.
?:


Types:
Unary operators overloading in C++
Binary operators overloading in C++.
  • Unary Operator Overloading:
The unary operators operate on the object for which they were called and normally, this operator appears on the left side of the object, as in !obj, -obj, and ++obj but sometime they can be used as postfix as well like obj++ or obj--.
The unary operators operate on a single operand and following are the examples of Unary operators:
  • The increment(++) and the decrement(--) operator.
  • The unary minus (-) operator.
  • The logical not (!) operator.
  • Unary operator:-
  • C++ Unary operator overloaded by means of normal member function take no explicit argument but overloaded by means of friend function take 1 explicit argument.

4.2 PROGRAM:

2) Following is an example which makes use of unary operator overloading:

#include <iostream>using namespace std;class counter{private:int count;public:counter(){count=0;}int get_count(){return count;}void operator ++(){count++;}};int main(){counter c1,c2;cout<<c1.get_count()<<"\n";cout<<c2.get_count()<<"\n";++c1;cout<<c1.get_count()<<"\n";++c2;cout<<c2.get_count()<<"\n";return 0;}


4.3 OUTPUT:


5.1 THEORY:
  • Binary operator:-
When binary operator overloaded with normal member function the left hand operator must be object of relevant class.
Ex: Complex c1,c2,c3;
c3=c1+c2;
i.e. c3=c1.operator +c2;
Here c1=implicit object
c2=explicit object


5.2 PROGRAM:

3) Following is an example which makes use of binary operator overloading:

#include <iostream>using namespace std;class Distance{private:int feet;float inches;public:Distance(){feet=0;inches=0.0;}Distance(int ft,float in){feet=ft;inches=in;}void getdist(){cout<<"Enter feet";cin>>feet;cout<<"Enter inches";cin>>inches;}void showdist(){cout<<feet<<"-"<<inches;}Distance operator + (Distance d2){int f=feet+d2.feet;float i=inches+d2.inches;if(i>12.0){i=i-12.0;f++;}return Distance(f,i);}};int main(){
Distance dist1,dist3;dist1.getdist();Distance dist(11,6.25);dist3=dist1+dist;cout<<"Distance1=";dist1.showdist();cout<<"\n Distance2=";dist.showdist();cout<<"\n distance3=";dist3.showdist();return 0;}



5.3OUTPUT:


6. conclusion:
Thus a C++ program by using functions overloading and operator overloadinghas been executed successfully.
7. PRECAUTIONS:
1. Avoid wrong keywords.

2. Check whether all brackets are closed properly or not.

3. Take desired output.

4. In case of abnormal results, Contact the teacher/instructor, repeat the
experiment, and check the algorithm and program.



8. REMARK:
Function is the concept are used for calling the values from outside the main function & same function call called many times by main().Operator overloading completes the data abstraction facilities. "data abstraction" means programmer-defined types which can be used with the same flexibility as built-in types.
9. Discussion Questions:
1) What is Function Overloading & Properties ofit?
____________________________________________________________________________________________________________________________________________________________
2) What is operator overloading?
____________________________________________________________________________________________________________________________________________________________
3)What are the advantages of operator overloading.
____________________________________________________________________________________________________________________________________________________________

4) What are the rules for overloading operators? ____________________________________________________________________________________________________________________________________________________________

No comments:

Post a Comment

About Me

Hi, I am Prof. Amol Zade, working in the field of teaching and research from last more than 10 years. My area of interests are Artificial Intelligence, Wireless Networking, Algorithms. I have 16 International paper publications along with 4 International Conference; also 3 Books published. published on Object Oriented Programming with open source approach.