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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

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

發布時間:2025/3/11 编程问答 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c构造函数和析构函数_C ++构造函数和析构函数| 查找输出程序| 套装2 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

c構造函數和析構函數

Program 1:

程序1:

#include<iostream> using namespace std;class Sample {private:int X;int Y;public:Sample(int x, int y){X = x;Y = y;}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:

輸出:

main.cpp: In function ‘int main()’: main.cpp:32:12: error: no matching function for call to ‘Sample::Sample()’Sample S[2];

Explanation:

說明:

The above code will generate a syntax error because in the main() function we created an array of objects that must be instantiated by the default constructor, but in the class, the default constructor is not defined.

上面的代碼將產生語法錯誤,因為在main()函數中,我們創建了必須由默認構造函數實例化的對象數組 ,但是在類中,未定義默認構造函數。

Program 2:

程式2:

#include <iostream> using namespace std;class Sample { private:int X;int Y;public:Sample(int x, int y){X = x;Y = y;}void set(int x, int y){X = x;Y = y;}void print(){cout << X << " " << Y << endl;} };int main() {Sample S[2] = { Sample(0, 0), Sample(0, 0) };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 parameterized constructor, set() and print() member functions.?

在上面的程序中,我們創建了一個Sample類,其中包含兩個數據成員 X和Y ,一個參數化的構造函數 , set()和print()成員函數。

Now coming to the main() function, we created an array of objects and that will instantiated by the parameterized constructor. And created a pointer that is being initialized with the base address of array objects. And then called set() and print() functions using the pointer.

現在進入main()函數,我們創建了一個對象數組,該數組將由參數化構造函數實例化。 并創建了一個使用數組對象的基地址初始化的指針。 然后使用指針調用set()和print()函數。

Program 3:

程式3:

#include <iostream> using namespace std;class Sample { private:int X;public:Sample() const{}void set(int x){X = x;}void print(){cout << X << endl;} };int main() {Sample S;S.set(10);S.print();return 0; }

Output:

輸出:

main.cpp:9:14: error: constructors may not be cv-qualifiedSample() const^~~~~

Explanation:

說明:

The above program will generate an error because we cannot create a constructor as a const member function in C++.

上面的程序將產生錯誤,因為我們不能在C ++中將構造函數創建為const成員函數。

Recommended posts

推薦的帖子

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

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

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

c構造函數和析構函數

總結

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

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