반응형
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | #include <iostream> #include <string> #include <iomanip> using namespace std; class Name { string name; public: Name() { this->name = "noName"; } Name(string name) { this->name = name; } void setName(string name) { this->name = name; } string getName() const { return name; } }; class Subject { int subj; public: Subject(int subj = 0) { this->subj = subj; } void setSubj(int subj) { this->subj = subj; } int getSubj() const { return subj; } }; class ScoMag { Name name; Subject kor, eng, math; float avg; public: ScoMag() { } ScoMag(string name, Subject subj) : name(name) { } // setter void setName(string name) { this->name.setName(name); } void setKor(int kor) { this->kor.setSubj(kor); } void setEng(int eng) { this->eng.setSubj(eng); } void setMath(int math) { this->math.setSubj(math); } // getter string getName() const { return name.getName(); } int getKor() const { return kor.getSubj(); } int getEng() const { return eng.getSubj(); } int getMath() const { return math.getSubj(); } // total과 average는 set할 필요가 없으므로 getter만 만듦 int getTotal() const { return kor.getSubj() + eng.getSubj() + math.getSubj(); } float getAvg() const { return this->getTotal() / 3.f; } }; void main() { ScoMag *sm; string name; int score, total; float avg; int cnt; cout << "몇 명 성적 입력? : "; cin >> cnt; cout << cnt << "사람 만큼 성적 입력을 시작합니다." << endl; sm = new ScoMag[cnt]; // 입력 for (int i = 0; i < cnt; i++) { cout << "이름을 입력하세요. : "; cin >> name; sm[i].setName(name); cout << "국어 성적을 입력하세요 : "; cin >> score; sm[i].setKor(score); cout << "영어 성적을 입력하세요 : "; cin >> score; sm[i].setEng(score); cout << "수학 성적을 입력하세요 : "; cin >> score; sm[i].setMath(score); } // 출력 for (int i = 0; i < cnt; i++) { // Fair style cout << "NAME" << setw(9) << "KOR" << setw(9) << "ENG" << setw(9) << "MATH" << setw(9) << "TOTAL" << setw(9) << "AVG" << endl; // CoRock sytle cout << sm[i].getName() << setw(7) << sm[i].getKor() << setw(9) << sm[i].getEng() << setw(9) << sm[i].getMath() << setw(9) << sm[i].getTotal() << setw(13) << sm[i].getAvg() << endl; } delete[]sm; } | cs |
OOP는 확장성이 좋아야한다
국영수를 '사용'해라
반응형