c ++结构体构造函数

Constructors in C++ or any other language are a member function of a class that is used to initialize the newly created objects.

C ++或任何其他语言的构造函数是用于初始化新创建的对象的类的成员函数。

They have the same name as that of the class and do not return any value.

它们具有与类相同的名称,并且不返回任何值

Working of Constructors:

施工人员的工作:

As soon as the compiler encounters a newly created object, it invokes the constructor of the corresponding class automatically.

编译器一旦遇到新创建的对象,就会自动调用相应类的构造函数。

Then, the constructor initializes the newly created object of the class.

然后,构造函数初始化该类的新创建的对象。



C ++中的构造函数类型 (Types of Constructors in C++)

Following are the types of Constructors in C++:

以下是C ++中构造函数的类型:

  • Default Constructors默认构造函数
  • Parameterized Constructors参数化构造函数
  • Copy Constructors复制 构造函数


1.默认构造函数 (1. Default Constructors)

Defaulter Constructors do not accept any arguments or parameters in it.

默认构造函数不接受任何自变量或参数。

This constructor is implicitly invoked by the compiler if not declared by the user explicitly.

如果用户未明确声明,则编译器将隐式调用此构造函数。

Syntax:

句法:


class_name()
{//code
}

Example:

例:


#include <iostream>
using namespace std; class Display {
public: int x; // Default Constructor Display() { x = 10;}
}; int main()
{ Display d; cout << "The value of x: " << d.x << endl ;return 1;
} 

Output:

输出:


The value of x: 10


2.参数化构造函数 (2. Parameterized Constructors)

Parameterized constructors allowing the passing of parameters during the creation of an object.

参数化构造函数允许在对象创建期间传递参数。

It helps in the initialization of the objects and provides distinct values to the different data variables of the objects.

它有助于对象的初始化,并为对象的不同数据变量提供不同的值。

Syntax:

句法:


class_name(parameters)
{//code
}

Example:

例:


#include <iostream>
using namespace std;
class Multiply{
public://Parameterized constructorMultiply(int a, int b) {int result = a*b;cout<<result<<endl;}
};
int main(void){Multiply m(10, 10); //implicit call to the constructorMultiply ex = Multiply(100, 5); //explicit call to the constructorreturn 0;
}

Output:

输出:


100
500


3.复制构造函数 (3. Copy Constructors)

Copy Constructor creates a copy of an existing object of the corresponding class. Thus, it creates a copy of all the data variables of one object into another object.

Copy构造函数创建相应类的现有对象副本 。 因此,它将创建一个对象的所有数据变量到另一个对象的副本。

Syntax:

句法:


class_name(const class_name & object_name)
{//code
}

Example:

例:


#include<iostream>
using namespace std;
class Construct
{int a, b;   public:Construct(int aa, int bb){a = aa;b = bb;}Construct (const Construct &con) // Copy constructor{ a = con.a;b = con.b;}void show (){cout<<a<<" "<<b<<endl;}
};int main()
{Construct C(5, 5);     Construct C1 = C;      // Calling the Copy constructorcout<<"Copy constructor:\n ";C1.show();return 0;
}

Output:

输出:


Copy constructor:5 5


C ++中的构造方法重载 (Constructor Overloading in C++)

Overloading serves with the same function definition having different numbers and types of arguments.

重载用于具有不同数量和参数类型的相同函数定义。

In a similar fashion of Function Overloading, even the Constructors can be overloaded in C++ language.

以类似的函数重载方式,即使构造函数也可以用C ++语言重载。

Example:

例:


#include<iostream>
using namespace std;
class Area_parameters
{public:int a, len, width;Area_parameters(int x){a = x;cout<<x<<endl;}Area_parameters(int l, int b){len = l;width = b;cout<<l<<" "<<b<<endl;}
};int main()
{Area_parameters obj(5);Area_parameters obj1(10, 20);}

Output:

输出:


5
10 20


构造函数与成员函数之间的区别 (Difference between Constructors and Member Functions)

  • Constructor doesn’t provide any return type, on the other hand, Functions do have a return type.构造函数不提供任何返回类型,另一方面,函数确实具有返回类型。
  • Constructor gets implicitly called when an object is created while Member functions need to be called explicitly.当需要显式调用成员函数时,在创建对象时隐式调用构造函数。


结论 (Conclusion)

Thus, in this article, we have understood the functionality served by the Constructors in C++.

因此,在本文中,我们了解了C ++中的构造函数所提供的功能。



参考资料 (References)

Constructors in C++ Documentation

C ++文档中的构造函数

翻译自: https://www.journaldev.com/35031/constructors-in-c-plus-plus

c ++结构体构造函数

c ++结构体构造函数_C ++中的构造函数相关推荐

  1. C语言结构体与C++中结构体和类的区别

    在C++中除了类中可以有构造函数和析构函数外,结构体中也可以包含构造函数和析构函数,这是因为结构体和类基本雷同,唯一区别是,类中成员变量默认为私有,而结构体中则为公有.注意,C++中的结构体是可以有析 ...

  2. C语言试题五十二之学生的记录由学号和成绩组称个,n名大学生得数据已在主函数中放入结构体数组a中,请编写函数fun,它的功能时:按分数的高低排列学生的记录,高分在前。

    1. 题目 请编写一个函数void function(Student a[], int n),其功能时:学生的记录由学号和成绩组称个,n名大学生得数据已在主函数中放入结构体数组a中,请编写函数fun, ...

  3. c语言结构体在内存中的存储,C语言结构体在内存中的存储情况探究------内存对齐...

    条件(先看一下各个基本类型都占几个字节): voidsize_(){ printf("char类型:%d", sizeof(char)); printf("int类型:% ...

  4. c语言 由函数组成的数组,学生的记录由学号和成绩组成,N名学生的数据已在主函数中放入结构体数组s中,请编写函数fun(),它的_考题宝...

    学生的记录由学号和成绩组成,N名学生的数据已在主函数中放入结构体数组s中,请编写函数fun(),它的功能是按分数的高低排列学生的记录,低分在前. 注意:部分源程序给出如下. 请勿改动主函数main和其 ...

  5. linux查看内存条pn,实验:使用GDB查看结构体在内存中的存储方式

    结构体在内存中的表示形式是怎么样的? 结构体在内存中和普通变量存储没有太大的区别. 首先我们看看,计算机如何读取普通变量:   普通变量例如int是占据4个字节,计算机读内存的时候会从起始地址开始读, ...

  6. MATLAB结构体写入excel中

    MATLAB结构体写入excel中 C = struct2cell(Data): Data 是要转化得结构体 C 是转成功得元胞数组 xlswrite('Names.xlsx',C{1}); Name ...

  7. 结构体是否有默认的构造函数?(没有,如果需要,需要自己写)

    // node type used by winner tree#ifndef player_ #define player_struct player //竞赛树的每个外节点 {int id, ke ...

  8. java显式构造函数_C++中的显式构造函数

    有如下一个简单的复数类: classClxComplex {public: ClxComplex(doubledReal=0.0,doubledImage=0.0){m_dReal=dReal;dIm ...

  9. c语言结构体内嵌结构体指针_C语言中的结构指针

    c语言结构体内嵌结构体指针 Prerequisite: 先决条件: Structures in C programming language. C编程语言中的结构. Dynamic Memory al ...

最新文章

  1. 硬盘显示容量和实际容量不符合_为啥我买的64G U盘实际只有57G?聊聊存储市场的“不足量”现象...
  2. 在asp.net中调用Office来制作各种(3D)统计图
  3. 基于概率论的生成式建模新模式
  4. [SDOI2017]新生舞会
  5. jstl 处理Date 时间
  6. python文件操作模式是什么,python --文件操作模式详解
  7. Java 遍历指定目录下的所有目录
  8. 孙悟空谈即时通讯有多神通广大
  9. mysql gis空间数据库_GIS开发:使用空间数据库
  10. 更换Homebrew为中科大源
  11. Pentaho的Mondrian对Hive的支持
  12. oracle卸载和服务问题
  13. php读取json三级,php-流明从文件中读取JSON
  14. 2010 我的求职经历(2)
  15. LinuxC:锁、条件变量、信号量实现线程间的同步 生产者与消费者 pthread_mutex_init pthread_cond_init sem_init
  16. Oracle触发器(当A表新增/修改/删除时,同步数据到B表)
  17. MySQL数据库课程设计_Wincc实现与数据库的交互以及报表的实现方式
  18. 如何利用LaTex的写毕业论文
  19. 企业微信加密消息体_无代码开发能保障企业系统数据的安全吗?这篇文章为你全面解析!...
  20. 科学的互联网思想 指引我国网络强国建设稳步前行

热门文章

  1. 用Netty解析Redis网络协议
  2. SharePoint【学习笔记】-- SPWeb.EnsureUser()注意AllowUnsafeUpdates=true
  3. [转载] 包含对象的json格式_如何把JSON数据格式转换为Python的类对象?
  4. [转载] Python一行代码实现1到100之和
  5. Vue.js 学习笔记 四 用一,二,三的知识做个跑马灯
  6. UDP协议和socketserver以及文件上传
  7. 2018.4.3 做lab0
  8. linux 安装 PHP fileinfo 扩展
  9. 数据结构上机实践第四周项目5 - 猴子选大王
  10. 简述无人驾驶感知功能