一个类的对象可以作为另一个类的数据成员,简称为对象作为成员,有时也称类之间的“复合”。要访问内部类,必须穿过外部对象,方法有两种:1是在外部类中顶一个一些公有函数,主要用于访问内部对象。2.通过 外部对象名.内部对象名.内部对象公有成员 的方式来直接访问内部对象的公有成员。
下面举例说明:
class Person{
public:
Date dofb; //Date is a class
string Name;
string Sex;
public:
Person(int d,int m,int y,string n,string s);
int &Year();//the first method to operate the inner class
};
Person::Person(int d,int m,int y, string n,string s):dofb(d,m,y),Name(n),Sex(s){
cout<<"initialize over"<<endl;
}
int & Person::Year(){
return dofb.year;
} int _tmain(int argc, _TCHAR* argv[])
{
Person per(21,3,1999,"li","male");
per.Year()=2000; cout<<per.Year()<<endl; //the first method to visit the class member
cout<<per.dofb.year<<endl;// the second method to visit the class member
return 0;
}
简单的实现了一下~