当前位置:高等教育资讯网  >  中国高校课件下载中心  >  大学文库  >  浏览文档

川北医学院:《C++程序设计》课程教学资源(课件讲稿)第9章 关于类和对象的进一步讨论

资源类别:文库,文档格式:PDF,文档页数:121,文件大小:3.56MB,团购合买
9.1 构造函数 9.2 析构函数 9.3 调用构造函数和析构函数的顺序 9.4 对象数组 9.5 对象指针 9.6 共用数据的保护 9.7 对象的动态建立和释放 9.8 对象的赋值和复制 9.9 静态成员 9.10 友元 9.11 类模板
点击下载完整版文档(PDF)

第9章关于类和对象的进一步讨论 9.1构造函数 9.2析构函数 9.3调用构造函数和析构函数的顺序 9.4对象数组 9.5对象指针 9.6共用数据的保护 9.7对象的动态建立和释放 9.8对象的赋值和复制 9.9静态成员 9.10友元 9.11类模板 2017年4月26日12时 第9章关于类和对象的进一步讨 H04 15分 论 BACK NEXT

HOME2017年4月26日12时 15分 第9章 关于类和对象的进一步讨 论 2 9.1 构造函数 9.2 析构函数 9.3 调用构造函数和析构函数的顺序 9.4 对象数组 9.5 对象指针 9.6 共用数据的保护 9.7 对象的动态建立和释放 9.8 对象的赋值和复制 9.9 静态成员 9.10 友元 9.11 类模板

9.1构造函数 9.1.1对象的初始化 类的数据成员是不能在声明类时初始化的。 如果一个类中所有的成员都是公用的,则可以在定义对象时 对数据成员进行初始化。如 class Time public: /声明为公用成员 hour;minute;sec; }; Time t1={14,56,30}; /将1初始化为14:56:30 如果数据成员是私有的,或者类中有private或protected的成 员,就不能用这种方法初始化。 2017年4月26日12时 第9章关于类和对象的进一步讨 3 论 BACK NEXT

HOME2017年4月26日12时 15分 第9章 关于类和对象的进一步讨 论 3 类的数据成员是不能在声明类时初始化的。 如果一个类中所有的成员都是公用的,则可以在定义对象时 对数据成员进行初始化。如 class Time { public: //声明为公用成员 hour; minute; sec; }; Time t1={14,56,30}; //将t1初始化为14:56:30 如果数据成员是私有的,或者类中有private或protected的成 员,就不能用这种方法初始化

Constructor 1.Definition: A special member function,it is primarily used to allocate memory for object and initialize the object as a particular state. 2.Characteristic: The name of constructor must be same as the class name,or else it will be regarded as a common member function. Recalled by the complier system when the object is ◆ defined. 2017年4月26日12时 第9章关于类和对象的进一步讨 论 BACK NEXT

HOME2017年4月26日12时 15分 第9章 关于类和对象的进一步讨 论 4 1. Definition: A special member function, it is primarily used to allocate memory for object and initialize the object as a particular state. 2. Characteristic: uThe name of constructor must be same as the class name, or else it will be regarded as a common member function. uRecalled by the complier system when the object is defined

Constructor It can have any type of parameters,but can't have returning type.So when defining and declaring a constructor,you can't point out its type,even the “void'type. It may be inline functions,overloading functions, functions without format parameters. In practice,you usually need define a constructor for each class.If you haven't define a constructor,the complier will automatically create a constructor without a parameters. 2017年4月26日12时 H05务 第9章关于类和对象的进一步讨 论 BACK NEXT

HOME2017年4月26日12时 15分 第9章 关于类和对象的进一步讨 论 5 uIt can have any type of parameters, but can’t have returning type. So when defining and declaring a constructor, you can’t point out its type, even the “void” type. uIt may be inline functions, overloading functions, functions without format parameters. uIn practice, you usually need define a constructor for each class. If you haven’t define a constructor, the complier will automatically create a constructor without a parameters

9.1.2构造函数的作用 构造函数(constructor)是一种特殊的成员函数,不需 要也不能被用户来调用它,而是在建立对象时自动 执行。 构造函数的名字必须与类名同名。 它不具有任何类型,不返回任何值。 构造函数的功能是初始化。 如果用户自已没有定义构造函数,则C+系统会自动 生成一个空的构造函数。 2017年4月26日12时 0分 第9章关于类和对象的进一步讨 论 BACK NEX

HOME2017年4月26日12时 15分 第9章 关于类和对象的进一步讨 论 6 构造函数(constructor)是一种特殊的成员函数,不需 要也不能被用户来调用它,而是在建立对象时自动 执行。 构造函数的名字必须与类名同名。 它不具有任何类型,不返回任何值。 构造函数的功能是初始化。 如果用户自己没有定义构造函数,则C++系统会自动 生成一个空的构造函数

Example of constructor #include using namespace std; class Time f public: Time() //the definition of constructor hour-0; minute=0; sec-0; void set_time(); void show time(); private: int hour; /private data members int minute; int sec; 2017年4月26日12时 第9章关于类和对象的进一步讨 论 BACK NEXT

HOME2017年4月26日12时 15分 第9章 关于类和对象的进一步讨 论 7 #include using namespace std; class Time {public: Time( ) //the definition of constructor { hour=0; minute=0; sec=0; }void set_time( ); void show_time( ); private: int hour; // private data members int minute; int sec; };

void Time:set time() /定义成员函数,向数据成员赋值 cin>>hour; cin>>minute; cin>>sec; void Time:show time() /定义成员函数,输出数据成员的值 {cout<<hour<<":"<<minute<<":"<<sec<<endl;} int mainO //The invoke of constructor { Time t1; /隐含调用构造函数 t1.show_time(); /∥显示t1的数据成员的值 t1.set time(); /对t1的数据成员赋值 t1.show_time(); /显示t1的数据成员的值 return 0; 2017年4月26日12时 第9章关于类和对象的进一步讨 8 论 BACK NEXT

HOME2017年4月26日12时 15分 第9章 关于类和对象的进一步讨 论 8 void Time::set_time() //定义成员函数,向数据成员赋值 { cin>>hour; cin>>minute; cin>>sec; } void Time::show_time() //定义成员函数,输出数据成员的值 { cout<<hour<<":"<<minute<<":"<<sec<<endl;} int main() //The invoke of constructor { Time t1; //隐含调用构造函数 t1.show_time( ); //显示t1的数据成员的值 t1.set_time( ); //对t1的数据成员赋值 t1.show_time( ); //显示t1的数据成员的值 return 0; }

9.1.3 Constructor with parameters /求长方柱的体积 #include using namespace std; class Box public: Box(int,int,int); //the declaration of constructor int volume(); private: int height; int width; int length; //define constructor outside of the class Box:Box(int h,int w,int len)//the realization of constructor { height=h; width-w; length=len; 2017年4月26日12时 第9章关于类和对象的进一步讨 9 H0务 论 BACK NEXT

HOME2017年4月26日12时 15分 第9章 关于类和对象的进一步讨 论 9 //求长方柱的体积 #include using namespace std; class Box {public: Box(int, int, int); //the declaration of constructor int volume( ); private: int height; int width; int length; };//define constructor outside of the class Box::Box(int h,int w,int len) //the realization of constructor { height=h; width=w; length=len; }

//Box::Box(int h,int w,int len):height(h),width(w), length(len){/用参数初始化表实现初始化,9.1.4 int Box:volumeO /定义计算体积的函数 { return(height*width*length); int main() Boxb0x1(12,25,30); /隐含调用构造函数,将初始值作为实参 cout<<"The volume of box1 is "<<box1.volume()<<endl; B0xb0x2(15,30,21); /隐含调用构造函数,将初始值作为实参 cout<<"The volume of box2 is "<<box2.volume()<<endl; return 0; 2017年4月26日12时 第9章关于类和对象的进一步讨 10 论 BACK NEXT

HOME2017年4月26日12时 15分 第9章 关于类和对象的进一步讨 论 10 //Box::Box(int h,int w,int len):height(h),width(w), length(len){ }//用参数初始化表实现初始化,9.1.4 int Box::volume() //定义计算体积的函数 { return(height*width*length); } int main( ) { Box box1(12,25,30); //隐含调用构造函数,将初始值作为实参 cout<<"The volume of box1 is "<<box1.volume( )<<endl; Box box2(15,30,21); //隐含调用构造函数,将初始值作为实参 cout<<"The volume of box2 is "<<box2.volume( )<<endl; return 0; }

9.1.5 Overloading of constructors Similar to a common function,the constructors are also can be overloaded. The constructors which have similar function ,but different type and number of parameters,can be assigned the same function name. The overload constructors can be respectively recalled according to their parameters'type and number by the complier. 2017年4月26日12时 H0务 第9章关于类和对象的进一步讨 1 论 BACK NEXT

HOME2017年4月26日12时 15分 第9章 关于类和对象的进一步讨 论 11 • Similar to a common function, the constructors are also can be overloaded. • The constructors which have similar function ,but different type and number of parameters, can be assigned the same function name. • The overload constructors can be respectively recalled according to their parameters’ type and number by the complier

点击下载完整版文档(PDF)VIP每日下载上限内不扣除下载券和下载次数;
按次数下载不扣除下载券;
24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
共121页,可试读30页,点击继续阅读 ↓↓
相关文档

关于我们|帮助中心|下载说明|相关软件|意见反馈|联系我们

Copyright © 2008-现在 cucdc.com 高等教育资讯网 版权所有