《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每日次数-->可用次数-->下载券;
- 电子科技大学:《机器学习 Machine Learning》课程教学资源(课件讲稿)第12讲 超参数优化与自动学习 Hyperparameters Optimization & AutoML.pdf
- 电子科技大学:《机器学习 Machine Learning》课程教学资源(课件讲稿)第18讲 强化学习 Reinforcement Learning.pdf
- 电子科技大学:《机器学习 Machine Learning》课程教学资源(课件讲稿)第17讲 循环神经网络 Recurrent Neural Networks.pdf
- 电子科技大学:《机器学习 Machine Learning》课程教学资源(课件讲稿)第16讲 生成对抗网络 GAN.pdf
- 电子科技大学:《机器学习 Machine Learning》课程教学资源(课件讲稿)第14讲 深度CNN Deep CNN.pdf
- 电子科技大学:《机器学习 Machine Learning》课程教学资源(课件讲稿)第13讲 卷积神经网络 Convolution Neural Nets.pdf
- 电子科技大学:《机器学习 Machine Learning》课程教学资源(课件讲稿)第12讲 特征学习 Feature Learning.pdf
- 电子科技大学:《机器学习 Machine Learning》课程教学资源(课件讲稿)第11讲 特征提取 Feature Extraction.pdf
- 电子科技大学:《机器学习 Machine Learning》课程教学资源(课件讲稿)第10讲 特征提取 Feature Extraction.pdf
- 电子科技大学:《机器学习 Machine Learning》课程教学资源(课件讲稿)第9讲 特征选择 Feature Selection.pdf
- 电子科技大学:《机器学习 Machine Learning》课程教学资源(课件讲稿)第8讲 非监督学习 Unsupervised Learning.pdf
- 电子科技大学:《机器学习 Machine Learning》课程教学资源(课件讲稿)第7讲 其他分类方法 Classifiers for More.pdf
- 电子科技大学:《机器学习 Machine Learning》课程教学资源(课件讲稿)第6讲 近邻法与Logist回归 Nearest Neighbors & Logist Regression.pdf
- 电子科技大学:《机器学习 Machine Learning》课程教学资源(课件讲稿)第5讲 人工神经网络分类器 Classifiers with ANN.pdf
- 电子科技大学:《机器学习 Machine Learning》课程教学资源(课件讲稿)第4讲 支持向量机 Support Vector Machines.pdf
- 电子科技大学:《机器学习 Machine Learning》课程教学资源(课件讲稿)第3讲 线性模型 Linear Models.pdf
- 电子科技大学:《机器学习 Machine Learning》课程教学资源(课件讲稿)第2讲 模型评估与选择 Evaluation and Selection of Models.pdf
- 电子科技大学:《机器学习 Machine Learning》课程教学资源(课件讲稿)第1讲 概论 Introduction(主讲:郝家胜).pdf
- 《机器学习 Machine Learning》课程教学资源(书籍文献)SVM Tutorial.pdf
- 《机器学习 Machine Learning》课程教学资源(书籍文献)A random forest guided tour.pdf
- 杭州电子科技大学:《计算机视觉》课程教学资源(PPT课件讲稿)第五讲 目标分割.pdf
- 杭州电子科技大学:《人工智能导论》课程教学资源(PPT课件讲稿)第六讲 机器学习基础(机器学习与知识发现).pdf
- 杭州电子科技大学:《人工智能导论》课程教学资源(PPT课件讲稿)第三讲 搜索与求解.pdf
- 杭州电子科技大学:《人工智能导论》课程教学资源(PPT课件讲稿)第二讲 人工智能概述.pdf
- 杭州电子科技大学:《人工智能与模式识别》课程教学资源(讲稿)第二讲 基础知识(概念).pdf
- 杭州电子科技大学:《人工智能与模式识别》课程教学资源(讲稿)第一讲 绪论(主讲:周文晖).pdf
- 杭州电子科技大学:《人工智能导论》课程教学资源(PPT课件讲稿)第一讲 绪论(主讲:周文晖).pdf
- Deep Learning-Based CT Radiomics for Feature Representation and Analysis of Aging Characteristics of Asian Bony Orbit.pdf
- 杭州电子科技大学:《计算机视觉》课程教学资源(PPT课件讲稿)第一讲 绪论(主讲:周文晖).pdf
- 杭州电子科技大学:《计算机视觉》课程教学资源(PPT课件讲稿)第三讲 图像预处理.pdf
- 杭州电子科技大学:《计算机视觉》课程教学资源(PPT课件讲稿)第二讲 图像采集.pdf
- 杭州电子科技大学:《计算机视觉》课程教学资源(PPT课件讲稿)第四讲 基元检测.pdf
- 杭州电子科技大学:《计算机视觉》课程教学资源(PPT课件讲稿)第六讲 立体视觉.pdf
- 杭州电子科技大学:《人工智能导论》课程教学资源(PPT课件讲稿)第四讲 遗传算法.pdf
- 杭州电子科技大学:《人工智能导论》课程教学资源(PPT课件讲稿)第五讲 不确定性知识的表示与推理.pdf
- 杭州电子科技大学:《人工智能导论》课程教学资源(PPT课件讲稿)第七讲 决策树学习.pdf
- 杭州电子科技大学:《人工智能导论》课程教学资源(PPT课件讲稿)第八讲 神经网络学习.pdf
- 杭州电子科技大学:《人工智能导论》课程教学资源(PPT课件讲稿)第九讲 深度学习基础.pdf
- 安徽理工大学:《网络与信息安全 Network and Information Security》课程教学资源(PPT课件讲稿)Part 1 Introduction to Network & Information Security Section 1-1 Current Security Situation.pptx
- 安徽理工大学:《网络与信息安全 Network and Information Security》课程教学资源(PPT课件讲稿)Part 1 Introduction to Network & Information Security Section 1-2 Preliminary Knowledge.pptx