全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 产后抑郁的药物不良反应监测
- 创新团队建设方案
- 交叉设计在生物等效性试验的假设检验流程
- 血液透析血管通路陈湛华
- 初二年级上册期末模拟物理质量检测试题答案
- 五块石商业中心项目策划书建议案
- 浅析《欲望号街车》中的象征主义
- 科技文献检索实践报告
- 2025年中国农业与食品行业创新科技发展报告
- 中国石化石家庄炼化分公司2025年校园招聘简章【模板】
- 2025贵州毕节市人民政府办公室下属事业单位考调5人模拟试卷及答案详解(历年真题)
- 企业食品安全风险隐患内部报告奖励制度(模版)
- 119消防安全培训演练课件
- 老年科常见急危重症课件
- 蛙泳教学大班课件
- 上海市二手车合同协议
- 货运车辆挂靠合同协议书
- 年产30万吨功能性饮料技术改造项目可行性研究报告模板立项申批备案
- 保密三员培训课件
- 基于单片机智能鞋柜控制系统设计
- 伤寒-本科完整版本
评论
0/150
提交评论