추상클래스
추상 클래스(Abstract Class)
순수가상함수
virtual ReturnType FunctionName(Params) = 0;#include <iostream>
using namespace std;
class Person {
public:
virtual void myAction() = 0; // 순수가상함수
void startAction() {
cout << "Good morning!" << endl;
myAction();
cout << "Sleep!" << endl;
}
};
class Student : public Person {
public:
virtual void myAction() {
cout << "Study ..." << endl;
}
};
class Employee : public Person {
public:
void myAction() {
cout << "Work ..." << endl;
}
};
int main() {
Person* man1 = new Student;
Person* man2 = new Employee;
man1->startAction();
cout << endl;
man2->startAction();
return 0;
}다형성
Last updated