c构造函数和析构函数_C ++构造函数和析构函数| 查找输出程序| 套装1

c构造函数和析构函数

Program 1:

程序1:

#include <iostream>
using namespace std;
class Sample {
private:
int X;
int Y;
public:
Sample()
{
X = 0;
Y = 0;
}
Sample(int x, int y)
{
X = X;
Y = Y;
}
void print()
{
cout << X << " " << Y << endl;
}
};
int main()
{
Sample S1;
Sample S2(10, 20);
S1.print();
S2.print();
return 0;
}

Output:

输出:

0 0
0 0 [or - Some garbage value]

Explanation:

说明:

In the above program, we created a Sample class with data member X and Y, two constructors one is default constructor and the other is the parameterized constructor.

在上面的程序中,我们创建了一个带有数据成员XYSample类,两个构造函数一个是默认构造函数 ,另一个是参数化构造函数 。

The default constructor will initialize the data members by 0. But there is a problem with parameterized constructor; here we assign data members by itself instead of local parameters. Then, data members X, and Y will contain garbage values.

默认的构造函数将以0初始化数据成员。 在这里,我们自己分配数据成员,而不是局部参数。 然后,数据成员XY将包含垃圾值。

Look to the main() function, here we created two objects S1 and S2.

看一下main()函数,在这里我们创建了两个对象S1S2

S1 initialized with the default constructor so the data members of S1 will be initialized with 0.

S1使用默认构造函数初始化,因此S1的数据成员将使用0初始化。

S2 object will call the parameterized constructor, so data members of S2 will contain garbage values.

S2对象将调用参数化的构造函数,因此S2的数据成员将包含垃圾值。

When we call the print() function with S1, then it will print "0 0" on the console screen. And when we call the print() function with S2, then it will print garbage value on the console screen.

当我们使用S1调用print()函数时,它将在控制台屏幕上打印“ 0 0” 。 当我们使用S2调用print()函数时,它将在控制台屏幕上打印垃圾值。

Program 2:

程式2:

#include <iostream>
using namespace std;
class Sample {
private:
int X;
int Y;
public:
Sample()
{
X = 0;
Y = 0;
}
Sample(int x, int y)
{
X = x;
Y = y;
}
void print()
{
cout << X << " " << Y << endl;
}
};
int main()
{
Sample S1;
Sample S2 = Sample(10, 20);
Sample S3(30, 40);
S1.print();
S2.print();
S3.print();
return 0;
}

Output:

输出:

0 0
10 20
30 40

Explanation:

说明:

In the above program, we created a Sample class with data member X and Y, we created two constructors one is the default constructor and the other is the parameterized constructor.

在上面的程序中,我们创建了一个带有数据成员XYSample类,我们创建了两个构造函数,一个是默认构造函数,另一个是参数化构造函数。

The default constructor will initialize the data members by 0, and the parameters constructor will initialize data members by specified values.

默认构造函数将通过0初始化数据成员,而参数构造函数将通过指定的值初始化数据成员。

Coming to the main() function. Here we created 3 objects of S1, S2, and S3. S1 initialized by default construct whereas S2 and S3 initialized by the parameterized constructor. S2 and S3 are used in different ways to create objects with the parameterized constructor, but both have the same effect.

来到main()函数。 在这里,我们创建了S1S2S3的 3个对象。 S1由默认构造初始化,而S2S3由参数化构造函数初始化。 S2S3以不同的方式用于通过参数化构造函数创建对象,但是两者具有相同的效果。

S1 initialized X and Y by 0 and 0 respectively.
S2 initialized X and Y by 10 and 20 respectively.
S3 initialized X and Y by 30 and 40 respectively.

S1分别用0和0初始化XY。
S2分别XY初始化为10和20。
S3分别XY初始化为30和40。

We print the values of all objects using the print() function.

我们使用print()函数打印所有对象的值。

Program 3:

程式3:

#include <iostream>
using namespace std;
class Sample {
private:
int X;
int Y;
public:
Sample()
{
X = 0;
Y = 0;
}
void set(int x, int y)
{
X = x;
Y = y;
}
void print()
{
cout << X << " " << Y << endl;
}
};
int main()
{
Sample S[2];
Sample* PTR;
PTR = S;
PTR[0].set(10, 20);
PTR[1].set(30, 40);
PTR[0].print();
PTR[1].print();
return 0;
}

Output:

输出:

10 20
30 40

Explanation:

说明:

In the above program, we created a class Sample that contains two data members X and Y, one default constructor, set() and print() member functions.

在上面的程序中,我们创建了一个Sample类,其中包含两个数据成员XY ,一个默认构造函数, set()print()成员函数。

Coming to the main() function, we created the array of objects with size 2, and a pointer that initialized with the base address of array objects. And then called set() and print() functions using the pointer, by this way functions will be called correctly and they print the assigned values.

来到main()函数中,我们创建了大小为2的对象数组,以及一个以数组对象的基地址初始化的指针。 然后使用指针调用set()print()函数,这样将正确调用函数,并打印分配的值。

Recommended posts

推荐的帖子

  • C++ Constructor and Destructor | Find output programs | Set 2

    C ++构造函数和析构函数| 查找输出程序| 套装2

  • C++ Constructor and Destructor | Find output programs | Set 3

    C ++构造函数和析构函数| 查找输出程序| 套装3

  • C++ Constructor and Destructor | Find output programs | Set 4

    C ++构造函数和析构函数| 查找输出程序| 套装4

  • C++ Constructor and Destructor | Find output programs | Set 5

    C ++构造函数和析构函数| 查找输出程序| 套装5

  • C++ Default Argument | Find output programs | Set 1

    C ++默认参数| 查找输出程序| 套装1

  • C++ Default Argument | Find output programs | Set 2

    C ++默认参数| 查找输出程序| 套装2

  • C++ Arrays | Find output programs | Set 1

    C ++数组| 查找输出程序| 套装1

  • C++ Arrays | Find output programs | Set 2

    C ++数组| 查找输出程序| 套装2

  • C++ Arrays | Find output programs | Set 3

    C ++数组| 查找输出程序| 套装3

  • C++ Arrays | Find output programs | Set 4

    C ++数组| 查找输出程序| 套装4

  • C++ Arrays | Find output programs | Set 5

    C ++数组| 查找输出程序| 套装5

  • C++ Class and Objects | Find output programs | Set 1

    C ++类和对象| 查找输出程序| 套装1

  • C++ Class and Objects | Find output programs | Set 2

    C ++类和对象| 查找输出程序| 套装2

  • C++ Class and Objects | Find output programs | Set 3

    C ++类和对象| 查找输出程序| 套装3

  • C++ Class and Objects | Find output programs | Set 4

    C ++类和对象| 查找输出程序| 套装4

  • C++ Class and Objects | Find output programs | Set 5

    C ++类和对象| 查找输出程序| 套装5

翻译自: https://www.includehelp.com/cpp-tutorial/constructor-and-destructor-find-output-programs-set-1.aspx

c构造函数和析构函数

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/380345.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

我的项目的架构(三)

TranContext是一个比较重要的类,在这个类中,使用了反射方法,实现了根据配置文件动态创建类,实现了接口的作用. 1 publicabstractclassConfigurationFactory2 {3 publicConfigurationFactory()4 {5 //6 //TODO: 在此处添加构造函数逻辑7 //8 }9 publicstaticobjectCreateObject(…

1的个数

描述 小南刚学了二进制&#xff0c;他想知道一个数的二进制表示中有多少个1&#xff0c;你能帮他写一个程序来完成这个任务吗&#xff1f; 输入 第一行输入一个整数N&#xff0c;表示测试数据的组数(1< N<1000) 每组测试数据只有一行&#xff0c;是一个整数M(0< M…

单片机编程文件组织形式(个人编程规范)

1、外设或系统资源驱动函数组织形式。所有函数写在.c文件里面&#xff0c;.c最前面包含自身头文件。每个.c文件都有一个相对应的.h文件&#xff0c;其他文件或系统只调用.h文件。 2、.c文件除了最前面要包含自身头文件外&#xff0c;应该尽量全部是函数定义&#xff0c;接口信息…

计算机在我国开始被应用于,计算机应用推动自动化与信息化的发展

计算机应用推动自动化与信息化的发展【摘要】本文简单介绍了我国计算机应用的发展情况&#xff0c;分析了计算机应用同自动化和信息化之间的关系&#xff0c;通过计算机应用在我国各领域中的运用&#xff0c;来体现计算机应用对自动化和信息化的推动作用。【关键词】计算机&…

cobaltstrike生成一个原生c,然后利用xor加密解密执行

首先cobaltstrike生成一个原生c&#xff0c;我的是&#xff1a; /* length: 797 bytes */ unsigned char buf[] "\xfc\xe8\x89\x00\x00\x00\x60\x89\xe5\x31\xd2\x64\x8b\x52\x30\x8b\x52\x0c" "\x8b\x52\x14\x8b\x72\x28\x0f\xb7\x4a\x26\x31\xff\x31\xc0\xac…

tolowercase_JavaScript中的String toLowerCase()方法与示例

tolowercase字符串toLowerCase()方法 (String toLowerCase() Method) toLowerCase() Method is a string method in JavaScript, it is used to converts all alphabets in lowercase and returns the new string with lowercase alphabets. toLowerCase()方法是JavaScript中的…

这样就可以很方便的知道明天的天气了

今天在侧边栏加了一个实用的小东西——天气预报。它可以根据来访者的ip地址自动判断地区&#xff0c;并展现今天以及明天的天气预报。这样来看blog的时候就可以知道什么时候该去收衣服啦&#xff5e;哈哈&#xff01;实现代码其实很简单。就是套一个IFRAME&#xff0c;里面套个…

队花的烦恼一

描述 ACM队的队花C小经常抱怨&#xff1a;“C语言中的格式输出中有十六、十、八进制输出&#xff0c;然而却没有二进制输出&#xff0c;哎&#xff0c;真遗憾&#xff01;谁能帮我写一个程序实现输入一个十进制数n&#xff0c;输出它的二进制数呀&#xff1f;” 难道你不想帮…

计算机数学基础模拟试题,计算机数学基础》模拟考试试题.doc

PAGE / NUMPAGES《计算机数学基础(2)》模拟试题(1)一、单项选择题(每小题3分&#xff0c;共15分)1. 数值x*的近似值x0.121510-2&#xff0c;若满足( )&#xff0c;则称x有4位有效数字。A. B.C. D.2.设矩阵&#xff0c;那么以A为系数矩阵的线性方程组AXb的雅可比迭代矩阵为( )。…

压缩矩阵

压缩矩阵&#xff1a;指为多个值相同的元素只分配一个存储空间&#xff0c;对零元素不分配存储空间特殊矩阵&#xff1a;指具有许多相同矩阵元素或零元素&#xff0c;并且这些相同矩阵元素或零元素的分配有一定规律性 1、对称矩阵 对称矩阵&#xff1a;矩阵每个元素都有aijaj…

线性方程组 python_线性方程组的表示 使用Python的线性代数

线性方程组 pythonPrerequisites: 先决条件&#xff1a; Defining a Vectors 定义向量 Defining a Matrix 定义矩阵 In this article, we are going to learn how to represent a linear equation in Python using Linear Algebra. For example we are considering an equatio…

初步体验数据驱动之美---TreeView

1.前言继上一篇《WPF应用基础篇---TreeView》的发布之后&#xff0c;有部分朋问我关于里面一些基础应用的问题&#xff0c;可能是我写得不够详细&#xff0c;所以在这里&#xff0c;我想再次那文章中的案例来谈谈初步体验数据驱动之美&#xff0c;摆脱旧WinForm编程习惯(靠触发…

c#2.0匿名方法五(转)

在循环控制结构内使用匿名方法的局部变量的用法   当处理循环控制结构时将局部变量封装入类的数据成员有着有趣但危险的一面&#xff0c;让我们看看下面代码&#xff1a;public class Program{ public delegate void MyDelegate(); public static void Main(string[] args)…

不可以!

描述 判断&#xff1a;两个数x、y的正负性。 要求&#xff1a;不可以使用比较运算符&#xff0c;即”<”,”>”,”<”,”>”,””,”!”。 输入 有多组数据&#xff0c;每组数据占一行&#xff0c;每一行两个数x&#xff0c;y。 x、y保证在int范围内。 输出 …

树的基本概念

0x01 树 树&#xff1a;n个结点的有限集合&#xff0c;n0&#xff0c;空树任何非空树只有一个根结点n个结点的树只有n-1条边&#xff08;除根结点&#xff0c;每个结点只有一个前驱&#xff0c;一个前驱一条边&#xff0c;根据这个算的&#xff09;有序树与无序树&#xff1a;…

制作两个字符串字谜的最小步骤数

Prerequisite: 先决条件&#xff1a; Hashing data structure 散列数据结构 Problem statement: 问题陈述&#xff1a; Find the minimum number of steps to make two strings Anagram. Both strings are of the same length and the lower case. At each step, you can con…

小学计算机教学教师培训,例谈小学信息技术课堂的有效教学

例谈小学信息技术课堂的有效教学在社会的各个领域&#xff0c;大家都不可避免地会接触到论文吧&#xff0c;论文可以推广经验&#xff0c;交流认识。为了让您在写论文时更加简单方便&#xff0c;以下是小编整理的例谈小学信息技术课堂的有效教学的论文相关内容&#xff0c;供大…

关于varchar2在pl/sql和schema级别的最大值

http://www.cublog.cn/u/30637/showart_1919003.html http://www.itpub.net/thread-1216548-1-1.html  转载于:https://www.cnblogs.com/wzc998/archive/2011/08/12/2136406.html

业务工作流平台设计(九)

自定义审核活动 前面已经讲了许多有关自定义活动在设计上需要注意的一些事项&#xff0c;但对于自定义审核活动来讲&#xff0c;我们的设计还要有许多工作要进行。 为了简化用户的流程上的设计将流程的一些算法封装到自定义活动中可以大大增加自定义活动的使用的方便性。其直接…

国王的魔镜

描述 国王有一个魔镜&#xff0c;可以把任何接触镜面的东西变成原来的两倍——只是&#xff0c;因为是镜子嘛&#xff0c;增加的那部分是反的。 比如一条项链&#xff0c;我们用AB来表示&#xff0c;不同的字母表示不同颜色的珍珠。如果把B端接触镜面的话&#xff0c;魔镜会把…