"The best way to cheer yourself up is to try to cheer somebody else up." Mark Twain
Showing posts with label Function overloading. Show all posts
Showing posts with label Function overloading. Show all posts

Tuesday, March 16, 2010

Write a program to accomplish the following functions specified below.

handle( a-character) returns the reversed case of the passed character or prints it twice depending upon whether you assign the return value to it or not.


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


void handle(char ch)
{
cout<< ch << " " << ch;
}

int handle(int ch)
{
if(ch > = 65 && ch < = 90) // ASCII CODE of A = 65,Z=90
  ch =ch + 32;
else
   { if(ch > = 97 && ch < = 122) // ASCII code of a=97,z=122
           ch = ch - 32;
    }
return ch;
}
     
void main()
{
cout<< "\n Enter a character :";
char c;
cin >> c;

// To print the character twice
handle(c);


cout<<"\n\n" ;

// To reverse the character
int a = handle(c);

cout<< "\n The reverse character of " < < c << "is " << (char)a;

getch();
}

Monday, March 15, 2010

Write definitions for two versions of an overloaded function. This function's first version sum() takes an argument, int array, and returns the sum of all the elements of the passed array. the 2nd version of sum() takes two arguments, an int array and a character ('E' or 'O'). If the passed character is 'E', it returns the sum of even elements of the passed array and if the passed character is 'O', it returns the sum of odd elements. In case of any other character it returns 0 (zero).


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

int sum( int a[]); // sum of all elements of an array
int sum(int a[], char ch); // sum of odd or even elements of an array, depending on choice

void main()
{
int num[10];

cout<< "\n Enter 10 integers : ";

for(int i = 0;  i < 10 ;  i++ )
cin >> num[i];

/* Sum of all 10 integers */
int s = sum(num);
cout<< "\n The Sum of all 10 integers = " << s;

/* Sum of all the even numbers out of all the 10 integers */
s = sum(num, 'E');
cout<< "\n The Sum of all even numbers out of all 10 integers = " << s;


/* Sum of all the odd numbers out of all the 10 integers */
s = sum(num, 'O');
cout<< "\n The Sum of all odd numbers out of all 10 integers = " << s;


/* No result...for wrong choice */
s = sum(num , 'X');
cout<< "\n No Sum for wrong choice . Hence the result  = " << s;


getch();
}

// Function definition of FIRST sum function

 
int sum(int arr[])
{
int s = 0;
for ( int i = 0; i <10 ; i++ )
s = s+ arr[i];

return s;
}


// Function definition of SECOND sum function
int sum(int arr[], char a)
{
int se = 0, so = 0, s = 0;

switch(a)
{
case 'E':
case 'e':

             for ( int i = 0; i <10 ; i++ )
               {
                     if(arr[i]%2 == 0)
                        se = se+ arr[i];
               }
              s = se;
              break;

case 'O':
case 'o':
             for ( int i = 0; i <10 ; i++ )
             {
              if(arr[i]%2 ! = 0)
                 so = so + arr[i];
             }
            s = so;
            break;
default :
           s = 0;
}

return s;
}

Sunday, March 14, 2010

Write a program with overloaded prototypes of volume(), a function that returns volumes of different structures . Write 3 versions : one for cube's volume that takes one float side of the cube, one for cylinder's volume that takes float radius and float height of the cylinder, and one rectangular box's volume that takes float length, float breadth and float height of the box.

A function name having several definitions that are differentiable by the number or types of their arguments(signature), is known as function overloading.

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


// Volume of a cube

double volume(float s)
{
double vol;
vol = (double) ( s * s * s);
return vol ;}


// volume of a cylinder
double volume(float r, float h)
{
double vol = (double)(3.14 * r * r * h);
return vol;
}

// volume of a rectangular box
double volume( float l, float b, float h)


{
double vol = (double) (l * b * h );
return vol;
}

void main()
{
double volume(float s);
double volume(float r, float h);
double volume ( float l, float b, float h);

/* Volume of a cube with side 3.6 unit */
double v = volume(3.6);
cout<< "\n The Volume of a cube = " << v << "Unit Cube";

/* Volume of a cylinder with radius 2.5 unit, height = 6 unit */
v = volume(2.5, 6);
cout<< "\n The Volume of a cylinder = " << v << "Unit Cube";

/* Volume of a rectangular box of l = 10, b =6 , h =4 units , respectively */
v = volume(10, 6, 4);
cout<< "\n The Volume of a rectangular box = " << v << "Unit Cube";

getch();
}

Thursday, March 11, 2010

Write program with overloaded prototypes of themax(), a function that displays maximum values of its arguments. Write three versions : one for single int value, one for two int values, and one for an array of int values.

/* Function Overloading :: All Function names are same but, arguments are of different types and different quantities */ 

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

// Function 1
void themax(int a)
{
cout<< "\n The maximum number is = " << a;
}

// Function 2
int themax(int m, int n)
{
(m>n ? return m : return n);
}

// Function 3
int themax( int arr[ ])
{
int max = arr[0];
for(int i = 1; i < n ; i++)
{
if( max < arr[i] )
   max = arr[i];
}

return max;
}


void main()
{
int a[10], int x , y , max;

/* Function 1 calling ...demonstrated */

cout<< "\n Enter 2 numbers ";
cin >> x >> y;

if(x>y)
   themax(x);
else
   themax(y);

getch(); 

/* Function 2 calling ...demonstrated */

cout<< "\n Enter 2 numbers ";
cin >> x >> y;

max = themax(x,y);
cout<< The Maximum between " << x << " and " << y << " is " << max ;

getch();
clrscr();

/* Function 3 calling ...demonstrated */

for(int i = 0; i < 10 ; i ++)
{
cout<< "\n Enter an integer ";
cin >> a[i];
}

max = themax(a);                                   

cout<< "\n\n The maximum value is " << max ;

getch();
}

Wednesday, March 10, 2010

Write overloaded prototypes of inquote(), a function that displays its argument enclosed in double quotation marks. Write three versions : one for single int value, one for two int values, and one for an array of int values



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

/* function 1 */
void inquote(int a)
{
cout<< "\n The value to be printed is \" " << a << " \" ";
}

/* function 2 */
void inquote(double a)
{
cout<< "\n The value to be printed is \" " << a << " \" ";
}

/* function 3 */
void inquote(char a)
{
cout<< "\n The value to be printed is \" " << a << " \" ";
}

void main()
{
int integer;
double dou;
char ch;

/* function 1 is called */
cout<< "\n Enter an integer :";
cin>> integer;
inquote(integer);

/* function 2 is called */
cout<< "\n Enter a decimal value :";
cin>> dou;
inquote(dou);

/* function 3 is called */
cout<< "\n Enter a character :";
cin>> ch;
inquote(ch);

getch();
}

Raising a number n to a power p is the same as multiplying n by itself p times. Write a overloaded function power() having two versions for it. The first version takes double n and int p and returns a double value. Another version takes takes int n and int p returning int value. Use a default value of 2 for p in case p is omitted in the function call.


#include<iostream.h>
#include<conio.h>
                              
//first function
int power(int n, int p=2)
{
int res  = 1;
for(int i = 1; i < = p ; i++)
res = res * n;
                      
return res;
}


//second function
double power(double n, int p = 2)
{
double res = 1.0;
                   
for(int i = 1; i < = p ; i++)
res = res * n;
                         
return res;
}

void main()
{
int value, powr, result;
/* Calculate the square of an integer */
cout<< "\n Enter a value (any integer ) to find it's square:";
cin>> value;
// first function will be called
result = power(value);
cout<< "The square of " << value << " is " << result;
                                                                                 /* Calculate n to the power m */                                        
cout<< "\n Enter a value (any integer) :";
cin>> value;
cout<< "\n Enter its power (any integer) :";
cin>> powr;
// first function will be called
                                         

result = power(value, powr);
cout<< "\n The " << value << " to the power of " << powr <<" is " << result;

int p;
float val, r;

/* Calculate n to the power of m */
cout<< "\n Enter a value :";
cin>> val;
cout<< "\n Enter its power ( integer):";
cin>> p;
//second function is called
                                       
 r = power(val, p);
cout<< "\n The " << val << " to the power of " << p <<" is " << r;
                        
/* Calculate the square of a decimal number */
cout<< "\n Enter a value :";
cin>> val;
// second function will be called
r = power(val);
cout<< "\n The square of " << val < <" = " << r;
getch();
              
}

Write s overloaded function power() having two versions for it. The first version takes double n and int p and returns a double value. Another version takes takes int n and int p returning int value. Use a default value of 2 for p in case p is omitted in the function call.


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

//first function
int power(int n, int p=2) 
{
int res = pow(n, p);
return res;
}

//second function
double power(double n, int p = 2) 
{
double res;
res = pow(n,p);
return res;
}



void main()
{

int value, powr, result;

cout<< "\n Enter a value (any integer ) to find it's square:";
cin>>  value;
// first function will be called
result = power(value); 
cout<< "\n The square of " << value << " = " << result;

cout<< "\n Enter a value (any integer ) to find it's cube:";
cin>> value;
// first function will be called
result = power(value, 3); 

cout<< "\n The cube of " << value < <" = " << result;

cout<< "\n Enter a value (any integer)  :";
cin>> value;
cout<< "\n Enter its power (any integer) :";
cin>> powr;
// first function will be called
result = power(value, powr); 
cout<< "\n The  " << value < <" to the power of " << powr <<" is " << result;

int p;
float val,  r;
cout<< "\n Enter a value  :";
cin>> val;
cout<< "\n Enter its power ( integer):";
cin>> p;
//second function is called
r = power(val, p); 
cout<< "\n The " << val << " to the power of " << p <<" is " << r;



cout<< "\n Enter a value :";
cin>> val;
// second function will be called
r = power(val); 
cout<< "\n The square of " << val << " = " << r;
getch();
}
C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off.
Now Playing: Ballade Pour Adeline

About Me

My photo
I m an IT lecturer of a college. I love social-work. I want to do something beneficial for society before dying , that can promote our society, to some extent.