◈ 이 글은 강의를 위하여 제가 직접 작성한 내용입니다. 따라서 퍼가실 경우, 출처를 명확히 해주시기 바랍니다!!
◈ This ariticle was written by me for teaching. So if you want to copy this article, please clarify the source!!
◈ This ariticle was written by me for teaching. So if you want to copy this article, please clarify the source!!
// 클래스 객체를 배열로 만들경우, 기본 생성자가 없으면 선언할 때 반드시 초기화를 해줘야 한다. #include#include using namespace std; #define MAX 4 class Student { public: int kor; int eng; int math; public: Student():kor(0), eng(0), math(0){} Student(int k, int e, int m) : kor(k), eng(e), math(m) { } int Total() { return kor + eng + math; } double Average() { return Total() / 3.0; } }; //void print(Student (*stu)[MAX], int row, int col) void print(Student grade[][MAX], int row, int col) { cout << "학급" << "\t학생\t"; char subject[][10]={"국어","영어","수학"}; int subject_count = sizeof(subject)/sizeof(subject[0]); for(int i=0; i < subject_count; i++) { cout << subject[i] << "\t"; } cout << "총점" << "\t평균" << endl; for(int i=0; i < row; i++) { cout << i+1; for(int n=0; n < col; n++) { cout << "\t" << n+1 << "\t"; cout << grade[i][n].kor << "\t" << grade[i][n].eng << "\t" << grade[i][n].math << "\t"; cout << grade[i][n].Total() << "\t" << grade[i][n].Average() << endl; } cout << endl; } } void main( ) { // 3학급 4학생의 국영수 점수. Student firstGrade[][MAX] = { {Student(100, 90,80), Student(97, 98, 99), Student(100, 98,78), Student(78, 89,76)}, {Student(90, 100, 87), Student(100, 87, 86), Student(89,76, 98), Student(98,78,98)}, {Student(98, 99, 87), Student(90,80,95), Student(92, 90,85), Student(99, 94,91)} }; int row = sizeof(firstGrade) / sizeof(firstGrade[0]); // 학급수. int col = sizeof(firstGrade[0]) / sizeof(firstGrade[0][0]); // 학생수. srand(unsigned(time(0))); for(int i=0; i < row; i++) { for(int m=0; m < col; m++) firstGrade[i][m] = Student(rand()%51+50, rand()%51+50, rand()%51+50); } print(firstGrade, row, col); }