C++类构造函数和析构函数

一、构造函数

1、构造函数是初始化类对象的类的特殊成员函数。构造函数名称与类名称相同,并且没有返回类型。

在讲述构造函数时,我先引用一段代码来供大家分析和借鉴:

#include <iostream>
using namespace std;
class constructorDemo{
public:
   int num;
   char ch;
   /* This is a default constructor of the
    * class, do note that it's name is same as
    * class name and it doesn't have return type.
    */
   constructorDemo() {
      num = 100; ch = 'A';
   }
};
int main(){
   /* This is how we create the object of class,
    * I have given the object name as obj, you can
    * give any name, just remember the syntax:
    * class_name object_name;
    */
   constructorDemo obj;

   /* This is how we access data members using object
    * we are just checking that the value we have
    * initialized in constructor are reflecting or not.
    */
   cout<<"num: "<<obj.num<<endl;
   cout<<"ch: "<<obj.ch;
   return 0;
}

 输出结果:

num: 100
ch: A

由上面的引例,现在分析一下构造函数与类的成员函数的不同之处。

1)构造函数没有返回类型。成员函数具有返回类型。

2)创建类的对象时,会自动调用构造函数。需要使用类的对象显式调用成员函数。

3)不在类中创建任何构造函数时,C++ 编译器生成一个默认构造函数并将其插入到我们的代码中。这同样适用于成员函数。

编译器生成的默认构造函数的外观:

class XYZ
{ 
    ....
    XYZ()
    {
        //Empty no code
    }
};

2、构造函数的类型

1)默认构造函数

默认构造函数没有任何参数(或参数)。例如:

#include <iostream>
using namespace std;
class Website{
public:
   //Default constructor
   Website() {
      cout<<"Welcome to BeginnersBook"<<endl;
   }
};
int main(void){
   /*creating two objects of class Website.
    * This means that the default constructor
    * should have been invoked twice.
    */
   Website obj1;
   Website obj2;
   return 0;
}

输出:

Welcome to BeginnersBook

Welcome to BeginnersBook

注:

如果未在类中指定任何构造函数,则编译器将在代码中插入没有代码(空体)的默认构造函数。

 2)参数化构造函数

带参数的构造函数称为参数化构造函数。这些类型的构造函数允许在创建对象时传递参数。

假设类名是XYZ,则默认构造函数为:

XYZ() {

}
....
XYZ obj;
....

参数化构造函数:

XYZ(int a, int b) {

}
...
XYZ obj(10, 20);

如:

#include <iostream>
using namespace std;
class Add{
public:
   //Parameterized constructor
   Add(int num1, int num2) {
     cout<<(num1+num2)<<endl;
   }
};
int main(void){
   /* One way of creating object. Also
    * known as implicit call to the
    * constructor
    */
   Add obj1(10, 20);
   /* Another way of creating object. This
    * is known as explicit calling the
    * constructor.
    */
   Add obj2 = Add(50, 60);
   return 0;
}

输出:

30
110

二、类的析构函数

类的析构函数是类的一种特殊的成员函数,它会在每次删除所创建的对象时执行。

析构函数的名称与类的名称是完全相同的,只是在前面加了个波浪号(~)作为前缀,它不会返回任何值,也不能带有任何参数。析构函数有助于在跳出程序(比如关闭文件、释放内存等)前释放资源。

下面引用的实例有助于更好地理解析构函数的概念:

#include <iostream>
 
using namespace std;
 
class Line
{
   public:
      void setLength( double len );
      double getLength( void );
      Line();   // 这是构造函数声明
      ~Line();  // 这是析构函数声明
 
   private:
      double length;
};
 
// 成员函数定义,包括构造函数
Line::Line(void)
{
    cout << "Object is being created" << endl;
}
Line::~Line(void)
{
    cout << "Object is being deleted" << endl;
}
 
void Line::setLength( double len )
{
    length = len;
}
 
double Line::getLength( void )
{
    return length;
}
// 程序的主函数
int main( )
{
   Line line;
 
   // 设置长度
   line.setLength(6.0); 
   cout << "Length of line : " << line.getLength() <<endl;
 
   return 0;
}

输出:

Object is being created
Length of line : 6
Object is being deleted

补充一个知识点:

在C++中,"::"是作用域运算符,它可以用来访问命名空间、类、成员变量、成员函数等。在类的作用域内部,"::"可以用来定义和实现类的成员函数,例如Line类中就定义了一个构造函数Line(double len),它的完整声明为:Line::Line(double len)。

这里的"Line::"表示该构造函数属于Line类的作用域,"Line(double len)"表示该函数的名称及参数列表。因此在代码中出现Line::Line(double len),表示定义并实现Line类的构造函数。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小小_扫地僧

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值