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

第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
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 川北医学院:《C++程序设计》课程教学资源(课件讲稿)第12章 多态性与虚函数 Polymorphism & Virtual Functions.pdf
- 川北医学院:《C++程序设计》课程教学资源(课件讲稿)第11章 继承与派生.pdf
- 川北医学院:《C++程序设计》课程教学资源(课件讲稿)第10章 运算符重载.pdf
- 川北医学院:《C++程序设计》课程教学资源(课件讲稿)第8章 类和对象.pdf
- 川北医学院:《C++程序设计》课程教学资源(课件讲稿)第6章 指针.pdf
- 川北医学院:《C++程序设计》课程教学资源(课件讲稿)第5章 数组 Arrays.pdf
- 川北医学院:《C++程序设计》课程教学资源(课件讲稿)第4章 函数与预处理.pdf
- 川北医学院:《C++程序设计》课程教学资源(课件讲稿)第3章 程序设计初步.pdf
- 川北医学院:《C++程序设计》课程教学资源(课件讲稿)第2章 数据类型与表达式 Data Types & Expression.pdf
- 川北医学院:《C++程序设计》课程教学资源(课件讲稿)第1章 C++的初步知识(主讲:祝元仲)C++ Programming.pdf
- 同济大学:《Visual Basic程序设计简明教程》配套PPT课件讲稿(第三版)第10章 数据库应用基础.ppt
- 同济大学:《Visual Basic程序设计简明教程》配套PPT课件讲稿(第三版)第7章 用户界面设计、第8章 数据文件、第9章 图形操作.ppt
- 同济大学:《Visual Basic程序设计简明教程》配套PPT课件讲稿(第三版)第4章 VB控制结构、第5章 数组和自定义类型、第6章 过程.ppt
- 同济大学:《Visual Basic程序设计简明教程》配套PPT课件讲稿(第三版)第1章 Visual Basic程序设计概述、第2章 VB可视化编程基础、第3章 VB语言基础.ppt
- 甘肃农业大学:《VB程序设计基础》课程教学资源(PPT讲稿)C语言概述.ppt
- 甘肃农业大学:《VB程序设计基础》课程教学资源(作业习题)VB习题1.pdf
- 甘肃农业大学:《VB程序设计基础》课程教学资源(授课教案)第一章 VB概述.doc
- 甘肃农业大学:《VB程序设计基础》课程教学资源(教学大纲)Programming of Visual Basic.pdf
- Connectivity Analysis in Wireless Networks with Correlated Mobility and Cluster Scalability.pdf
- Asymptotic Analysis on Content Placement and Retrieval in MANETs.pdf
- 川北医学院:《单片机原理》课程教学资源(教学大纲)单片机原理与应用技术 Monolithic principle and application technology.pdf
- 川北医学院:《单片机原理》课程教学资源(考试大纲).pdf
- 川北医学院:《单片机原理》课程教学资源(教案)生物医学工程专业.pdf
- 川北医学院:《单片机原理》课程教学资源(讲稿,共八章)生物医学工程专业.pdf
- 川北医学院:《单片机原理》课程教学资源(讲义)第1章 基础知识.pdf
- 川北医学院:《单片机原理》课程教学资源(讲义)第2章 单片机的结构原理与简单应用.pdf
- 川北医学院:《单片机原理》课程教学资源(讲义)第3章 8051指令系统.pdf
- 川北医学院:《单片机原理》课程教学资源(讲义)第4章 汇编语言程序设计.pdf
- 川北医学院:《单片机原理》课程教学资源(讲义)第5章 中断系统.pdf
- 川北医学院:《单片机原理》课程教学资源(讲义)第6章 定时器/计数器.pdf
- 川北医学院:《单片机原理》课程教学资源(讲义)第7章 并行扩展技术.pdf
- 川北医学院:《单片机原理》课程教学资源(讲义)第8章 串行通信.pdf
- 川北医学院:《单片机原理》课程教学资源(讲义)第9章 串行扩展技术.pdf
- 烟台理工学院:《Python程序设计》课程教学资源(教学大纲)理论课教学大纲(自动化和机器人工程专业大一本科、人工智能专业大二本科).docx
- 烟台理工学院:《Python程序设计》课程教学资源(教学大纲)Course Design of Python(人工智能专业本科大二).doc
- 烟台理工学院:《人工智能编程技术》课程教学资源(教学大纲)Course Design of artificial intelligence program technology.doc
- 烟台理工学院:《人工智能原理》课程教学资源(教学大纲)Principles of Artificial Intelligence(人工智能专业本科大二).doc
- 烟台理工学院:《深度学习课程设计》教学大纲.doc
- 烟台理工学院:《神经网络与深度学习》教学大纲.doc
- 烟台理工学院:《计算机控制系统课程设计》课程教学大纲 Course Design of Computer Control.doc