| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
- Flutter 앱 배포
- Flutter 강좌
- node.js
- Hello World
- Networking
- MainAxisAlignment
- flutter
- InkWell
- Row
- AppBar
- Scaffold
- navigator
- FutureBuilder
- Image.network
- WillPopScope
- Flutter Tutorial
- Flutter 예제
- Cached Image
- ListTile
- Snackbar
- ListView.builder
- listview
- HTTP
- CrossAxisAlignment
- Row Widget
- 반석천
- sqlite
- Load Image
- Column Widget
- Flutter Example
- Today
- Total
꿈꾸는 시스템 디자이너
C++ 클래스 상속 예제 본문
간단한 C++ 클래스 상속 예제를 설명한다.
/*
* Parent.h
*
* Created on: 2014. 10. 1.
* Author: here4you
*/
#ifndef PARENT_H_
#define PARENT_H_
#include <iostream>
using namespace std;
class Parent {
// Child 클랙스가 사용할 변수들이므로 private이 아닌 protected로 선언
protected:
string name;
string value;
public:
// 아규먼트가 없는 기본 생성자
Parent();
// name값을 아규먼트로 가지는 생성자
Parent(string name);
// name값과 value값을 아규먼트로 가지는 생성자
Parent(string name, string value);
// 소멸자
virtual ~Parent();
// name값을 설정
void setName(string name);
// value값을 설정
void setValue(string value);
// name값을 반환
string getName();
// value값을 반환
string getValue();
};
#endif /* PARENT_H_ */
위 헤더파일에서는 string타입의 name과 value 멤버를 protected로 설정하였는데 이는 이 클래스를 상속하는 클래스에서만 이들 멤버의 접근이 가능하도록 하기 위함이다.
/*
* Parent.cpp
*
* Created on: 2014. 10. 1.
* Author: here4you
*/
#include "Parent.h"
Parent::Parent() {
cout << "Parent() is called" << endl;
}
// 아규먼트 name을 멤버 name에 저장
Parent::Parent(string name) {
cout << "Parent(string name) is called" << endl;
this->name = name;
}
// 아규먼트 name과 value를 멤버 name과 value에 저장
Parent::Parent(string name, string value) {
cout << "Parent(string name, string value) is called" << endl;
this->name = name;
this->value = value;
}
Parent::~Parent() {
cout << this->name << "'s ~Parent() is called" << endl;
}
// 이름 설정
void Parent::setName(string name) {
this->name = name;
}
// 값 설정
void Parent::setValue(string value) {
this->value = value;
}
// 이름 반환
string Parent::getName() {
return this->name;
}
// 값 반환
string Parent::getValue() {
return this->value;
}
실제 Parent클래스의 구현은 위와 같다.
/*
* Child.h
*
* Created on: 2014. 10. 1.
* Author: here4you
*/
#ifndef CHILD_H_
#define CHILD_H_
#include "Parent.h"
#include <iostream>
using namespace std;
class Child: public Parent {
public:
// 아규먼트가 없는 기본 생성자
Child();
// name값을 아규먼트로 가지는 생성자
Child(string name);
// name값과 value값을 아규먼트로 가지는 생성자
Child(string name,string value);
// 소멸자
virtual ~Child();
// name값을 출력
void printName();
// name값을 반환
void printValue();
};
#endif /* CHILD_H_ */
Parent 클래스를 상속하는 Child 클래스는 위와 같이 정의한다.
10번 라인처럼 Parant클래스를 상속함을 기술한다.
Child.cpp의 구현은 위와 같다./* /* * Child.cpp * * Created on: 2014. 10. 1. * Author: here4you */ #include "Child.h" Child::Child() { cout << "Child() is called" << endl; } //Child::Child(string name){ Child::Child(string name):Parent(name){ cout << "Child(string name) is called" << endl; } //Child::Child(string name,string value){ Child::Child(string name,string value):Parent(name,value){ cout << "Child(string name,string value) is called" << endl; } Child::~Child() { cout << this->name << "'s ~Child() is called" << endl; } // 이름 출력 void Child::printName() { cout << "name: " << this->name << endl; } // 값 출력 void Child::printValue() { cout << "value: " << this->value << endl; }
주목할 점은, 각 생성자의 구현 부분이다.
두 번째, 세 번째 생성자에서 :을 쓰고 그 뒤에 Parent의 생성자를 기술하고 있다.
만약 주석처럼 부모클래스에 대한 기술 없이 구현하면 Parent 생성자는 실행되지 않는다.
마지막으로 메인함수의 구현은 위와 같다./* * Tester.cpp * * Created on: 2014. 10. 1. * Author: here4you */ #include <iostream> #include "Child.h" using namespace std; int main() { // Child 클래스의 인스턴스 생성 Child child1; // 이름과 값을 설정 child1.setName("Kim"); //Parent에서 구현 child1.setValue("500"); //Parent에서 구현 child1.printName(); //Child에서 구현 child1.printValue(); //Child에서 구현 cout << "---------------" << endl; Child child2("Lee"); child2.setValue("300"); child2.printName(); child2.printValue(); cout << "---------------" << endl; Child child3("Park","1000"); child3.printName(); child3.printValue(); cout << "---------------" << endl; return 0; }
Parent 클래스에서 정의한 setName(),setValue()를 Child 클래스에서 사용할 수 있다.
위 사진은 예제의 실행 모습이다.
일반적으로 파라미터를 가지지 않는 생성자는 부모생성자->자식생성자->자식소멸자->부모소멸자 순으로 호출되는 것을 확인할 수 있다.
위 사진은 Child 클래스에서 생성자를 구현할 때 Parent 클래스의 생성자를 기술하지 않은 경우의 실행예이다.
부모클래스의 기본 Parent()생성자가 호출되어 파라미터 초기화가 이루어지지 않는 것을 확인할 수 있다.
'Development > C++' 카테고리의 다른 글
| Ubuntu에서 Eclipse Boost 설정 (0) | 2016.11.28 |
|---|---|
| C++ 싱글턴 패턴(Singleton Pattern)의 이해 (4) | 2014.09.19 |