C++/Function/Function Overloaded

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Compute area of a rectangle using overloaded functions.

 

#include <iostream>
using namespace std;
double rect_area(double length, double width)
{
  return length * width;
}
double rect_area(double length)
{
  return length * length;
}
int main()
{
  cout  << "10 x 5.8 rectangle has area: ";
  cout << rect_area(10.0, 5.8) << "\n";
  cout  << "10 x 10 square has area: ";
  cout << rect_area(10.0) << "\n";
  return 0;
}


Create three functions called prompt( ) that perform this task for data of types int, double, and long

  
#include <iostream>  
using namespace std;
void prompt(char *str, int *i);  
void prompt(char *str, double *d);  
void prompt(char *str, long *l);  
     
int main(void){  
  int i;  
  double d;  
  long l;  
     
  prompt("Enter an integer: ", &i);  
  prompt("Enter a double: ", &d);  
  prompt("Enter a long: ", &l);  
     
  cout << i << " " << d << " " << l;  
     
  return 0;  
}  
     
void prompt(char *str, int *i)  
{  
  cout << str;  
  cin >> *i;  
}  
     
void prompt(char *str, double *d)  
{  
  cout << str;  
  cin >> *d;  
}  
     
void prompt(char *str, long *l)  
{  
  cout << str;  
  cin >> *l;  
}


function overloading between int and string type

  
#include <iostream>
#include <string>
using namespace std;
int triple(int number);
string triple(string text);
int main(){
    cout << "Tripling 5: " << triple(5) << "\n\n";
    cout << "Tripling "gamer": " << triple("gamer");
    return 0;
}
int triple(int number){
    return (number * 3);
}
string triple(string text){
    return (text + text + text);
}


Functions differ in number of parameters

 
#include <iostream>
using namespace std;
int myFunction(int i);       
int myFunction(int i, int j);
int main()
{
  cout << myFunction(10) << " "; // calls myFunction(int i)
  cout << myFunction(4, 5);      // calls myFunction(int i, int j)
  return 0;
}
int myFunction(int i)
{
  return i;
}
int myFunction(int i, int j)
{
  return i*j;
}


Overload abs() three ways

 
#include <iostream>
using namespace std;

int abs(int n);
double abs(double n);
int main()
{
  cout << "Absolute value of -10: " << abs(-10) << endl;
  cout << "Absolute value of -10.01: " << abs(-10.01) << endl;
  return 0;
}
int abs(int n)
{
  cout << "In integer abs()\n";
  return n<0 ? -n : n;
}

double abs(double n)
{
  cout << "In double abs()\n";
  return n<0 ? -n : n;
}


Overload function: int and float

 
#include <iostream>
using namespace std;
int difference(int a, int b)
{
  return a-b;
}
float difference(float a, float b)
{
  return a-b;
}
int main()
{
  int (*p1)(int, int);
  float (*p2)(float, float);
  p1 = difference; // address of difference(int, int)
  p2 = difference; // address of difference(float, float);
  cout << p1(10, 5) << " ";
  cout << p2(10.5, 8.9) << "\n";
  return 0;
}


Overload function: int and long

 
#include <iostream>
using namespace std;
int rotate(int i);
long rotate(long i);
int main()
{
  int a;
  long b;
  a = 0x8000;
  b = 8;
  cout  << rotate(a);
  cout << endl;
  cout << rotate(b);
  
  return 0;
}
int rotate(int i)
{
  int x;
 
  if(i & 0x8000) x = 1;
  else x = 0;
  i = i << 1;
  i += x;
  return i;
}
long rotate(long i)
{
  int x;
 
  if(i & 0x80000000) x = 1;
  else x = 0;
  i = i << 1;
  i += x;
  return i;
}


Overload function to accept integer or char * argument

 
#include <iostream>
using namespace std;
void sleep(int n);
void sleep(char *n);
#define DELAY 100000
int main()
{
  cout << ".";
  sleep(3);
  cout << ".";
  sleep("2");
  cout << ".";
  return 0;
}
// Sleep() with integer argument.
void sleep(int n)
{
  long i;
  for( ; n; n--)
    for(i = 0; i <DELAY; i++) ;
}
// Sleep() with char * argument.
void sleep(char *n)
{
  long i;
  int j;
  j = atoi(n);
  for( ; j; j--)
    for(i = 0; i <DELAY; i++) ;
}


Overload the min() function.

 
#include <iostream>
#include <cctype>
using namespace std;
char min(char a, char b);
int min(int a, int b);
double min(double a, double b);
int main()
{
  cout << "Min is: " << min("x", "a") << endl;
  cout << "Min is: " << min(10, 20) << endl;
  cout << "Min is: " << min(0.2234, 99.2) << endl;
  return 0;
}
// min() for chars
char min(char a, char b)
{
  return tolower(a)<tolower(b) ? a : b;
}
// min() for ints
int min(int a, int b)
{
  return a<b ? a : b;
}
// min() for doubles
double min(double a, double b)
{
  return a<b ? a : b;
}