캡슐화 실습 소스 코드
Stu.h |
#pragma once #include <string> #include <iostream> using namespace std; #include "StuProperty.h" class Stu { int hp; int iq; string name; const int num; int scnt; int stress; static int last_num; public: Stu(string _name); void Dance(); void Drink(); string GetName()const; int GetNum()const; void ListenLecture(); void Relax(); void Sleep(); void Study(); void View()const; private: static int SetStuNum(); void SetIq(int _iq); void SetHp(int _hp); void SetStress(int _stress); void SetSCnt(int _scnt); int GetIq()const; int GetHp()const; int GetStress()const; int GetSCnt()const; }; |
Stu.cpp |
#include "Stu.h"
int Stu::last_num;
Stu::Stu(string _name):num(SetStuNum()) { name = _name; hp = StuProperty::def_hp; iq = StuProperty::def_iq; stress = StuProperty::def_stress; scnt = StuProperty::def_scnt; } void Stu::Dance() { SetIq(GetIq()+3); SetHp(GetHp()-30); SetStress(GetStress()-20); SetSCnt(0); } void Stu::Drink() { SetIq(GetIq()-4); SetHp(GetHp()-20); SetStress(GetStress() - 10); SetSCnt(0); } string Stu::GetName()const { return name; } int Stu::GetNum()const { return num; }
void Stu::ListenLecture() { SetIq(GetIq()+GetSCnt()); SetHp(GetHp()-10); SetStress(GetStress() + 20 - GetSCnt()*5); SetSCnt(0); } void Stu::Relax() { SetHp(GetHp()+30); SetStress(GetStress()-20); SetSCnt(0); } void Stu::Sleep() { SetHp(GetHp()+50); SetStress(GetStress()-50); SetSCnt(0); } void Stu::Study() { SetIq(GetIq() + 5 - GetSCnt()); SetHp(GetHp() - 5); SetStress(GetStress() + 10); SetSCnt(GetSCnt()+1); } void Stu::View()const { cout<<"번호:"<<num<<" 이름:"<<name<<endl; cout<<"체력:"<<hp<<" 아이큐:"<<iq<<" 스트레스:"<<stress<<endl; } int Stu::SetStuNum() { last_num++; return last_num; }
void Stu::SetIq(int _iq) { iq = _iq; if(iq > StuProperty::max_iq) { iq = StuProperty::max_iq; } if(iq < StuProperty::min_iq) { iq = StuProperty::min_iq; } } void Stu::SetHp(int _hp) { hp = _hp; if(hp > StuProperty::max_hp) { hp = StuProperty::max_hp; } if(hp < StuProperty::min_hp) { hp = StuProperty::min_hp; } } void Stu::SetStress(int _stress) { stress = _stress; if(stress > StuProperty::max_stress) { stress = StuProperty::max_stress; } if(stress < StuProperty::min_stress) { stress = StuProperty::min_stress; } }
void Stu::SetSCnt(int _scnt) { scnt = _scnt; if(scnt > StuProperty::max_scnt) { scnt = StuProperty::max_scnt; } if(scnt < StuProperty::min_scnt) { scnt = StuProperty::min_scnt; } } int Stu::GetIq()const { return iq; } int Stu::GetHp()const { return hp; } int Stu::GetStress()const { return stress; } int Stu::GetSCnt()const { return scnt; } |
StuProperty.h |
#pragma once class StuProperty { public: static const int def_hp; static const int min_hp; static const int max_hp; static const int def_iq; static const int min_iq; static const int max_iq; static const int def_stress; static const int min_stress; static const int max_stress; static const int def_scnt; static const int min_scnt; static const int max_scnt; private: StuProperty(void){ } }; |
StuProperty.cpp |
#include "StuProperty.h"
const int StuProperty::def_hp = 50; const int StuProperty::min_hp = 0; const int StuProperty::max_hp = 100; const int StuProperty::def_iq = 100; const int StuProperty::min_iq = 80; const int StuProperty::max_iq = 200; const int StuProperty::def_stress = 0; const int StuProperty::min_stress = 0; const int StuProperty::max_stress = 100; const int StuProperty::def_scnt = 0; const int StuProperty::min_scnt = 0; const int StuProperty::max_scnt = 5;
|
Test.cpp |
#include "Stu.h" void main() { Stu *s = new Stu("홍길동"); s->View();
s->Study(); s->Study(); s->ListenLecture(); s->View();
s->Study(); s->Drink(); s->Dance(); s->Sleep(); s->View();
s->Study(); s->Relax(); s->View();
cout<<"이름:"<<s->GetName()<<endl; cout<<"번호:"<<s->GetNum()<<endl;
delete s; cin.get(); } |
(모든
동영상 강의는 무료입니다.)
'언어 자료구조 알고리즘 > Escort C++' 카테고리의 다른 글
[C++] 실현관계 (Realization) (0) | 2016.04.15 |
---|---|
[C++] 의존 관계 (Dependency) (0) | 2016.04.15 |
[C++] 연관(Association) 관계와 직접 연관(Directed Association) 관계 (0) | 2016.04.15 |
[C++] 집합(Aggregation)관계와 구성(Composition) 관계 (0) | 2016.04.15 |
[C++} 4. 1 일반화 (Generalization) 관계 (0) | 2016.04.15 |
[C++] 캡슐화 실습 - 멤버 메서드 구현 (0) | 2016.04.15 |
[C++] 캡슐화 실습 - 테스트 모듈 작성하기 (0) | 2016.04.15 |
[C++]정적 멤버로 구성된 클래스 사용하기 (0) | 2016.04.15 |
[C++] 클래스에 캡슐화 할 멤버 약속하기 (0) | 2016.04.15 |
[C++] 캡슐화 실습 - 구현할 실습 대상 (0) | 2016.04.15 |