W10 构造函数.ppt

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

top=T; bottom=B;
}
... ... ... };
而是直接用类名 当作函数名
与其它函数不同, 不能为构造函数指定返回值类型和void类型
第10章 构造函数和析构函数
10.1 构造函数
class Crect { int left,top,right,bottom;
public: Crect(int L, int R, int T, int B) { left=L; right=R; top=T; bottom=B; }
{ left=L; right=R; top=T; bottom=B; }
Crect()
{ left=0; right=0; top=0; bottom=0; }
};vCoriedctS(hionutw(L)=0, int R=0, int T=0, int B=0)
{{ lceofutt=<L<;“lerfitg=h“t<=<Rl;eft<t<o“pl=iTg;ht=“b<o<tltiogmh=tB<;<“}\n”;
public:
Crect(int L, int R, int T, int B)
{ left=L; right=R; top=T; bottom=B; }
Crect()
{ left=0; right=0; top=0; bottom=0; }
};vCoriedctS(hionwt()L=0, int R=0, int T=0, int B=0)
class Crect { int left,top,right,bottom;
public:
Crect产(i生nt对L象, 时in,t 系R,统i必nt定T调, 用in构t 造B)函数。 如C{r果ecl类te(f中)t=没L;有定ri义gh构t=造R;函数to时p=,T;编译b器ot自to动m=产B;生一}
right=没0有; 对这应里t的o将p无引=0参起;构错造误b函,o数t因t可为om供r=调0;用}
{ cout<<“left=“<<left<<“light=“<<light<<“\n”;
cout<<“top=“<<top<<“bottom=“<<bottom<<“\n”; }
};
void main(void) {
C c1 ={25,”张三”, {77.8,99.56}}; 列表法仅可对公有数据成员初始化.
此外, C++还提供了专门用作初始化的成员函数————构造函数。 而当某个对象的寿命到期时,它还提供了 一个用于释放对象所占资源的函数————析构函数。
本章将分别介绍这两个函数。
第10章 构造函数和析构函数
第10章 构造函数和析构函数
10.1 构造函数 10.2 析构函数 10.3 拷贝构造函数 10.4 拷贝函数与对象成员
第10章 构造函数和析构函数
上一章的例题中曾提到对象的成员的初始化问题
class Crect { int left,top;
现在再回顾一下
public:
int right,bottom;
Crect r, rr(10,20,30,40);
}
第10章 构造函数和析构函数
10.1 构造函数
class Crect
{ int left,top,right,bottom;
public:
Crect(int L, int R, int T, int B)
{
left=L; right=R; top=T; bottom=B; }
void setcoord(int, int, int, int); ... ...
};
在类Crect中共有4个数据成员,其中
left,top 是私有成员 right, bottom 是公有成员
公有成员可以直接用对象对它们初始化,私有成员的初始化 则必须调用公有成员函数,并在函数体中通过访问私有成员来完成
class Crect
统自动调用的。如果没有定义无参构造函数,……
{ int left,top,right,bottom;
public:
Crect(int L, int R, int T, int B)
{ left=L; right=R; top=T; bottom=B; }
Crect()
{ left=0; };void Show()
cout<<“top=“<<top<<“bottom=“<<bottom<<“\n”; }
};
因此,对象r不知道调用哪一个
void main(void) {
Crect r, rr(10,20,30,40);
这就是说,在一个类中 缺省构造函数只能有一个
}
第10章 构造函数和析构函数
10.1 构造函数
{ left=0; r个ig缺ht省=0的; 构造to函p=数0;: bottom=0; }
};vCoriedctS(hionutw(L)=0, int R=0, int T=0, int B=0) {{ lceofutt=<L<;“lerfitg=h“t<=<Rl;eft<t<o“pl=iTg;ht=“b<o<tltiogmh=tB<;<“}\n”; cout<<“top=“<<top<<“bottom=“<<bottom<<“\n”; }
10.1 构造函数 现在把上例中4个数据成员都改成私有成员
class Crect { int left,top,right,bottom;
public: Crect(int L, int R, int T, int B) { left=L; right=R; top=T; bottom=B; } ... ... ...
Crect(int L, int R, int T, int B)
{
left=L; right=R; top=T; bottom=B; }
定义对象rr, 并调用带参构造函数
Cre.c.t.()... ... }; { left=0; right=0; top=0; bottom=0; }
};
void main(void) {
请注意,这种形式不是调用 无参构造函数
Cre.c.t.()... ... }; { left=0; right=0; top=0; bottom=0; }
};
void main(void) {
Crect r, rr(10,20,30,40);, rrr();
rrr不是对象,这种格式是 说明一个函数原型。
public:
结果使得: left=0 right=0 top=0 bottom=0
Crect(int L, int R, int T, int B)
{
left=L; right=R; top=T; bottom=B; }
定义对象r, 并调用无参构造函数
Cre.c.t.()... ... }; { left=0; right=0; top=0; bottom=0; }
Crect r, rr(10,20,30,40);
}
第10章 构造函数和析构函数
10.1 构造函数
但是,本例中将发生错误!
因为无参或者参数为缺省值的构造函
class Crect
数都被认为是缺省构造函数,它们的
{ int left,top,right,bottom; 性质相同。
public:
Crect(int L, int R, int T, int B)
};
void main(void) {
Crect r, rr(10,20,30,40);
}
第10章 构造函数和析构函数
10.1 构造函数
class Crect { int left,top,right,bottom;
public:
结果使得: left=10 right=20 top=30 bottom=40
};void Show() { cout<<“left=“<<left<<“light=“<<light<<“\n”; cout<<“top=“<<top<<“bottom=“<<bottom<<“\n”; }
};
void main(void)
{
Crect r, rr(10,20,30,40); r.Show();
{
left=L; right=R; top=T; bottom=B; }
结果使得: left=100 right=300 top=50 bottom=200
void main(void) {
Crect r,rr;
r.setcoord(100,300,50,200);
}
第10章 构造函数和析构函数
C++数据成员的初始化方法,除上述方法之外,还可 以采用我们已知的结构体变量的初始化方法称为列表法 (见例题10.1,P218)。
第10章 构造函数和析构函数
例如
class Crect { int left,top;
这是调用成员函数, 可使公有和私有数据成员都被初始化
public:
int right,bottom;
void setcoord(int, int, int, int); ... ...
};
void Crect::setcoord(int L, int R, int T, int B)
};
构造函数是类中的一个特殊的成员函数
第10章 构造函数和析构函数
10.1 构造函数
class Crect 它的特殊性表现为:构造函数没有自己的函数名
{ int left,top,right,bottom; public:
Crect(int L, int R, int T, int B)
{
left=L; right=R;
这个函数的返回值是Crect 类型的对象
}
下面在成员函数中补充一个输出函数
第10章 构造函数和析构函数
10.1 构造函数
left=0 right=0 top=0 bottom=0
class Crect { int left,top,right,bottom;
public: Crect(int L, int R, int T, int B) { left=L; right=R; top=T; bottom=B; } Crect() { left=0; right=0; top=0; bottom=0; }
Crect(int L, int R, int T, int B)
{ left=L; right=R; top=T; bottom=B; }
Crect()
{ left=0; right=0; top=0; bottom=0; }
};void Show()
{ cout<<“left=“<<left<<“light=“<<light<<“\n”;
cout<<“top=“<<top<<“bottom=“<<bottom<<“\n”; }
};
void main(void) {
Crect r, rr(10,20,30,40); r.Show(); rr.Show();
}
对象rr调用成员函数Show(),其结果是
第10章 构造函数和析构函数
10.1 构造函数 注意:只要在类中定义了构造函数,构造函数是由系
};Cre{.c.tl.e(f).t.=.0//;不..带.ri参gh数t=的0;构造t函op数=0; bottom=0; }
};
一个类可以定义一个或多个构造函数,多个构造函数 满足函数重载原则。
下面看对象如何调用构造函数
第10章 构造函数和析构函数
10.1 构造函数
class Crect { int left,top,right,bottom;
对象r调用成员函数Show(),由于r是通过调
}
用无参构造函数初始化的,因此,输出结果是
第10章 构造函数和析构函数
10.1 构造函数
left=0 right=0 top=0 bottom=0
class Crect { int left,top,right,bottom;
public:
left=10 right=20 top=30 bottom=40
{{lceofutt=<L<;“lerfitg=h“t<=<Rl;eft<t<o“pl=iTg;ht=“b<o<tltiogmh=tB<;<“}\n”;
cout<<“top=“<<top<<“bottom=“<<bottom<<“\n”; }
};
void main(void) {
曾记否?所谓参数为缺省值的函数 就是给形参赋了初值的函数
Crect r, rr(10,20,30,40); r.Show(); rr.Show();
}
Hale Waihona Puke 第10章 构造函数和析构函数
10.1 构造函数 不带参数的构造函数也是参数为缺省值的构造函数
此前,我们介绍过参数为缺省值的函数,
class Crect
这种函数同样可用于构造函数,例如
{ int left,top,right,bottom;
相关文档
最新文档