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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

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

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

c構(gòu)造函數(shù)和析構(gòu)函數(shù)

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.

在上面的程序中,我們創(chuàng)建了一個帶有數(shù)據(jù)成員X和Y的Sample類,兩個構(gòu)造函數(shù)一個是默認構(gòu)造函數(shù) ,另一個是參數(shù)化構(gòu)造函數(shù) 。

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.

默認的構(gòu)造函數(shù)將以0初始化數(shù)據(jù)成員。 在這里,我們自己分配數(shù)據(jù)成員,而不是局部參數(shù)。 然后,數(shù)據(jù)成員X和Y將包含垃圾值。

Look to the main() function, here we created two objects S1 and S2.

看一下main()函數(shù),在這里我們創(chuàng)建了兩個對象S1和S2 。

S1 initialized with the default constructor so the data members of S1 will be initialized with 0.

S1使用默認構(gòu)造函數(shù)初始化,因此S1的數(shù)據(jù)成員將使用0初始化。

S2 object will call the parameterized constructor, so data members of S2 will contain garbage values.

S2對象將調(diào)用參數(shù)化的構(gòu)造函數(shù),因此S2的數(shù)據(jù)成員將包含垃圾值。

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調(diào)用print()函數(shù)時,它將在控制臺屏幕上打印“ 0 0” 。 當我們使用S2調(diào)用print()函數(shù)時,它將在控制臺屏幕上打印垃圾值。

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.

在上面的程序中,我們創(chuàng)建了一個帶有數(shù)據(jù)成員X和Y的Sample類,我們創(chuàng)建了兩個構(gòu)造函數(shù),一個是默認構(gòu)造函數(shù),另一個是參數(shù)化構(gòu)造函數(shù)。

The default constructor will initialize the data members by 0, and the parameters constructor will initialize data members by specified values.

默認構(gòu)造函數(shù)將通過0初始化數(shù)據(jù)成員,而參數(shù)構(gòu)造函數(shù)將通過指定的值初始化數(shù)據(jù)成員。

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()函數(shù)。 在這里,我們創(chuàng)建了S1 , S2和S3的 3個對象。 S1由默認構(gòu)造初始化,而S2和S3由參數(shù)化構(gòu)造函數(shù)初始化。 S2和S3以不同的方式用于通過參數(shù)化構(gòu)造函數(shù)創(chuàng)建對象,但是兩者具有相同的效果。

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初始化X和Y。
S2分別將X和Y初始化為10和20。
S3分別將X和Y初始化為30和40。

We print the values of all objects using the print() function.

我們使用print()函數(shù)打印所有對象的值。

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.

在上面的程序中,我們創(chuàng)建了一個Sample類,其中包含兩個數(shù)據(jù)成員X和Y ,一個默認構(gòu)造函數(shù), set()和print()成員函數(shù)。

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()函數(shù)中,我們創(chuàng)建了大小為2的對象數(shù)組,以及一個以數(shù)組對象的基地址初始化的指針。 然后使用指針調(diào)用set()和print()函數(shù),這樣將正確調(diào)用函數(shù),并打印分配的值。

Recommended posts

推薦的帖子

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

    C ++構(gòu)造函數(shù)和析構(gòu)函數(shù)| 查找輸出程序| 套裝2

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

    C ++構(gòu)造函數(shù)和析構(gòu)函數(shù)| 查找輸出程序| 套裝3

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

    C ++構(gòu)造函數(shù)和析構(gòu)函數(shù)| 查找輸出程序| 套裝4

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

    C ++構(gòu)造函數(shù)和析構(gòu)函數(shù)| 查找輸出程序| 套裝5

  • C++ Default Argument | Find output programs | Set 1

    C ++默認參數(shù)| 查找輸出程序| 套裝1

  • C++ Default Argument | Find output programs | Set 2

    C ++默認參數(shù)| 查找輸出程序| 套裝2

  • C++ Arrays | Find output programs | Set 1

    C ++數(shù)組| 查找輸出程序| 套裝1

  • C++ Arrays | Find output programs | Set 2

    C ++數(shù)組| 查找輸出程序| 套裝2

  • C++ Arrays | Find output programs | Set 3

    C ++數(shù)組| 查找輸出程序| 套裝3

  • C++ Arrays | Find output programs | Set 4

    C ++數(shù)組| 查找輸出程序| 套裝4

  • C++ Arrays | Find output programs | Set 5

    C ++數(shù)組| 查找輸出程序| 套裝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-1.aspx

c構(gòu)造函數(shù)和析構(gòu)函數(shù)

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

總結(jié)

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

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。