Object of a class as a member of another class

Source: Internet
Author: User

An object of a class can be a data member of another class, referred to as a member of an object, and sometimes called a "compound" between classes. To access an inner class, you must go through the external object in two ways: 1 is the top of a public function in an external class that is used primarily to access internal objects. 2. The public members of the internal object are directly accessed by means of the external object name, the internal object name, and the public member of the internal object.

The following examples illustrate:

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 to operate the inner class
};
Person::P erson (int d,int m,int y, string n,string s):d ofb (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 ' the ' to visit ' class member cout<<per.dofb.year<<endl;//the ' second ' to
	visit the Class member return
	0;
}


Simple to achieve a bit ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.