[ create a new paste ] login | about

Link: http://codepad.org/ir4bjDON    [ raw code | output | fork ]

C++, pasted on Jun 15:
#include <iostream>
#include <vector>
using namespace std;

enum Grade {FRESH = 1, SOPHOMORE, JUNIOR, SENIOR};

class CStudent;


class CSchool {
	const string name;
	float budget;
public:
	vector<void*> m_students;
	CSchool(const string & _name,  int size = 0): name(_name)
        {
		budget = 0;
	}
        ~CSchool()
        {
            m_students.clear();
        }
        void fn_push(CStudent & st);

        void print();
	string getName() const 
        {
		return name;
	}
};


class CStudent 
{
	string name;
	Grade grade;
	const CSchool & school;
public:
	CStudent(const CSchool & _school, const string & _name = "")
		: school(_school) 
        {
            this->name = _name; 
            grade = FRESH;
        }
        ~CStudent()
        {
        }

	void print() const 
        {
                cout << "CStudent" << endl;
		string gradeString;
		switch (grade) {
			case 1:
				gradeString = "FRESH";
			break;
			default:
			break;
		}
		cout << name << grade << school.getName() << endl;
	}
	string getName() {
		return name;
	}
};


//Student 클래스가 선언만 되어 있는 경우. 클래스를 구조체로 인식하여.
//웹에서 컴파일 오류가 발생합니다.
//함수 정의 구현부에서 클래스 참조를 못하여. 컴파일이 안됩니다.
//그럴때는. 함수 정의 구현부'를 클래스 헤더의 아래로 옮겨주어 해결합니다.
void CSchool::fn_push(CStudent & st)
{
    m_students.push_back(&st);


}


//t.cpp: In member function 'void School::print()':
//Line 25: error: invalid use of undefined type 'struct Student'
//compilation terminated due to -Wfatal-errors.

//t.cpp: In member function 'void School::print()':
//Line 24: error: request for member 'getName' in 'pSt', which is of non-class type 'Student*'
//compilation terminated due to -Wfatal-errors.

	void CSchool::print() 
        {
                cout << "CSchool" << endl;
		cout << name << budget << endl;

		for (vector<void*>::iterator it = m_students.begin(); it != m_students.end(); ++it) 
                {
                        cout << "[x]" << endl;
                        CStudent * pSt = (CStudent *)*it;
                        cout << "[x]" << endl;
                        string name = (pSt)->getName();
                        cout << "[x]" << endl;
                        cout << name << endl;
		}
	}


int main(void)
{
	CSchool school("Busan", 10);
	CStudent a(school, "sumin");
	CStudent b(school, "sumin2");

        school.fn_push(a);
        school.fn_push(b);

	school.print();
	a.print();
	b.print();
	Grade grade;
	grade = static_cast<Grade>(2);

}


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
CSchool
Busan0
[x]
[x]
[x]
sumin
[x]
[x]
[x]
sumin2
CStudent
sumin1Busan
CStudent
sumin21Busan


Create a new paste based on this one


Comments: