1、构造函数是初始化类对象的类的特殊成员函数。构造函数名称与类名称相同,并且没有返回类型。
在讲述构造函数时,我先引用一段代码来供大家分析和借鉴:
- #include
- 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: "<
- cout<<"ch: "<
- return 0;
- }
输出结果:
num: 100
ch: A
由上面的引例,现在分析一下构造函数与类的成员函数的不同之处。
1)构造函数没有返回类型。成员函数具有返回类型。
2)创建类的对象时,会自动调用构造函数。需要使用类的对象显式调用成员函数。
3)不在类中创建任何构造函数时,C++ 编译器生成一个默认构造函数并将其插入到我们的代码中。这同样适用于成员函数。
编译器生成的默认构造函数的外观:
- class XYZ
- {
- ....
- XYZ()
- {
- //Empty no code
- }
- };
-
2、构造函数的类型
1)默认构造函数
默认构造函数没有任何参数(或参数)。例如:
- #include
- using namespace std;
- class Website{
- public:
- //Default constructor
- Website() {
- cout<<"Welcome to BeginnersBook"<
- }
- };
- 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
- using namespace std;
- class Add{
- public:
- //Parameterized constructor
- Add(int num1, int num2) {
- cout<<(num1+num2)<
- }
- };
- 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
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() <
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类的构造函数。
-
相关阅读:
可编程 USB 转串口适配器开发板 DS1302 时钟芯片参数读取与修改
【牛客网面试必刷TOP101】二叉树篇(三)
在进行自动化测试,遇到验证码的问题,怎么办?
DAO 与存储库Repository模式
什么是Vue.js的响应式系统(reactivity system)?如何实现数据的双向绑定?
力扣解法汇总641-设计循环双端队列
Linux第4课 Linux的基本操作
css常见问题处理
linux-如何用起来ubuntu
Pangolin安装报错解决
-
原文地址:https://blog.csdn.net/m0_73931287/article/details/133531806