본문 바로가기

프로그래밍/C++

C++ 문법 - 오버로딩

< 오버로딩 (overloading) >


파라매터의 형식과 개수에 따라 서로 다른 함수 또는 메소드가 됩니다.


1. 함수 오버로딩


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
 
using namespace std;
 
void test()
{
    cout << "파라매터 0개" << endl;
}
 
void test(int a)
{
    cout << "파라매터 1개" << endl;
}
 
void test(int a, int b)
{
    cout << "파라매터 2개" << endl;
}
 
int main()
{
    test();
    test(1);
    test(12);
    return 0;
}
cs



2. 메소드 오버로딩


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
 
using namespace std;
 
class Test
{
public:
    void kk()
    {
        cout << "파라매터 0개" << endl;
    }
    void kk(int a)
    {
        cout << "int 파라매터 1개" << endl;
    }
    void kk(float a)
    {
        cout << "float 파라매터 1개" << endl;
    }
};
int main()
{
    Test test;
    test.kk();
    test.kk(100);
    test.kk(3.14f);
    return 0;
}
cs



3. 생성자 오버로딩


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
 
using namespace std;
 
class A
{
public:
    A()
    {
        cout << "파라매터 0개" << endl;
    }
    A(int a)
    {
        cout << "파라매터 1개" << endl;
    }
    A(int a, int b)
    {
        cout << "파라매터 2개" << endl;
    }
};
 
int main()
{
    A one;
    A two(999);
    A three(100200);
    return 0;
}
 
cs



'프로그래밍 > C++' 카테고리의 다른 글

C++ 문법 - 상수  (0) 2018.01.28
C++ 문법 - 재귀함수  (0) 2018.01.28
C++ 문법 - 상속 접근제어 변경  (0) 2018.01.22
C++ 문법 - 소멸자  (0) 2018.01.22
C++ 문법 - 생성자  (0) 2018.01.22