全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
C+ 嵌套类使用(二)C+嵌套类1、 嵌套类的名字只在外围类可见。2、 类的私有成员只有类的成员和友元可以访问,因此外围类不可以访问嵌套类的私有成员。嵌套类可以访问外围类的成员(通过对象、指针或者引用)。3、 一个好的嵌套类设计:嵌套类应该设成私有。嵌套类的成员和方法可以设为 public 。4、 嵌套类可以直接访问外围类的静态成员、类型名( typedef )、枚举值。/ qiantaolei.cpp : Defines the entry point for the console application./#include stdafx.h#includeusing namespace std;class MotherClasspublic:MotherClass(int b)a=b;coutMotherClass constructed endl;int fun();int getA();public:class mothersClasspublic:mothersClass(int b)mothersT =b;coutMotherClass:mothersClass constructed endl;int funT();int getmothersT();/int getMotherClassAA()/ return MotherClass:aa;/int getMotherClassBB()/ MotherClass:bb=1234;/ return MotherClass:bb;/protected:private:int mothersT;protected:public:private:int a;/static int MotherClass:aa =100;int MotherClass:fun()mothersClass mothersClassT(1);return mothersClassT.getmothersT();int MotherClass:getA()/a= mothersClass:getmothersT();/errorreturn a;int MotherClass:mothersClass:getmothersT()return mothersT;int MotherClass:mothersClass:funT()MotherClass MotherClassT(2);return MotherClassT.getA();int _tmain(int argc, _TCHAR* argv)MotherClass myClass(3);MotherClass:mothersClass myClassT(4);MotherClass:mothersClass myClassTT(5);int a= myClass.getA();coutMotherClass:getA()=aendl;coutMotherClass:fun()=myClass.fun()endl;a= myClassT.getmothersT();coutMotherClass:mothersClass:getmothersT()=aendl;a=myClassT.funT();coutMotherClass:mothersClass:funT()=aendl;/coutMotherClass:mothersClass.getMotherClassAA()=MotherClass:mothersClass.getMotherClassAA()endl;cin.get();return 0;下面内容是从网上贴来的。下面内容是从网上贴来的。C+嵌套类嵌套类的访问问题:记得白凤煮的C+中有一句这样的话:C+嵌套类只是语法上的嵌套。然而在实践过程中,却并非如此。Ex:class Apublic:static int a;class A1void output()coutaendl; /instead of A:a;int A:a;可见,类 A1 嵌入A后访问A的静态变量不写外围域没有任何问题,从编译的角度看,此时位于A:的作用域内,在符号表中是可以找到a的(注意,a必须为static的)。这一点,与分开写的两个类明显不同还有一个特殊之处,见如下代码:Ex:class Aprivate:int a;class A1void output(A aObject)coutaObject.aendl; /instead of A:a;这段代码在VC中不能编译通过,但在DEVC+是可以的,也就是不同的编译对于嵌套类是否能访问外围类的私有成员的定义是不一致的。嵌套类的不同作用域同名问题:class Apublic:static int a;class A1static int a;int void output()coutaendl;
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年张家界市武陵源区街道办人员招聘考试备考试题及答案详解
- 2026年渝中区江北区中小学教师招聘考试备考试题及答案详解
- 2025年乌海市海勃湾区中小学教师招聘考试试题及答案详解
- 2026年山东省青岛市街道办人员招聘笔试参考试题及答案详解
- 2026年蚌埠市淮上区法检系统书记员招聘考试模拟试题及答案详解
- 2025年呼伦贝尔市海拉尔区街道办人员招聘考试试题及答案详解
- 2026年大同市矿区中小学教师招聘笔试备考试题及答案详解
- 2026年伊春市五营区街道办人员招聘笔试参考题库及答案详解
- 2026年商州区中小学教师招聘考试参考试题及答案详解
- 锡林浩特市2027届四年级数学第一学期期末达标检测模拟试题含解析
- DB11-T 2543-2026 花坛花境植物景观营造与养护技术规程
- 2025年甘肃人力资源服务股份有限公司面向社会招聘浙能集团甘肃有限公司古浪黄花滩新能源项目制工作人员笔试历年参考题库附带答案详解
- 2026年企业所得税汇算清缴新政与纳税调整
- 2026年双随机一公开监管题库
- 国土规划股工作制度
- 统编版小学六年级语文下册《奋斗的历程》综合性学习项目化导学案
- 水利水电工程单元工程施工质量检验表与验收表(SLT631.7-2025)
- GB/T 18210-2025光伏方阵电流-电压特性的现场测量
- 2025年云南楚雄州金江能源集团有限公司招聘考试笔试试卷附答案
- 呼吸机相关性肺炎的集束化管理
- 四川电网新建电源并网服务指南(2025年)
评论
0/150
提交评论