본문 바로가기
Language/C++ & openGL

C++ 상속inheritance, (순수)가상함수(pure)virtual function, 추상 클래스(abstract class), overriding, 바인딩

by javapp 자바앱 2019. 1. 18.
728x90

가상 함수 정의


virtual 기본 클래스 멤버 함수의 선언;

virtual int computeSalary() { return 1500000; }



가상 함수 구현 Code 예시


#include <iostream>

#include <string>

using namespace std;


//추상 클래스

class Employee

{

private:

string name;

int id;


public:

Employee();

Employee(string name, int id);

~Employee();

void setName(string name);

string getName();

void setId(int id);

int getId();


virtual int computeSalary() { return 1500000; }                 //가상 함수, 보다 직관적으로 함수를 호출

};


Employee::Employee()

{

name = " ";

id = 0;

}


Employee::Employee(string name, int id)

{

this->name = name;

this->id = id;

}


Employee::~Employee(){}


void Employee::setName(string name){this->name = name;}


string Employee::getName(){return name;}


void Employee::setId(int id){this->id = id;}


int Employee::getId(){return id;}



class SalariedEmployee : public Employee                        //Employee를 상속 하는 클래스 1

{

private:

int salary;


public:

SalariedEmployee(string name, int id, int salary);

~SalariedEmployee();

void setSalary(int salary);

int getSalary();

int computeSalary();

};


SalariedEmployee::SalariedEmployee(string name, int id, int salary) : Employee(name, id)

{

this->salary = salary;

}


SalariedEmployee::~SalariedEmployee(){}


void SalariedEmployee::setSalary(int salary){this->salary = salary;}


int SalariedEmployee::getSalary(){return salary;}


int SalariedEmployee::computeSalary(){return salary;}



class HourlyEmployee : public Employee                                //Employee를 상속 하는 클래스 2

{

private:

int pay;

int time;

public:

HourlyEmployee(string name, int id, int pay, int time);

~HourlyEmployee();

void setPay(int pay);

int getPay();

void setTime(int time);

int getTime();

int computeSalary();

};


HourlyEmployee::HourlyEmployee(string name, int id, int pay, int time) : Employee(name, id)

{

this->pay = pay;

this->time = time;

}


HourlyEmployee::~HourlyEmployee(){}


void HourlyEmployee::setPay(int pay){this->pay = pay;}


int HourlyEmployee::getPay(){return pay;}


void HourlyEmployee::setTime(int time){this->time = time;}


int HourlyEmployee::getTime(){return time;}


int HourlyEmployee::computeSalary(){return time * pay;}


void main()

{

SalariedEmployee e("김개똥", 2004123, 2000000);

HourlyEmployee h("이성계", 2013234, 5000, 2);


cout << e.getName() << "의 월급은 " << e.computeSalary() << endl;

cout << h.getName() << "의 월급은 " << h.computeSalary() << endl;

}




순수 가상 함수 / 추상 클래스


선언

virtual 멤버 함수의 선언 = 0;


0의 대입을 의미하는 게 아니고, '명시적으로 몸체를 정의하지 않았음'을 컴파일러에게 알리는 것이다.

클래스에 순수 가상 함수가 선언되어 있으면 추상 클래스(abstract class) 가 된다.

추상 클래스로부터 파생된 클래스는 객체를 생성할 수  있다.

단, 추상 클래스로부터 상속받은 순수 가상 함수에 몸체를 만들어 주어야만 한다.




//추상 클래스

class Employee

{

private:

string name;

int id;


public:

Employee();

Employee(string name, int id);

~Employee();

void setName(string name);

string getName();

void setId(int id);

int getId();


virtual int computeSalary() { return 1500000; }                 //가상 함수, 보다 직관적으로 함수를 호출

};


이 클래스는 기본 클래스로서만 의미를 가진다. (객체 생성목적 x)

-> 가상 함수를 '순수 가상 함수'로 선언하여 객체의 생성을 문법적으로 막는 것이 좋다.



//추상 클래스

class Employee

{

...

virtual int computeSalary() = 0;   //순수 가상 함수, 보다 직관적으로 함수를 호출

};


기본 클래스의 객체 생성을 문법적으로 막을 수 있다.


동적 바인딩


C++에서는 java 와는 달리 동적 바인딩이 되지 않는다.


JAVA 동적 바인딩


가상함수는 객체지향의 개념이다.

함수가 가상함수로 선언되면, 포인터의 자료형을 기반으로 호출대상을 결정하지 않고,

포인터 변수가 실제로 가리키는 객체를 참조하여 호출의 대상을 결정한다.


상위 클래스  하위 클래스

First *ptr2 = new Second();

ptr2->SimpleFunc();       //오버라이딩

delete ptr2;



참고 : 

C++ 컴파일러는 포인터를 이용한 연산의 가능성 여부를 판단할 때,

포인터의 자료형을 기준으로 판단, 실제 가리키는 객체의 자료형을 기준으로 판단하지 않는다.


-윤성우 열혈 C++프로그래밍-

댓글