欢迎来到天天文库
浏览记录
ID:58702244
大小:661.00 KB
页数:90页
时间:2020-10-04
《第3章+类和对象(二)ppt课件.ppt》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、在定义类时不能对成员变量进行初始化,因为无法确定成员变量属于哪一个对象。成员变量一般都定义为私有属性,也不能在声明对象后利用赋值运算对成员变量进行初始化。成员变量的初始化一般是利用一个名为构造函数的成员函数来完成。3.1构造函数如何进行成员变量的初始化??构造函数是一种特殊的成员函数,它是在创建对象时(声明或new动态创建)系统自动调用的成员函数。构造函数的名字必须与类名同名它不具有任何类型,不返回任何值。什么是构造函数:#includeclassTime{private:inthour;intminute;intsecond;public:Time(){hou
2、r=0;minute=0;sec=0;}//构造函数voidset_time();voidshow_time();};例为类Time添加构造函数。定义成员函数voidTime::set_time(){cin>hour;Cin>>minute;Cin>>sec;}voidTime::show_time(){cout<3、);t2.show_time();return0;}如果用户自己没有定义构造函数,则C++系统会自动生成一个构造函数,只是这个构造函数的函数体是空的,也没有参数,不执行初始化操作。构造函数不需用户调用,也不能被用户调用。带参数的构造函数构造函数可以带参数,在调用不同对象的构造函数时,从外面将不同的数据传递给构造函数,以实现不同的初始化。构造函数首部一般格式为:构造函数名(类型1形参1,类型2形参2….);实参在定义对象时给出,定义对象的一般格式为:类名对象名(实参1,实参2,….);例3.2有两个长方体,其长宽高为别为:(1)12,20,25;(2)10,14,20.分别求出它们的体积4、#includeusingnamespacestd;classBox{public:Box(int,int,int);intvolume();private:intheight;intwidth;intlength;};Box::Box(inth,intw,intlen){height=h;width=w;length=len;}intBox::volume(){return(height*width*length);}intmain(){Boxbox1(12,25,30);cout<<"Thevolumeofbox1is"<5、oxbox2(15,30,21);cout<<"Thevolumeofbox2is"<6、不同例3.3定义两个构造函数,一个无参数,一个有参数#includeusingnamespacestd;classBox{public:Box();Box(inth,intw,intlen):height(h),width(w),length(len){}intvolume();private:intheight;intwidth;intlength;};Box::Box(){height=10;width=10;length=10;}intBox::volume(){return(height*width*length);}intmain(){Boxbox1;cout7、<<"Thevolumeofbox1is"<
3、);t2.show_time();return0;}如果用户自己没有定义构造函数,则C++系统会自动生成一个构造函数,只是这个构造函数的函数体是空的,也没有参数,不执行初始化操作。构造函数不需用户调用,也不能被用户调用。带参数的构造函数构造函数可以带参数,在调用不同对象的构造函数时,从外面将不同的数据传递给构造函数,以实现不同的初始化。构造函数首部一般格式为:构造函数名(类型1形参1,类型2形参2….);实参在定义对象时给出,定义对象的一般格式为:类名对象名(实参1,实参2,….);例3.2有两个长方体,其长宽高为别为:(1)12,20,25;(2)10,14,20.分别求出它们的体积
4、#includeusingnamespacestd;classBox{public:Box(int,int,int);intvolume();private:intheight;intwidth;intlength;};Box::Box(inth,intw,intlen){height=h;width=w;length=len;}intBox::volume(){return(height*width*length);}intmain(){Boxbox1(12,25,30);cout<<"Thevolumeofbox1is"<5、oxbox2(15,30,21);cout<<"Thevolumeofbox2is"<6、不同例3.3定义两个构造函数,一个无参数,一个有参数#includeusingnamespacestd;classBox{public:Box();Box(inth,intw,intlen):height(h),width(w),length(len){}intvolume();private:intheight;intwidth;intlength;};Box::Box(){height=10;width=10;length=10;}intBox::volume(){return(height*width*length);}intmain(){Boxbox1;cout7、<<"Thevolumeofbox1is"<
5、oxbox2(15,30,21);cout<<"Thevolumeofbox2is"<6、不同例3.3定义两个构造函数,一个无参数,一个有参数#includeusingnamespacestd;classBox{public:Box();Box(inth,intw,intlen):height(h),width(w),length(len){}intvolume();private:intheight;intwidth;intlength;};Box::Box(){height=10;width=10;length=10;}intBox::volume(){return(height*width*length);}intmain(){Boxbox1;cout7、<<"Thevolumeofbox1is"<
6、不同例3.3定义两个构造函数,一个无参数,一个有参数#includeusingnamespacestd;classBox{public:Box();Box(inth,intw,intlen):height(h),width(w),length(len){}intvolume();private:intheight;intwidth;intlength;};Box::Box(){height=10;width=10;length=10;}intBox::volume(){return(height*width*length);}intmain(){Boxbox1;cout
7、<<"Thevolumeofbox1is"<
此文档下载收益归作者所有