Cpp-Ch09(2011-0917-1833--Administrator--2012-09-28-20,09,25).ppt

Cpp-Ch09(2011-0917-1833--Administrator--2012-09-28-20,09,25).ppt

Cpp-Ch09(2011-0917-1833--Administrator--2012-09-28-20,09,25)

9.10 Default Memberwise Assignment --- 自定义拷贝构造函数 test: test(const test a) { pArray = new int[n]; //申请资源 for(int i=0;i++;in) { pArray[i]=a.pArray[i] } } Summary 条件编译指令 访问成员函数的三种方式(句柄+操作符) 成员函数的作用域:class scope 访问函数和工具函数 带默认实参的构造函数 构造函数和析构函数被调用的顺序 破坏类的封装的一种做法:返回对私有数据成员的引用 利用一个对象初始化另一个对象(拷贝构造函数) 实验题目: 5,6,7,14 具有缺省参数的函数 形式: void func(int x = 100) { coutx; } void main(void) { func(); } 6.15 Default Arguments --- 函数定义 int boxVolume( int length, int width, int height ); int main() { boxVolume(); return 0; } int boxVolume( int length = 1, int width = 1, int height = 1) { return length * width * height; } error C2660: boxVolume : function does not take 0 parameters 缺省参数的说明必须出现在函数调用之前! 6.15 Default Arguments --- 函数定义 int boxVolume( int length = 1, int width = 1, int height = 1 ); int main() { …….. } int boxVolume( int length = 1, int width = 1, int height = 1) { return length * width * height; } error C2572: boxVolume : redefinition of default parameter 6.15 Default Arguments --- 函数定义 默认实参必须是函数参数列表中右端(尾部)的参数,即如果某个参数设定了默认实参,那么该参数右边的所有参数都必须具有默认实参! // OK int boxVolume( int length = 1, int width = 1, int height = 1 ); int boxVolume( int length, int width = 1, int height = 1 ); int boxVolume( int length, int width, int height = 1 ); // ERROR int boxVolume( int length = 1, int width, int height = 1 ); int boxVolume( int length, int width=1, int height ); // ERROR: missing default parameter for parameter x // test.h,类接口定义 class Test{ public: void aaa(int=1, int=2, int=3); }; // test.cpp,类实现 #include iostream using namespace std; #include test.h void Test::aaa(int a, int b, int c) { cout a= a , b= b , c= c endl; } // main.cpp,测试程序 #include test.h int main() { Test test; test.aaa(2); test.aaa(2,3); return 0; } 6.15 Default Arguments --- 函数调用 函数调

文档评论(0)

1亿VIP精品文档

相关文档