C++ 11. 캡슐화 실습(학생 성적)

-캡슐화 실습(학생 성적)

 

//Student.h

#pragma once

#include <iostream>

#include <string>

using namespace std;

 

class Student

{

        static int last_num; //학생 번호 정적으로 선언

        int number; //학생 번호

        string name;//학생 이름

        int kor;

        int eng;

        int math;

        static const int Def_Kor; //국어점수 디폴트

        static const int Def_Eng;

        static const int Def_Math;

        int sum;

        double average;

public:

        static int GetStuCount();//정적 메서드

        Student(string name);

        void SetKor(int kor);

        void SetEng(int eng);

        void SetMath(int math);

        void SetName(string name);

        void SetNumber(int number);

        void SetSum();

        void SetAverage();

        int GetKor()const;

        int GetEng()const;

        int GetMath()const;

        string GetName()const;

        int GetNumber()const;

        int GetSum()const;

        double GetAverage()const;

        void View();

};

 

 

//Student.cpp

#include "Student.h"

 

int Student::last_num;

const int Student::Def_Kor = -1;

const int Student::Def_Eng = -1;

const int Student::Def_Math = -1;

 

int Student::GetStuCount()//정적 메소드(학생 알려주는 메소드)

{

        return last_num;

}

 

Student::Student(string name):number(last_num)

{

        last_num++;

        this->name = name;

        SetKor(Def_Kor);

        SetEng(Def_Eng);

        SetMath(Def_Math);

}

 

void Student::SetKor(int kor)

{

        if(kor < 0 || kor > 100)

        {

               kor = Def_Kor;

        }

        this->kor = kor;

}

 

void Student::SetEng(int eng)

{

        if(eng < 0 || eng > 100)

        {

               eng = Def_Eng;

        }

        this->eng = eng;

}

 

void Student::SetMath(int math)

{

        if(math < 0 || math > 100)

        {

               math = Def_Math;

        }

        this->math = math;

}

 

void Student::SetName(string name)

{

        this->name = name;

}

 

void Student::SetNumber(int number)

{

        this->number = number;

}

 

int Student::GetKor()const

{

        return this->kor;

}

 

int Student::GetEng()const

{

        return this->eng;

}

 

int Student::GetMath()const

{

        return this->math;

}

 

string Student::GetName()const

{

        return this->name;

}

 

int Student::GetNumber()const

{

        return this->number + 1;

}

 

void Student::SetSum()

{

        int sum = 0;

        sum += (kor + eng + math);

        this->sum = sum;

}

 

int Student::GetSum()const

{

        return this->sum;

}

 

void Student::SetAverage()

{

        double average = 0.0;

        average = GetSum() / (double) 3.0;

        this->average = average;

}

 

double Student::GetAverage()const

{

        return this->average;

}

 

void Student::View()

{

        cout<<"학생 이름 :"<<GetName()<<endl;

        cout<<"학생 번호 :"<<GetNumber()<<endl;

        cout<<"국어 성적 :"<<GetKor()<<endl;

        cout<<"영어 성적 :"<<GetEng()<<endl;

        cout<<"수학 성적 :"<<GetMath()<<endl;

        cout<<"전체 총점 :"<<GetSum()<<endl;

        cout<<"전체 평균 :"<<GetAverage()<<endl;

        cout<<"+++++++++++++++++++++++++++++++++++++"<<endl;

}

 

 

//Program.cpp

#include "Student.h"

 

int main(void)

{

         Student *arr[3];

   

    arr[0] = new Student("조범희");

    arr[1] = new Student("김주연");

    arr[2] = new Student("박정호");

 

        arr[0]->SetKor(100);

        arr[0]->SetEng(100);

        arr[0]->SetMath(100);

        arr[0]->SetSum();

        arr[0]->SetAverage();

   

        arr[1]->SetKor(100);

        arr[1]->SetEng(100);

        arr[1]->SetMath(100);

        arr[1]->SetSum();

        arr[1]->SetAverage();

 

        arr[2]->SetKor(2);

        arr[2]->SetEng(3);

        arr[2]->SetMath(8);

        arr[2]->SetSum();

        arr[2]->SetAverage();

   

    //학생 목록 출력

        cout<<"현재 학생 :"<<Student::GetStuCount()<<endl;

        cout<<"+++++++++++++++++++++++++++++++++++++"<<endl;

    for(int i = 0; i<3;i++)

    {

        arr[i]->View();

    }

    //학생 목록 출력

    cout<<"현재 학생 :"<<Student::GetStuCount()<<endl;

    for(int i = 0; i<3;i++)

    {

        arr[i]->View();

    }

    //학생 객체 소멸

    for(int i = 0; i<3;i++)

    {

        delete arr[i];

    }

        return 0;

}

 

 

728x90

'C++ > 설명' 카테고리의 다른 글

C++ 13.C++에서의 형변환  (0) 2016.04.19
C++ 12. 캡슐화 실습(학생)  (0) 2016.04.19
C++ 10.캡슐화 실습(복소수)  (0) 2016.04.19
C++ 9.복사 생성자  (0) 2016.04.19
C++ 7.생성자와 소멸자  (0) 2016.04.19

이 글을 공유하기

댓글

Designed by JB FACTORY