본문 바로가기

과제모음

[C++]함수오버로딩 & 디폴트 파라메터

반응형

[[연습문제 1]]
#include <iostream>
using namespace std;

int square(int su);
double square (double f);

int main()
{
 int su;
 cout << "정수 입력 : ";
 cin >> su;

 double f;
 cout << "실수입력 : ";
 cin >> f;

 cout << "정수 제곱값 : " << square(su) << endl;
 cout << "실수 제곱값 : " << square(f) << endl;

 return 0;
}


int square(int su)
{
 return (su*su);
}

double square(double f)
{
 return (f*f);
}

[[연습문제 2]]
#include <iostream>
using namespace std;


//void outDate(int year, int month, int day);
void outDate(int y, int m, int d, char kiho = '/');
void outDate(char *pDate);

int main()
{
 outDate(2004, 3, 5);
 outDate(2007, 8, 5, '#');
 outDate("2006/9/2");

 return 0;
}

/*void outDate(int year, int month, int day)
{
 cout << year << "/" << month << "/" << day << endl;
}*/

void outDate(int y, int m, int d, char kiho)
{
 cout << y << kiho << m << kiho << d << kiho << endl;
}

void outDate(char *pDate)
{
 
 cout << pDate << endl;

}

[[연습문제 3]]
#include <iostream>
using namespace std;

int hap(int sta = 1, int end = 100);

int main()
{
 cout << "1 ~ 100의 합 = " << hap() << endl;
 cout << "50 ~ 100의 합 = " << hap(50) << endl;
 cout << "70 ~ 90의 합 = " << hap(70, 90) << endl;

 return 0;
}


int hap(int sta, int end)
{
 int i, resul=0;
 for(i=sta; i <= end; i++)
 {
  resul += i;
 }

 return resul;
}

[[연습문제 4]]
#include <iostream>
using namespace std;

void prnChar(char ch = '*', int n = 10);

int main()
{
 prnChar();

 char ch;
 int n;
 cout << "출력문자 / 개수 입력 : ";
 cin >> ch >> n;
 prnChar(ch, n);

 return 0;
}

void prnChar(char ch, int n)
{
 int i;
 for(i = 0; i < n; i++)
 {
  cout << ch << " ";
 }
 cout << endl;
}


반응형