Pages

Widgets

message passing using C++

Posted by sudheer | Posted in | Posted on 9:20 PM



Here I am  posting programs which are in our lab list. If you find any mistakes or  you want to suggest any thing  kindly post them in comment section.


NOTE: All these programs work only in turbo compilers



  • Design a program in C++ to illustrate the concept of function overloading.
                            

                         To perform this I am printing biggest number in given two numbers and taking four public member functions as max1, max2, max3, max4 with difference in parameters.
finally in main program I use switch statement for the user choice.
Below is the code......




#include<iostream.h>
#include<conio.h>
class max
{
public:
void max1(int ,int );     //pass by value
void max2(int *,int *);   //pass by address
void max3(int &,int &);   //pass by reference
int & max4(int &,int &);  //return by reference
};
void max::max1(int a ,int b)
{
if(a>b)
cout<<a<<" is greater than "<<b;
else
cout<<b<<" is greater than "<<a;
}
void max::max2(int *a,int *b)
{
if(*a>*b)
cout<<*a<<" is greater than "<<*b;
else
cout<<*b<<" is greater than "<<*a;
}
void max::max3(int &a,int &b)
{
if(a>b)
cout<<a<<" is greater than "<<b;
else
cout<<b<<" is greater than "<<a;
}
int &max :: max4(int &a,int &b)
{
if(a>b)
return a;
else
return b;
}
/*********MAIN FUNCTION**********/
void main()
{
clrscr();
max m;
int a,b,n,ch,i=0;
do
{
cout<<"\nEnter two numbers : ";
cin>>a>>b;
cout<<"\n select 1.pass by value\n\t2.pass by address\n\t3.pass by reference\n\t4.Return by reference\n";
cout<<"Enter your choice : ";
cin>>ch;
i++;
switch(ch)
{
case 1:
m.max1(a,b);
break;
case 2:
m.max2(&a,&b);
break;
case 3:
m.max3(a,b);
break;
case 4:
n=m.max4(a,b);
cout<<n<<" is Big.";
break;
default:
cout<<"Invalid option.";
i=5;
}
}while(i<=3);
getch();
}

Comments (0)

Post a Comment