C++ Nested Class (C++嵌套类)

In C++, you can place a class declaration inside another class. The class declared within
another is called a nested class, and it helps avoid name clutter by giving the new type class
scope. 
参考书籍: C++ Primer plus(5th edition),P821 ,   Nested Classes这部分讲解非常详细。


Example:
 
  

#include <iostream>

using namespace std;
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 TListOfStudents::Add(const TStudent& d)
{
if( Count < Maximum )
{
Item[Count] = d;
Count++;
}
}

TListOfStudents::TStudent TListOfStudents::Retrieve(const int n)
{
return Item[n];
}

void DisplayList(TListOfStudents &List);
void 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];
}
}
}



The result is:
 
  

Full Name Age Gender
Harry Seamen 12 Male
Hermine Grand 14 Female
Ronald Hannah 18 Male
Johnny Hancock 16 Male


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值