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

c构造函数和析构函数

Program 1:

程序1:

#include <iostream>
using namespace std;

class Sample {
private:
    int X;

public:
    Sample()
    {
        X = 0;
    }

    void set(int x)
    {
        X = x;
    }
    void print()
    {
        cout << X << endl;
    }
};

int main()
{
    Sample S[2] = { Sample(), Sample() };

    S[0].set(10);
    S[1].set(20);

    S[0].print();
    S[1].print();

    return 0;
}

Output:

输出:

10
20

Explanation:

说明:

In the above program, we created a class Sample with data member X, default constructor, set() and print() member functions.

在上面的程序中,我们创建了带有数据成员 X默认构造函数set()print()成员函数的类Sample

Now coming to the main() function, here we created an array of objects that instantiated by default constructor. And then called set() and print() function using an array of objects then it will print above output.

现在转到main()函数 ,这里我们创建了一个对象数组,这些对象默认由构造函数实例化。 然后使用对象数组调用set()print()函数,然后它将在输出上方进行打印。

Program 2:

程式2:

#include <iostream>
#include <stdlib.h>
using namespace std;

class Sample {
private:
    int X;

public:
    Sample()
    {
        X = 0;
        cout << "Constructor called";
    }

    void set(int x)
    {
        X = x;
    }
    void print()
    {
        cout << X << endl;
    }
};

int main()
{
    Sample* S;

    S = (Sample*)malloc(sizeof(Sample));

    S.set(10);
    S.print();

    return 0;
}

Output:

输出:

main.cpp: In function ‘int main()’:
main.cpp:32:7: error: request for member ‘set’ in ‘S’, 
which is of pointer type ‘Sample*’ (maybe you meant to use ‘->’ ?)
     S.set(10);
       ^~~
main.cpp:33:7: error: request for member ‘print’ in ‘S’, 
which is of pointer type ‘Sample*’ (maybe you meant to use ‘->’ ?)
     S.print();
       ^~~~~

Explanation:

说明:

The above program will generate an error, because, S is a pointer, and we used Dot (.) Operator to call set() and print() member functions instead of referential operator (->).

上面的程序将产生错误,因为S是指针,并且我们使用Dot(。)运算符而不是引用运算符(->)来调用set()print()成员函数。

Program 3:

程式3:

#include <iostream>
#include <stdlib.h>
using namespace std;

class Sample {
private:
    int X;

public:
    Sample()
    {
        X = 0;
        cout << "Constructor called";
    }

    void set(int x)
    {
        X = x;
    }
    void print()
    {
        cout << X << endl;
    }
};

int main()
{
    Sample* S;

    S = (Sample*)malloc(sizeof(Sample));

    (*S).set(10);
    (*S).print();

    return 0;
}

Output:

输出:

10

Explanation:

说明:

In the above program, we created a class Sample with data member X, default constructor, set() and print() member functions.

在上面的程序中,我们创建了带有数据成员X ,默认构造函数, set()print()成员函数的类Sample

Coming to the main() function, here we declared a pointer of the class Sample and instantiate by malloc() function and then called set() and print() functions using the pointer. But, when we use malloc() function it will not call the constructor.

来到main()函数,这里我们声明了Sample类的指针,并通过malloc()函数实例化,然后使用该指针调用set()print()函数。 但是,当我们使用malloc()函数时 ,它将不会调用构造函数。

Recommended posts

推荐的帖子

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

c构造函数和析构函数

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值