The C++ language allows you to create a class inside of another. This technique is referred to as nesting. At the time of this writing, nesting a class B inside of a class A doesn't give any access privilege of class A' member variables and methods to class B. Therefore, there is no major advantage to do it, only a matter of convenience and probably personal preference and opinion. It is said that the next C++ standard will allow a nested class B to have access to the private member variables and methods of the nesting class A.
(As you can see, this program was created in Borland C++ Builder. If you are using another compiler, remove the #pragma and probably the conio lines)
//--------------------------------------------------------------------------- #include <iostream> #include <conio> using namespace std; #pragma hdrstop //--------------------------------------------------------------------------- #pragma argsused const int Maximum = 100; //--------------------------------------------------------------------------- class TListOfStudents { public: class TStudent { private: string FullName; int Age; char Gender; public: void setFullName(const string Name) { FullName = Name; } string getFullName() const { return FullName; } void setAge(const int a) { Age = a; } int getAge() const { return Age; } void setGender(const char g) { Gender = g; } char getGender() const { return Gender; } TStudent(); TStudent(string FName, int a, char g); inline virtual ~TStudent() {} }; private: TStudent Item[Maximum]; public: TListOfStudents() : Count(0){} inline ~TListOfStudents() {} void Add(const TStudent& d); TStudent Retrieve(const int n); int Count; }; //--------------------------------------------------------------------------- TListOfStudents::TStudent::TStudent() { FullName = ""; Age = 0.00; Gender = 'M'; } //--------------------------------------------------------------------------- TListOfStudents::TStudent::TStudent(string FName, int a, char g) { FullName = FName; Age = a; Gender = g; } //--------------------------------------------------------------------------- void __fastcall TListOfStudents::Add(const TStudent& d) { if( Count < Maximum ) { Item[Count] = d; Count++; } } //--------------------------------------------------------------------------- TListOfStudents::TStudent TListOfStudents::Retrieve(const int n) { return Item[n]; } //--------------------------------------------------------------------------- void __fastcall DisplayList(TListOfStudents &List); void __fastcall CreateList(TListOfStudents &List); //--------------------------------------------------------------------------- int main(int argc, char* argv[]) { TListOfStudents ListOfItems; CreateList(ListOfItems); DisplayList(ListOfItems); return 0; } //--------------------------------------------------------------------------- void CreateList(TListOfStudents &ListOfItems) { TListOfStudents::TStudent Pupil; Pupil.setFullName("Harry Seamen"); Pupil.setAge(12); Pupil.setGender('M'); ListOfItems.Add(Pupil); Pupil.setFullName("Hermine Grand"); Pupil.setAge(14); Pupil.setGender('F'); ListOfItems.Add(Pupil); Pupil.setFullName("Ronald Hannah"); Pupil.setAge(18); Pupil.setGender('M'); ListOfItems.Add(Pupil); Pupil.setFullName("Johnny Hancock"); Pupil.setAge(16); Pupil.setGender('M'); ListOfItems.Add(Pupil); } //--------------------------------------------------------------------------- void DisplayList(TListOfStudents &ListOfItems) { cout << "Full Name\tAge\tGender"; string Gender[] = { "Male", "Female", "Unknown" }; for(int i = 0; i < ListOfItems.Count; i++) { cout << "\n" << ListOfItems.Retrieve(i).getFullName() << "\t" << ListOfItems.Retrieve(i).getAge() << "\t"; switch(ListOfItems.Retrieve(i).getGender()) { case 'm': case 'M': cout << Gender[0]; break; case 'f': case 'F': cout << Gender[1]; break; default: cout << Gender[2]; } } } //--------------------------------------------------------------------------- |
Full Name Age Gender Harry Seamen 12 Male Hermine Grand 14 Female Ronald Hannah 18 Male Johnny Hancock 16 Male
No comments:
Post a Comment