C++ 25. 상속과 다형성 최종실습(학생 프로그램)
- C++/설명
- 2016. 4. 20. 12:39
-상속과 다형성 최종 실습(학생 프로그램)
//Student.h
#pragma once
#include <iostream>
#include <string>
using namespace std;
class Student
{
string name; //학생 이름
const int number; //학생 주민번호
static int last_number; //학생 주민번호 순차
int hp;
int iq;
int stress;
int scnt;
static const int Def_Hp;
static const int Max_Hp;
static const int Min_Hp;
static const int Def_Iq;
static const int Max_Iq;
static const int Min_Iq;
static const int Def_Stress;
static const int Max_Stress;
static const int Min_Stress;
static const int Def_Scnt;
static const int Max_Scnt;
static const int Min_Scnt;
public:
Student(string name);
int GetStuCount();
string GetName();
int GetHP();
int GetIQ();
int GetStress();
int GetScnt();
virtual void Lecture() = 0;
virtual void Study() = 0;
virtual void Sleep() = 0;
virtual void Rest() = 0;
virtual void Drink() = 0;
virtual void Sing() = 0;
virtual void View() = 0;
protected:
void SetName(string name);
void SetHP(int hp);
void SetIQ(int iq);
void SetStress(int stress);
void SetScnt(int scnt);
};
//Student.cpp
#include "Student.h"
const int Student::Def_Hp = 100;
const int Student::Max_Hp = 200;
const int Student::Min_Hp = 0;
const int Student::Def_Iq = 100;
const int Student::Max_Iq = 200;
const int Student::Min_Iq = 0;
const int Student::Def_Stress = 0;
const int Student::Max_Stress = 100;
const int Student::Min_Stress = 0;
const int Student::Def_Scnt = 0;
const int Student::Max_Scnt = 5;
const int Student::Min_Scnt = 0;
int Student::last_number;
Student::Student(string name):number(last_number++)
{
this->name = name;
SetHP(Def_Hp);
SetIQ(Def_Iq);
SetStress(Def_Stress);
SetScnt(Def_Scnt);
}
int Student::GetStuCount()
{
return this->last_number;
}
string Student::GetName()
{
return this->name;
}
void Student::SetName(string name)
{
this->name = name;
}
int Student::GetHP()
{
return this->hp;
}
int Student::GetIQ()
{
return this->iq;
}
int Student::GetStress()
{
return this->stress;
}
int Student::GetScnt()
{
return this->scnt;
}
void Student::SetHP(int hp)
{
if(hp > Max_Hp)
{
hp = Max_Hp;
}
if(hp < Min_Hp)
{
hp = Min_Hp;
}
this->hp = hp;
}
void Student::SetIQ(int iq)
{
if(iq > Max_Iq)
{
iq = Max_Iq;
}
if(iq < Min_Iq)
{
iq = Min_Iq;
}
this->iq = iq;
}
void Student::SetStress(int stress)
{
if(stress > Max_Stress)
{
stress = Max_Stress;
}
if(stress < Min_Stress)
{
stress = Min_Stress;
}
this->stress = stress;
}
void Student::SetScnt(int scnt)
{
if(scnt > Max_Scnt)
{
scnt = Max_Scnt;
}
if(scnt < Min_Scnt)
{
scnt = Min_Scnt;
}
this->scnt = scnt;
}
//void Student::Lecture()
//{
//}
//void Student::Study()
//{
//}
//void Student::Sleep()
//{
//}
//void Student::Rest()
//{
//}
//void Student::Drink()
//{
//}
//void Student::Sing()
//{
//}
void Student::View()
{
cout<<"이름:"<<GetName()<<endl;
cout<<"번호 :"<<number+1<<endl;
cout<<"학생 체력 :"<<GetHP()<<endl;
cout<<"학생 지력 :"<<GetIQ()<<endl;
cout<<"학생 스트레스: "<<GetStress()<<endl;
cout<<"학생 공부한 횟수 :"<<GetScnt()<<endl;
}
//MagicStudent.h
#pragma once
#include "Student.h"
class MagicStudent : public Student
{
int MagicWand; //지팡이
public:
MagicStudent(string name);
virtual void Lecture();
virtual void Study();
virtual void Sleep();
virtual void Rest();
virtual void Drink();
virtual void Sing();
virtual void View();
void Traveling();
};
//MagicStudent.cpp
#include "MagicStudent.h"
MagicStudent::MagicStudent(string name) : Student(name)
{
MagicWand = 0;
}
void MagicStudent::Lecture()
{
SetHP(GetHP() - 2);
SetIQ(GetIQ() + GetScnt());
SetStress(GetStress() + 5);
SetScnt(0);
}
void MagicStudent::Study()
{
SetHP(GetHP() - 3);
SetIQ(GetIQ() + GetScnt());
SetStress(GetStress() + 3);
SetScnt(GetScnt()+1);
}
void MagicStudent::Sleep()
{
SetHP(GetHP() + 10);
SetStress(GetStress() - 5);
SetScnt(0);
}
void MagicStudent::Rest()
{
SetHP(GetHP() + 3);
SetStress(GetStress() - 25);
SetScnt(0);
}
void MagicStudent::Drink()
{
SetHP(GetHP() + 5 + MagicWand);
SetIQ(GetIQ() - (10 - MagicWand));
SetStress(GetStress() - 2);
SetScnt(0);
}
void MagicStudent::Sing()
{
SetHP(GetHP() - (10 - MagicWand));
SetIQ(GetIQ() -5);
SetStress(GetStress() - 5);
SetScnt(0);
}
void MagicStudent::View()
{
cout<<"마법 학생";
Student::View();
cout<<"지팡이 :"<<MagicWand<<endl;
}
void MagicStudent::Traveling()
{
MagicWand++;
}
//DepartmentStudent.h
#pragma once
#include "Student.h"
class DepartmentStudent : public Student
{
int StackBrain; //더미뇌
int dummy_scnt; //더미뇌 5의 배수 체크
public:
DepartmentStudent(string name);
virtual void Lecture();
virtual void Study();
virtual void Sleep();
virtual void Rest();
virtual void Drink();
virtual void Sing();
virtual void View();
void Reading();
};
//DepartmentStudent.cpp
#include "DepartmentStudent.h"
#include "Student.h"
DepartmentStudent::DepartmentStudent(string name) : Student(name)
{
StackBrain = 0;
dummy_scnt = 0;
}
void DepartmentStudent::Lecture()
{
SetHP(GetHP() - 3);
SetIQ(GetIQ() + GetScnt());
SetStress(GetStress() + GetScnt());
SetScnt(0);
}
void DepartmentStudent::Study()
{
SetHP(GetHP() - 5);
SetIQ(GetIQ() + GetScnt() + StackBrain);
SetStress(GetStress() - 2);
SetScnt(GetScnt()+1);
dummy_scnt++;
if(dummy_scnt % 5 == 0)
{
StackBrain++;
}
}
void DepartmentStudent::Sleep()
{
SetHP(GetHP() + 10);
SetStress(GetStress() - 5);
SetScnt(0);
}
void DepartmentStudent::Rest()
{
SetHP(GetHP() + 3);
SetStress(GetStress() - 25);
SetScnt(0);
}
void DepartmentStudent::Drink()
{
SetHP(GetHP() + 5);
SetIQ(GetIQ() - 10);
SetStress(GetStress() + 2);
SetScnt(0);
}
void DepartmentStudent::Sing()
{
SetHP(GetHP() - 1);
SetIQ(5 - GetScnt());
SetStress(5 + GetScnt());
SetScnt(0);
}
void DepartmentStudent::Reading()
{
StackBrain++;
SetStress(GetStress() - 5);
SetScnt(0);
}
void DepartmentStudent::View()
{
cout<<"학사 학생";
Student::View();
cout<<"더미 뇌"<<StackBrain<<endl;
cout<<"연속으로 공부한 횟수 :"<<dummy_scnt<<endl;
}
//AthleteStudent.h
#pragma once
#include "Student.h"
class AthleteStudent : public Student
{
int air;
public:
AthleteStudent(string name);
virtual void Lecture();
virtual void Study();
virtual void Sleep();
virtual void Rest();
virtual void Drink();
virtual void Sing();
virtual void View();
void Dancing();
};
//AthleteStudent.cpp
#include "AthleteStudent.h"
#include "Student.h"
AthleteStudent::AthleteStudent(string name) : Student(name)
{
air = 0;
}
void AthleteStudent::Lecture()
{
SetHP(GetHP() - 1);
SetIQ(GetIQ() + GetScnt()/2);
air -= 5;
SetStress(GetStress() - (air * 3));
SetScnt(0);
}
void AthleteStudent::Study()
{
SetHP(GetHP() - 2);
SetIQ(GetIQ() + GetScnt()/2);
air -= 3;
SetStress(GetStress() - (air * 3));
SetScnt(GetScnt()+1);
}
void AthleteStudent::Sleep()
{
SetHP(GetHP() + 10);
SetStress(GetStress() - 5);
SetScnt(0);
}
void AthleteStudent::Rest()
{
SetHP(GetHP() + 8);
SetStress(GetStress() - 25);
SetScnt(0);
}
void AthleteStudent::Drink()
{
SetHP(GetHP() + 5);
SetIQ(GetIQ() - 3);
SetStress(GetStress() - 2);
SetScnt(0);
}
void AthleteStudent::Sing()
{
SetHP(GetHP() - 5);
SetIQ(GetIQ() + 2);
SetStress(GetStress() - 5);
SetScnt(0);
}
void AthleteStudent::View()
{
cout<<"운동하는 학생";
Student::View();
cout<<"air :"<<air<<endl;
}
void AthleteStudent::Dancing()
{
SetHP(GetHP() - 5);
SetIQ(GetIQ() + 3);
SetScnt(0);
air += 1;
}
//Program.cpp
#include "Student.h"
#include "DepartmentStudent.h"
#include "MagicStudent.h"
#include "AthleteStudent.h"
int main(void)
{
Student *stu[3];
stu[0] = new DepartmentStudent("서울대가 대학이야????? 나도 가고싶다....");
stu[1] = new MagicStudent("잉가리 레비오 싸");
stu[2] = new AthleteStudent("나는 리오넬 호구");
cout<<"현재 학생 수 : "<<stu[2]->GetStuCount()<<"명"<<endl;
cout<<"강의받다"<<endl;
for(int i = 0 ; i < 3; i++)
{
stu[i]->Lecture();
stu[i]->View();
cout<<endl;
}
cout<<"============================================================="<<endl;
cout<<"자습하다"<<endl;
for(int i = 0 ; i < 3; i++)
{
stu[i]->Study();
DepartmentStudent *ds = dynamic_cast<DepartmentStudent*>(stu[i]);
if(ds)
{
ds->Reading();
}
stu[i]->View();
cout<<endl;
}
cout<<"============================================================="<<endl;
cout<<"잠자다"<<endl;
for(int i = 0 ; i < 3; i++)
{
stu[i]->Sleep();
stu[i]->View();
cout<<endl;
}
cout<<"============================================================="<<endl;
cout<<"휴식하다"<<endl;
for(int i = 0 ; i < 3; i++)
{
stu[i]->Rest();
MagicStudent *ms = dynamic_cast<MagicStudent*>(stu[i]);
if(ms)
{
ms->Traveling();
}
stu[i]->View();
cout<<endl;
}
cout<<"============================================================="<<endl;
cout<<"음료를 마시다"<<endl;
for(int i = 0 ; i < 3; i++)
{
stu[i]->Drink();
stu[i]->View();
cout<<endl;
}
cout<<"============================================================="<<endl;
cout<<"노래하다"<<endl;
for(int i = 0 ; i < 3; i++)
{
stu[i]->Sing();
AthleteStudent *as = dynamic_cast<AthleteStudent*>(stu[i]);
if(as)
{
as->Dancing();
}
stu[i]->View();
cout<<endl;
}
for(int i = 0 ; i < 3; i++)
{
delete stu[i];
}
}
'C++ > 설명' 카테고리의 다른 글
콜백(CALLBACK)함수란 무엇인가??? (0) | 2018.08.06 |
---|---|
[C++] strcat_s 함수를 이용하여 문자열 결합하기 (0) | 2018.08.06 |
C++ 24.상속과 다형성 실습2(상품, 할인상품) (0) | 2016.04.20 |
C++ 23. 상속과 다형성 실습(도형) (0) | 2016.04.20 |
C++ 22. 파생 개체의 생성과 소멸 과정 (0) | 2016.04.19 |
이 글을 공유하기