I.  Write a program that uses function overloading to do the following tasks

a.       Find the maximum of two numbers

b.      Find the maximum of three numbers

 

II. write function definitions using function overloading to

a)      increment the value of a variable of type float.

b)      Increment the value of a variable of type char.

 

     III. Write a program in c++ to do the following tasks using function overloading.

a.       compute xy where x and y are both integers

b.      compute xy where x is float and y is an integer.

 

VII) Write a program to add two objects of the class complex_numbers. A complex number has two data members-real part and imaginary part. Complete the following definition and also write a main() function to perform addition of the complex_numbers objects c1 and c2.

 

 

 

I. Write a program that uses function overloading to do the following tasks

a.       Find the maximum of two numbers

b.      Find the maximum of three numbers

 

 

#include<iostream.h>

#include<conio.h>

int max(int a,int b)

{

if (a>b)

 return(a);

else

return(b);

}

 

int max(int a, int b, int c)

{

if(a>b)

  if(a>c)

    return(a);

  else

  return(c);

else if(b>c)

  return(b);

else

return(c);

}

void main()

{

int a,b,c;

clrscr();

a=12;b=4;c=18;

cout<<"\nMaximum of two numbers:"<<a<<" and "<<b<<":"<<max(a,b);

cout<<"\nMaximum of three numbers:"<<a<<","<<b<<" and "<<c<<":"<<max(a,b,c);

getch();

}

Top

 

 

 

II. Write function definitions using function overloading to

a)      increment the value of a variable of type float.

b)      Increment the value of a variable of type char.

 

#include<iostream.h>

#include<conio.h>

 

float incre(float a,int n)

{

 return a+=n;

}

 

char incre(char ch)

{

return ++ch;

}

 

void main()

{

clrscr();

cout<<"\nIncrementing float:"<<incre(1.234,2);

cout<<"\nIncrementing char:"<<incre('R');

getch();

}

 

Explanation:

There are two functions by the name incre. I have given two argument for the float function and one for char function. This is to avoid ambiguity of function calls.

Top

 

 

III. Write a program in c++ to do the following tasks using function overloading.

a.       compute xy where x and y are both integers

b.      compute xy where x is float and y is an integer.

 

#include<iostream.h>

#include<conio.h>

int power(int a, int b)

{

int k=1;

for(int i=0;i<b;i++)

  k*=a;

  return k;

  }

  float power(float a, int b=2)

{

float k=1.0;

for(int i=0;i<b;i++)

  k*=a;

  return(k);

  }

 

  void main()

  {

  clrscr();

  cout<<power(5,2)<<endl;

  cout<<power(5.3);

  getch();

  }

 

Explanation:

I have used default argument in the function call of power(float,int). This is again to avoid ambiguity.

Top

 

 

 

VII) Write a program to add two objects of the class complex_numbers. A complex number has two data members-real part and imaginary part. Complete the following definition and also write a main() function to perform addition of the complex_numbers objects c1 and c2.

 

 

#include<iostream.h>

#include<conio.h>

class complex_numbers

{

float x;

float y;

public:

void assign_data(float,float);

void display_data();

complex_numbers operator+(complex_numbers);

};

void complex_numbers::assign_data(float real,float imaginary)

{

x=real;y=imaginary;

}

void complex_numbers::display_data()

{

cout<<"Sum of Real part of the complex number:"<<x<<endl;

cout<<"Sum of imaginary part of the complex number:"<<y;

}

complex_numbers complex_numbers ::operator +(complex_numbers n1)

{

complex_numbers n2;

n2.x=x+n1.x;

n2.y=y+n1.y;

return n2;

}

void main()

{

clrscr();

complex_numbers ob1,ob2,ob3;

ob1.assign_data(12,2);

ob2.assign_data(16,3);

ob3=ob1+ob2;

ob3.display_data();

getch();

}

 

Explanation:

Three objects are created for class complex_numbers; Real and imaginary part is assigned to the private members of the class by passing arguments to the member function assign_data(). The + operator is overloaded. ob1 is the object which calls the function. ob2 is passed as arguments. The real and imaginary parts are added separately and stored in a temporary object of the class. It is returned and stored in ob3. ob3 calls the display_data() function to display the output.

Top

 

 

Hosted by www.Geocities.ws

1