- 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
- 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
- 4、该文档为VIP文档,如果想要下载,成为VIP会员后,下载免费。
- 5、成为VIP后,下载本文档将扣除1次下载权益。下载后,不支持退款、换文档。如有疑问请联系我们。
- 6、成为VIP后,您将拥有八大权益,权益包括:VIP文档下载权益、阅读免打扰、文档格式转换、高级专利检索、专属身份标志、高级客服、多端互通、版权登记。
- 7、VIP文档为合作方或网友上传,每下载1次, 网站将根据用户上传文档的质量评分、类型等,对文档贡献者给予高额补贴、流量扶持。如果你也想贡献VIP文档。上传文档
查看更多
六、类对象数组和静态成员(Six class objects, arrays, and static members)
六、类对象数组和静态成员(Six class objects, arrays, and static members)
Class object array
Class objects, like other C++ data types, can also build arrays, arrays, representations, and structures.
#include iostream.h
Class Date
{
Int, Mo, Da, yr;
Public:
Date (int, m=0, int, d=0, int, y=0) {mo=m; da=d; yr=y;}
Void display () const {cout};
Int, main ()
{
Date dates[2];
Date today (12,312003);
Dates[0]=today;
Dates[0].display ();
Dates[1].display ();
Return 0;
}
1. types of object arrays and default constructors
As mentioned earlier, constructors that do not have arguments or have default values for all parameters are called default constructors. If there are no constructors in the class, the compiler automatically provides a public default constructor that does nothing. If there is at least one constructor in the class, the compiler does not provide the default constructor.
If the default constructor is not included in the class, the array of objects cannot be instantiated. Because the array of instances of a class object array does not allow initialization values to match the list of parameters of a constructor.
In the program above, the main () function declares an array of Date objects of length 2, and a single Date object that contains the initialization values. Then assign the initialized Date object to the first object in the array, and then display the date contained in the two array element. As you can see from the output, the first date is the valid date, and the second one shows 0.
When a class array of objects is declared, the compiler calls the default constructor for each element.
The following program removes the default parameter value of the constructor and adds a default constructor.
#include
Class Date
{
Int, Mo, Da, yr;
Public:
Date ();
Date (int, m, int, D, int, y) {mo=m; da=d; yr=y;}
Void display () const {cout };
Date:: Date ()
{
Cout mo=0; da=0; yr=0;
}
Int, main ()
{
Date dates[2];
Date today (12,312003);
Dates[0]=today;
Dates[0].display ();
Dates[1].dis
您可能关注的文档
- 成全机构-ARTDECO建筑风格探讨(Discussion on architectural style of -ARTDECO).doc
- 成人自考(Adult self-taught).doc
- 怀孕后准妈妈必须准备的东西清单(List of things to be prepared for expectant mothers after pregnancy).doc
- 成功团队要一致 - 人力资源 - 管理e栈 管理e栈 - Powered by Discuz!(Successful teams should be consistent - Human Resources - management, e stack management, e stack - Powered, by, Discuz!).doc
- 成功打造全球团队(Successfully build a global team).doc
- 恋爱幸运曲奇(Love cookies).doc
- 成功的办事处销售经理(Successful Office Sales Manager).doc
- 成品蓝博化粪池方案(Rambo finished the septic tank scheme).doc
- 成吉思汗三道著名遗嘱谁是成吉思汗的继承人(Gen Gi Khan's three famous will who is the heir to Gen Gi Khan).doc
- 成型缺陷以及形成原因(Forming defects and causes of formation).doc
- 六一儿童节端午节超市促销(The Dragon Boat Festival Children's Day supermarket promotions).doc
- 六一儿童节营销(促销)方案汇总(Children's Day marketing plan summary (promotion)).doc
- 六合参数(Liuhe parameter).doc
- 六大因素导致工作障碍(Six major factors contribute to job barriers).doc
- 六安公交(Lu'an bus).doc
- 六字真言寓意(Six character Mantra).doc
- 六岁以前儿童绘画的重要问题和家长误区(The important problems and misunderstandings of children's painting before the age of six).doc
- 六年级上册数学第三单元试卷(The sixth grade mathematics book third unit test).doc
- 六年级《安全教育》教学计划安全教育(Grade six safety education, teaching plan, safety education).doc
- 六年级奥数(Six grade Olympiad).doc
文档评论(0)