日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2025/3/11 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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

推薦的帖子

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

    C ++構造函數和析構函數| 查找輸出程序| 套裝1

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

    C ++構造函數和析構函數| 查找輸出程序| 套裝2

  • 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-3.aspx

c構造函數和析構函數

總結

以上是生活随笔為你收集整理的c构造函数和析构函数_C ++构造函数和析构函数| 查找输出程序| 套装3的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。