博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c构造函数和析构函数_C ++构造函数和析构函数| 查找输出程序| 套装1
阅读量:2532 次
发布时间:2019-05-11

本文共 4258 字,大约阅读时间需要 14 分钟。

c构造函数和析构函数

Program 1:

程序1:

#include 
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 00 0 [or - Some garbage value]

Explanation:

说明:

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

在上面的程序中,我们创建了一个带有数据成员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 
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 010 2030 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 
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 2030 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构造函数和析构函数

转载地址:http://qbazd.baihongyu.com/

你可能感兴趣的文章
LimeJS指南3
查看>>
关于C++ const成员的一些细节
查看>>
《代码大全》学习摘要(五)软件构建中的设计(下)
查看>>
C#检测驱动是否安装的问题
查看>>
web-4. 装饰页面的图像
查看>>
微信测试账户
查看>>
Android ListView上拉获取下一页
查看>>
算法练习题
查看>>
学习使用Django一 安装虚拟环境
查看>>
Hibernate视频学习笔记(8)Lazy策略
查看>>
CSS3 结构性伪类选择器(1)
查看>>
IOS 杂笔-14(被人遗忘的owner)
查看>>
自动测试用工具
查看>>
前端基础之BOM和DOM
查看>>
[T-ARA/筷子兄弟][Little Apple]
查看>>
编译Libgdiplus遇到的问题
查看>>
【NOIP 模拟赛】Evensgn 剪树枝 树形dp
查看>>
java学习笔记④MySql数据库--01/02 database table 数据的增删改
查看>>
两台电脑如何实现共享文件
查看>>
组合模式Composite
查看>>