똑똑안녕하세요/C++ programming

오버로딩 / swap 함수 구현

ArielinAzit 2010. 5. 12. 17:51


Q. 다음 main 함수에서 필요로 하는 swap 함수를 구현하라.

#include <cstdlib>
#include <iostream>

using namespace std;

void swap(int *a, int *b);
void swap(char *a, char *b);
void swap(double *a, double *b);

int main()
{
    int num1 = 20, num2 = 30;
    swap(&num1, &num2);
    cout << num1 << ' ' << num2 << endl;
   
    char ch1 = 'a', ch2 = 'z';
    swap(&ch1, &ch2);
    cout << ch1 << ' ' << ch2 << endl;
   
    double dbl1 = 1.111, dbl2 = 5.555;
    swap(&dbl1, &dbl2);
    cout << dbl1 << ' ' << dbl2 << endl;
   


    system("PAUSE");
    return EXIT_SUCCESS;
}

void swap(int *a, int *b)
{
     int temp = *a;
     *a = *b;
     *b = temp;
}
 
void swap(char *a, char *b)
{
     char temp = *a;
     *a = *b;
     *b = temp;
}  

void swap(double *a, double *b)
{
     double temp = *a;
     *a = *b;
     *b = temp;
}