티스토리 뷰
오버라이딩 : 부모 클래스의 함수 이름, 매개 변수, 리턴 값 모두 일치하는 것
오버로딩 : 부모 클래스의 함수 이름만 일치 하는 것
코드로 예제를 봅시다.
부모 클래스는 school
자식 클래스는 teacher, student 구조로 만들겠습니다.
school.h
#pragma once
class school
{
public:
enum class kind {
TEACHER = 1,
STUDENT = 2,
};
// 교사인지 학생인지 판단 변수
kind who;
// 교사 수와 학생 수를 정의합니다.
int PeopleTeacher = 10;
int PeopleStudent = 50;
// 부모 함수
virtual void printPeople();
};
school.cpp
#include "school.h"
#include <iostream>
void school::printPeople()
{
printf("부모 클래스 printfPeople 함수 호출\r\n");
}
이제 school 부모 클래스를 가지는 자식 클래스를 만들어 봅시다.
자식 클래스를 만들 때 class teacher : public school 선언으로 부모 클래스를 설정할 수 있습니다.
teacher.h
#pragma once
#include "school.h"
class teacher :
public school
{
public:
teacher();
~teacher();
// 오버라이딩 - 교사 수를 출력합니다.
virtual void printPeople();
// 오버로딩
virtual void printPeople(bool callParent);
};
teacher.cpp
#include "teacher.h"
#include <iostream>
teacher::teacher()
{
who = kind::TEACHER;
}
teacher::~teacher()
{
}
void teacher::printPeople()
{
printf("교사 수는 : %d 명 입니다.\r\n", PeopleTeacher);
}
void teacher::printPeople(bool callParent)
{
if (callParent)
{
school::printPeople();
}
else
{
printPeople();
}
}
student.h
#pragma once
#include "school.h"
class student :
public school
{
public:
student();
~student();
// 오버라이딩 - 학생 수를 출력합니다.
virtual void printPeople();
// 오버로딩
virtual void printPeople(bool callParent);
};
student.cpp
#include "student.h"
#include <iostream>
student::student()
{
who = kind::STUDENT;
}
student::~student()
{
}
void student::printPeople()
{
printf("학생 수는 : %d 명 입니다.\r\n", PeopleStudent);
}
void student::printPeople(bool callParent)
{
if (callParent)
{
school::printPeople();
}
else
{
printPeople();
}
}
main.cpp
#include <iostream>
#include <Windows.h>
#include "teacher.h"
#include "student.h"
int main()
{
teacher* mTeacher = new teacher();
student* mStudent = new student();
printf("TEACHER : %d\r\n", mTeacher->who);
printf("STUDENT : %d\r\n", mStudent->who);
// 오버라이딩 함수 호출 각각 인원 수를 출력합니다.
mTeacher->printPeople();
mStudent->printPeople();
// 오버로딩
mTeacher->printPeople(true); // 부모 클래스의 함수를 호출한다.
mStudent->printPeople(false); // 자식 클래스의 함수를 호출한다.
delete mTeacher;
delete mStudent;
mTeacher = NULL;
mStudent = NULL;
return 0;
}
오버라이딩은 부모 클래스의 함수를 자식 클래스에서 재 정의하여 사용하는 방법입니다.
(함수명,매개변수,리턴 타입 일치)
오버로딩이란 함수명은 같으나 매개 변수등 다른 함수를 정의하여 사용하는 방법입니다.
사용처:
오버라이딩은 같은 함수를 자식 클래스마다 다른 동작을 하기 위해서 많이 사용됩니다.
예를 들어 교사한테 "거기 사람 몇 명이나 있습니까?" 라고 물은다면 교사 수를 말하는 형태이고
학생에게 "거기 사람 몇 명이나 있습니까?" 라고 물으면 학급 수를 말하는 것 처럼요.
오버로딩은 함수를 확장할 때 많이 사용합니다.
printPeople() 함수를 만들고 잘 사용하다가 나중에 부모 함수를 호출할 일이 생겼을 때 기존 코드를 전부 찾아 변경하는 것 보다 printPeople(bool callParent) 함수를 확장하여 사용하는 방법입니다.
꼭 부모 함수 호출이 아니더라도 상황에 따라서 교사수 + 학생 수를 출력 이라는 함수를 만들어 확장 시킬 수도 있습니다.
'C,C++' 카테고리의 다른 글
C,C++ 쓰레드 예제 (0) | 2020.09.17 |
---|
- Total
- Today
- Yesterday
- c++
- echo server
- uwp textbox
- UDP Echo Server
- 에코 클라이언트
- 채팅
- SOL
- 토큰
- _beginthreadex 예제
- 프로그램
- tcp echo server
- C
- uwp
- UDP 클라이언트
- 에코서버
- 비주얼 스튜디오
- 윈도으 템플릿
- echo tcp client
- UDP 소켓 프로그램
- 솔라나
- TCP 서버
- uwp button
- C++ 쓰레드
- SPL-TOKEN
- uwp textblock
- Visual Studio
- 큐 리스트
- C#
- UDP Socket
- program
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |