언어 자료구조 알고리즘/Escort C++

[C++] 캡슐화 실습 소스 코드

언제나휴일 2016. 4. 15. 13:16
반응형

 캡슐화 실습 소스 코드 


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();

}

 

3장 캡슐화 실습 Part1 

3장 캡슐화 실습 Part2 

(모든 동영상 강의는 무료입니다.)

반응형