이현진 님이 쓰신 글 : : :
: :
쥬신 님이 쓰신 글 : : c++책에 있는것과 제가 만든 것은 값은 같은 겂니다. 빌더로 옮길떄 다른 방법이 없을 까요 꼭 CLASS를 이용하여 만들고 싶구요..다른 방법으로 프로그램을 만들고 싶습니다.
:
#include <string> #include <list>
class Student { private: std::string m_StrName; int m_nNo; int m_nEng; int m_nMath;
public: explicit __fastcall Student(int no, const char* name, int eng, int math); virtual __fastcall ~Student(void);
std::string __fastcall GetName(); int __fastcall GetNo(); int __fastcall GetEng(); int __fastcall GetMath(); }; //---------------------------------------------------------------------------
__fastcall Student::Student(int no, const char* name, int eng, int math) : m_nNo(no), m_nEng(eng), m_nMath(math), m_StrName(name) { } //---------------------------------------------------------------------------
__fastcall Student::~Student(void) { } //---------------------------------------------------------------------------
std::string __fastcall Student::GetName() { return m_StrName; } //---------------------------------------------------------------------------
int __fastcall Student::GetNo() { return m_nNo; } //---------------------------------------------------------------------------
int __fastcall Student::GetEng(){ return m_nEng; } //---------------------------------------------------------------------------
int __fastcall Student::GetMath() { return m_nMath; } //---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender) { try { std::list< Student* > StudentList; // STL list 컨테이너를 사용해서 Student 클래스 포인터를 가지는 목록을 만듭니다. StudentList.push_back(new Student(1, "김성주", 90, 98)); // Student 개체를 생성해서 list 컨테이너에 추가합니다. StudentList.push_back(new Student(2, "이아랑", 80, 100)); StudentList.push_back(new Student(3, "이현진", 20, 10)); // ...... 목록 추가
Memo1->Lines->Clear(); // 메모장을 깨끗하게에~
// list 컨테이너에 담긴 개체들을 접근하기 위해 개체들을 가리키는 반복자(iterator)를 만듭니다. std::list< Student* >::iterator iterator; for( iterator=StudentList.begin(); // iterator에 목록의 첫 원소를 가리키게 합니다. iterator!=StudentList.end(); // iterator가 list 컨테이너의 마지막 원소를 가리키는지 검사합니다. ++iterator) // iterator가 list 컨테이너의 다음 원소를 가리키게 합니다. { Memo1->Lines->Add("-------------------------------------"); // iterator의 원소에 접근하기 위해 (*iterator) 처럼 사용합니다. (*iterator) == (Studeunt *) Memo1->Lines->Add("Number : "+IntToStr((*iterator)->GetNo())); Memo1->Lines->Add("Name : "+AnsiString((*iterator)->GetName().c_str())); Memo1->Lines->Add("English: "+IntToStr((*iterator)->GetEng())); Memo1->Lines->Add("Math : "+IntToStr((*iterator)->GetMath())); delete (*iterator); // 개체 삭제 (*iterator)=NULL; }
StudentList.clear(); // list 컨테이너에서 포인터들을 가리키는 원소들을 모두 삭제합니다. } catch(const Exception& E) { ShowMessage("Exception: "+E.Message); } catch(const std::exception& e) { ShowMessage("Exception: "+AnsiString(e.what())); } } //---------------------------------------------------------------------------
STL은 C++ 표준에 포함된 내용입니다. 다음 사이트에서 관련 자료를 얻으실 수 있을겁니다.
http://oopsla.snu.ac.kr/~sjjung/stl/
저는 "Addison Wesley - C++ Standard Library, The A Tutorial and Reference" 라는 E-Book으로 공부하고 있어요..
|