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

推荐的帖子

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

c构造函数和析构函数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值