【Linux基础】构造函数与析构函数

构造函数:对对象进行初始化。
析构函数:在对象销毁时进行内存释放等清理工作。
构造函数
1. 构造函数的特点
(1) 构造函数的函数名与类名相同。
(2) 不能定义构造函数的类型(即不能指明构造函数返回值的类型)。
(3) 构造函数应声明为公有函数。
(4) 构造函数不能在程序中调用,在对象创建时,构造函数被系统自动调用。
2. 构造函数的作用
构造函数的作用就是在对象被创建时利用特定的值构造对象,将对象初始化为一个特定的状态,使此对象具有区别于其它对象的特征。
二者是重载函数,在定义对象时,如果不给出参数,就自动调用第一个构造函数,如果给定5个参数就自动调用第二个构造函数。
例 2 为CRect类添加构造函数
class CRect
{
private:
char color[10];
……
public:
CRect( );
CRect(char *c, int t, int left, int len, int wid);
void SetColor(char *c);
……
};
CRect::CRect()
{
strcpy(color, "Black");
top = 0;
left = 0;
length = 0;
width = 0;
}
CRect::CRect(char *c, int t, int lef, int len, int wid )
{
strcpy(color, c);
top = t;
left = lef;
length = len;
width = wid;
}
void main()
{
CRect r1; //自动调用第一个构造函数
CRect r2(“red”, 10,10,100,100); //自动调用第二个构造函数
CRect r3("green", 200,200,50,50); //自动调用第二个构造函数
r1.Draw();
r2.Draw();
r3.Draw();
}
析构函数
1. 析构函数的特点
(1) 析构函数名字为符号“~”加类名。
(2) 析构函数没有参数,不能指定返回值类型。
(3) 一个类中只能定义一个析构函数,所以析构函数不能重载。
(4) 当一个对象作用域结束时,系统自动调用析构函数。
如CRect类的析构函数声明为:~CRect();
定义为: CRect::~CRect()
{ ……
}
2. 析构函数的作用
在删除一个对象前被调用,释放该对象成员的内存空间,以及其它一些清理工作。
例3 设计一个简单的字符串类,类中有两个数据成员,分别表示字符串的长度和字符串的内容,有一个构造函数和一个析构函数,函数GetLength( )返回字符串长度,函数GetContents( )获得字符串的内容,重载函数SetContents( )给字符串设置值。
#include <iostream.h>
#include <string.h>
class CString
{
private:
int length;
char *contents;
public:
CString(); //构造函数
~CString(); //析构函数
int GetLength();
void GetContents(char *str);
void SetContents(int len, char *cont);
void SetContents(char *cont);
};
CString::CString()
{
length = 0;
contents = NULL;
cout << "字符串对象初始化" << endl;
}
CString::~CString()
{
cout << contents << "被销毁" << endl;
if(contents != NULL)
delete contents;
}
int CString::GetLength()
{
return length;
}
void CString::GetContents(char *str)
{
strcpy(str, contents);
重载函数SetContents( )都是将要设置的字符串长度赋给数据成员length,然后判断原来数据成员contents是否已经有数据(即已经不为空NULL了),如果已不为空,则先释放原来的内存,再根据新字符串的长度重新申请内存。
}
void CString::SetContents(int len, char *cont)
{
length = len;
if(contents != NULL)
delete contents;
contents = new char[len+1];
strcpy(contents,cont);
cout << "两个参数的SetContents函数" << endl;
}
void CString::SetContents( char *cont)
{
length = strlen(cont);
if(contents != NULL)
delete contents;
contents = new char[length+1];
strcpy(contents,cont);
cout << "一个参数的SetContents函数" << endl;
}
void main()
{
CString str1,str2; //两次调用构造函数
str1.SetContents("第一个字符串"); //调用有一个参数的SetContents函数
str2.SetContents(20, "第二个字符串两个参数"); //调用有两个参数的SetContents函数
int i = str1.GetLength();
char string[100];
str1.GetContents(string);
cout << i << " "<< string << endl;
i = str2.GetLength();
str2.GetContents(string);
cout << i << " " << string << endl;
}
程序运行结果为:
字符串对象初始化
字符串对象初始化
一个参数的SetContents函数
两个参数的SetContents函数
12 第一个字符串
20 第二个字符串两个参数
第二个字符串两个参数被销毁
第一个字符串被销毁
拷贝构造函数
拷贝构造函数也是构造函数,它的作用是用一个已经存在的对象初始化新对象,拷贝构造函数的参数为该类对象的引用。
例4 设计一个复数类,两个数据成员分别表示复数的实部(real)和虚部(imag),三个构造函数分别在不同的情况下初始化对象,函数Set()设置复数实部和虚部的值,函数Print()输出复数,函数Add()和函数Sub()分别实现复数的加减法运算
#include "iostream.h"
class CComplex
{
private:
double real;
double imag;
public:
CComplex();
CComplex(double r, double i);
CComplex(CComplex &c); //复数类的拷贝构造函数声明
void Set(double r, double i);
void Print();
CComplex Add(CComplex c);
CComplex Sub(CComplex c);
};
CComplex::CComplex()
{
real = 0.0;
imag = 0.0;
}
CComplex::CComplex (double r, double i)
{
real = r;
imag = i;
}
CComplex::CComplex (CComplex &c) //复数类的拷贝构造函数定义
{
real = c.real;
imag = c.imag;
}
// 设置复数类的实部和虚部
void CComplex::Set(double r, double i)
{
real = r;
imag = i;
}
// 显示复数值
void CComplex:rint()
{
cout << "(" << real << "," << imag << ")" << endl;
}
// 返回两个复数的相加结果
CComplex CComplex::Add(CComplex c)
{
CComplex temp;
temp.real = real + c.real;
temp.imag = imag + c.imag;
return temp;
}
// 返回复数相减的结果
CComplex CComplex::Sub(CComplex c)
{
CComplex temp;
temp.real = real - c.real;
temp.imag = imag - c.imag;
return temp;
}
void main(void)
{
CComplex a, b(3.0,4.0), c;
CComplex d(b); //调用复数类的拷贝构造函数
cout << "a = ";
a.Print();
cout << "b = ";
b.Print();
cout << "d = ";
d.Print();
c = b.Add(d);
d = a.Sub(d);
cout << "c = ";
c.Print();
cout << "d = ";
d.Print();
}
程序运行结果为:
a = (0, 0)
b = (3, 4)
d = (3, 4)
c = (6, 8)
d = (-3, -4)  
本文转载于唯C教育,【Linux基础】万能MAKEFILE模板
http://www.weicedu.com/forum.php?mod=viewthread&tid=65&fromuid=4
(出处: http://www.weicedu.com/)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值