- 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
- 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
2016.5.27第5章上机内容概要
2016.5.27 上机内容 下面2道思考题不要求同学现在能做。 可以带着问题去预习第6章。 下面为阅读复习内容。 f1.cpp(16) : error C2437: a : already initialized #includeiostream using namespace std; class A { private: int x,y; const int t ; public: A():t(30){ } A(int a,int b,int c):t(c) { x=a; y=b;} void disp(){ coutx y tendl;} }; void main() { A dd(3,4,5) ; dd.disp(); } #includeiostream using namespace std; class A { private: int x,y; const int t ; public: void disp(){ coutx y tendl;} }; void main() { A dd ; //error dd.disp(); } f1.cpp(17) : error C2512: A : no appropriate default constructor available 说明:带有常成员函数的类 默认的无参构造函数不符合要求。要自己写出无参构造函数 #includeiostream using namespace std; class A{ private: int x; public: int getValue() const; int getValue(); A(int a=0 ) { x=a ; } }; int A::getValue() const { cout11 ; return x; } int A::getValue(){ cout22 ; return x; } void main() { A const dd(5); A tt(10); coutdd.getValue()endl; couttt.getValue()endl; } 普通对象 可以调用常成员函数 #includeiostream using namespace std; class A{ private: int x; public: int getValue() const; A(int a=0 ) { x=a ; } }; int A::getValue() const { cout11 ; return x; } void main() { A const dd(5); A tt(10); coutdd.getValue()endl; couttt.getValue()endl; } 改写了 书上的程序,使之运行输出。 * 5.6 多文件结构和编译预处理命令 前面学过的c++源程序实例,基本上都是由3个部分构成:类的定义,类的成员函数的实现和主函数。 在规模较大的项目中,往往需要多个源程序文件,每个源程序文件称做一个编译单元。这时,c++语法要求一个类的定义必须出现在所有使用该类的编译单元中。 * 5.6 多文件结构和编译预处理命令 常用做法: 将类的定义写在头文件中,使用该类的编译单元则包含这个头文件。 这样,一个项目至少划分为3个文件:类定义文件(*.h),类实现文件(*.cpp),类的使用文件(*.cpp,主函数文件)。 * * * 5.5.3 常引用(书上内容) 如果在声明引用时用const修饰,被声明的引用就是常引用。 常引用所引用的对象不能被更新。 如果用常引用做形参,便不会意外地发生对实参的改变。 * 5.5.3 常引用 常引用的声明形式如下: const 类型说明符 引用名 如: float dist( const Point p1, const Point p2) 非const的引用只能绑定到普通的对象,而不能绑定到常对象。 * 下面的程序有错吗? #include iostream using namespace std; void dis
文档评论(0)