Kong Eunho

함수 중복과 static 멤버

2025년 10월 22일 16시
카테고리 - LECTURE, 객체지향프로그래밍II


객체지향프로그래밍II(김정준) 중간고사 대비 (6장)

함수 중복

int sum(int a, int b) { return a + b; } // 기본 함수

int sum(int a, int b, int c) { return a + b + c; } // 매개변수 개수가 다름

double sum(double a, double b) { return a + b; } // 매개변수 타입이 다름

int main() { // 중복된 sum() 함수 호출, 컴파일러가 구분
  cout << sum(2, 6);
  cout << sum(2, 5, 33);
  cout << sum(12.5, 33.6);
}
// 중복 실패 사례
double sum(int a, int b) { return (double)(a + b); } // 컴파일러가 구분 불가

클래스에서의 함수 중복

class Circle {
  int radius;
public:
  Circle() { radius = 1; }
  Circle(int r) { radius = r; }
};

int main() {
  Circle donut; // Circle() 생성자 호출
  Circle pizza(30); // Circle(int r) 생성자 호출
}

디폴트 매개 변수(default parameter)

void msg(int id, string text="") { ... } // text의 디폴트 값 : ""
...
msg(10); // id에 10, text에 “” 전달, msg(10, “"); 호출과 동일
msg(20, "Good Morning"); // id에 20, text에 “Good Morning” 전달
msg(); // 컴파일 오류. 첫 번째 매개 변수 id에 반드시 값을 전달하여야 함
msg("Hello"); // 컴파일 오류. 첫 번째 매개 변수 id에 값이 전달되지 않았음
void calc(int a, int b=5, int c, int d=0); // 컴파일 오류
void calc(int a=0, int b, int c); // 컴파일 오류

void calc(int a, int b=5, int c=0, int d=0); // 컴파일 성공


함수 중복 간소화

Circle() { radius = 1; } // 생성자 함수 중복
Circle(int r) { radius = r; }
Circle(int r = 1) { radius = r; } // 2개의 생성자 함수를 하나의 함수로 간소화
Circle(int r) { radius = r; } // 컴파일 오류


함수 중복의 모호성

형 변환으로 인한 모호성

float square(float a) {
 return a * a;
}

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

int main() {
 cout << square(3.0); // 3.0은 double타입이므로 모호성 없음
 cout << square(3); // int 타입 3을 어떤 타입으로 변환할지 모호하여 오류 발생
}

참조 매개 변수로 인한 모호성

int add(int a, int b) {
  return a + b;
}

int add(int a, int &b) {
  b = b + a;
  return b;
}

int main(){
  int s = 10, t = 20, sum;
  sum = add(s, t); // 어떤 인자 전달 방식을 사용할지 모호하여 오류 발생
}

디폴트 매개 변수로 인한 모호성

void msg(int id) {
  cout << id << endl;
}

void msg(int id, string s="") {
  cout << id << ":" << s << endl;
}

int main(){
  msg(5, "Good Morning"); // 정상 컴파일. 두 번째 msg() 호출
  msg(6); // 디폴트 매개 변수를 이용하고 있는지 모호하여 오류 발생
}


static 멤버와 non-static 멤버

static 멤버 선언

class Person {
public:
  // non-static 멤버 선언
  int money; // 개인 소유의 돈
  void addMoney(int money) {
    this->money += money;
  }

  static int sharedMoney; // 공금
  static void addShared(int n) {
    sharedMoney += n;
  }
};

// static 멤버 변수 생성 (전체 프로그램에서 한 번만 생성)
int Person::sharedMoney = 10; // 반드시 전역 변수로 생성해야 함


static 멤버 사용

Person lee;
lee.sharedMoney = 500; // 객체.static멤버 방식

Person *p; // 객체 포인터
p = &lee;
p->addShared(200); // 객체포인터->static멤버 방식
Person::sharedMoney = 200; // 클래스명::static멤버변수명
Person::addShared(200); // 클래스명::static멤버함수명
Person::money = 100; // 컴파일 오류. non-static 멤버는 클래스명으로 접근 불가


static 활용


static 관련 접근 범위

class Person {
  int money;
public:
  static int sharedMoney;
  static int getMoney() { return money; } // 컴파일 오류
  // static 멤버 함수는 non-static 멤버에 접근 불가

  int total() { return money + sharedMoney; } // 정상 함수
  // non-static 멤버 함수는 static 멤버에 접근 가능

  static void addMoney(int n) { this->money + = n; } // 컴파일 오류
  // static 멤버 함수에서는 this 포인터를 사용할 수 없음
}
◀ 이전 글 LECTURE, 객체지향프로그래밍II
함수와 참조, 복사 생성자
2025-10-22
목록으로 다음 글 ▶ LECTURE, 객체지향프로그래밍II
프렌드와 연산자 중복
2025-10-22