ν‹°μŠ€ν† λ¦¬ λ·°

🧰 ν•¨μˆ˜ μ˜€λ²„λ‘œλ”© (function overloading) 

 

λ§€κ°œλ³€μˆ˜ (λ§€κ°œλ³€μˆ˜μ˜ μžλ£Œν˜•, 개수, μˆœμ„œ) κ°€ λ‹€λ₯΄λ©΄ 이름이 같은 ν•¨μˆ˜ 2개λ₯Ό μ •μ˜ν•  수 μžˆλ‹€. 이λ₯Ό ν•¨μˆ˜ μ˜€λ²„λ‘œλ”©μ΄λΌ ν•œλ‹€.

int max(int a,int b) //max(int,int)
{
}

double max(double a,double b) //max(double,double) 
{
}
// λ”°λΌμ„œ 두 ν•¨μˆ˜λŠ” ν•¨μˆ˜ μ‹œκ·Έλ‹ˆμ²˜κ°€ λ‹€λ₯΄λ―€λ‘œ 같은 이름이라도 ν•¨κ»˜ μ •μ˜ κ°€λŠ₯

 

🧰 ν•¨μˆ˜ μ‹œκ·Έλ‹ˆμ³ (function signature) 

 

ν”„λ‘œκ·Έλž¨μ΄ 같은 μ΄λ¦„μ˜ ν•¨μˆ˜λ₯Ό ν—ˆμš©ν•  λ•Œ, ν•¨μˆ˜λ“€μ„ κ΅¬λΆ„ν•˜κΈ° μœ„ν•΄ μ‚¬μš©ν•˜λŠ” 기쀀을 ν•¨μˆ˜ μ‹œκ·Έλ‹ˆμ³ (function signature) 라고 ν•œλ‹€. ν•¨μˆ˜ μ‹œκ·Έλ‹ˆμ²˜λŠ” λ§€κ°œλ³€μˆ˜λ“€μ˜ μžλ£Œν˜•κ³Ό 쑰합이닀. 이름이 같은 ν•¨μˆ˜λΌλ„ ν•¨μˆ˜ μ‹œκ·Έλ‹ˆμ³κ°€ λ‹€λ₯΄λ©΄ 호좜 μ‹œμ μ— μ»΄νŒŒμΌλŸ¬κ°€ μžμ‹ μ΄ μ–΄λ–€ ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•΄μ•Ό ν•˜λŠ”μ§€ ꡬ뢄할 수 μžˆλ‹€.

κ·ΈλŸ¬λ‚˜, 리턴값을 μ–΄λ–€ μžλ£Œν˜•μœΌλ‘œ ν™œμš©ν• μ§€ ν˜ΈμΆœμ‹œμ μ— μ•Œ 수 μ—†μœΌλ―€λ‘œ 리턴 μžλ£Œν˜•μ€ ν•¨μˆ˜ μ‹œκ·Έλ‹ˆμ²˜μ— ν¬ν•¨λ˜μ§€ μ•ŠλŠ”λ‹€. 

 

int get()
{
}

double get()
{
}

// 첫번째 ν•¨μˆ˜λŠ” get()μ΄λž€ μ‹œκ·Έλ‹ˆμ²˜λ₯Ό κ°–κ³ , λ‘λ²ˆμ§Έ ν•¨μˆ˜λ˜ν•œ get()μ΄λΌλŠ” μ‹œκ·Έλ‹ˆμ²˜λ₯Ό κ°€μ§„λ‹€. 
// λ”°λΌμ„œ 두 ν•¨μˆ˜λ₯Ό ν•¨κ»˜ μ •μ˜ν•˜λ©΄ 컴파일 였λ₯˜κ°€ λ°œμƒν•œλ‹€.

 

🧨 ν•¨μˆ˜ μ˜€λ²„λ‘œλ”© μ˜ˆμ‹œ 1) 

 

#include <iostream>
using namespace std;

int max(int n1, int n2);
int max(int n1, int n2, int n3);
int max(int n1, int n2, int n3, int n4);
int main()
{
	cout << "max(5,7): " << max(5, 7) << endl;
	cout << "max(5,7,8): " << max(5, 7, 8) << endl;
	cout << "max(1,-2,79,4): " << max(1, -2, 79, 4) << endl; 
    //μ„Έκ°œμ˜ signiture이 λ‹€λ₯΄κΈ° λ•Œλ¬Έμ— ꡬ뢄 κ°€λŠ₯
	return 0;
}

int max(int n1, int n2)
{
	int larger;
	if (n1 >= n2)
		larger = n1;
	else
		larger = n2;
	return larger;
}
int max(int n1, int n2, int n3)
{
	int larger;
	larger = max(max(n1, n2), n3); // λ‘κ°œμ˜ max ν•¨μˆ˜λŠ” λ‹€λ₯΄λ‹€
	return larger;
}
int max(int n1, int n2, int n3, int n4)
{
	int larger;
	larger = max(max(n1, n2, n3), n4);
	return larger;
}

RESULT:

λŒ“κΈ€
곡지사항
μ΅œκ·Όμ— 올라온 κΈ€
μ΅œκ·Όμ— 달린 λŒ“κΈ€
Total
Today
Yesterday
링크
TAG
more
Β«   2025/05   Β»
일 μ›” ν™” 수 λͺ© 금 ν† 
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 30 31
κΈ€ 보관함