c构造函数和析构函数_C ++构造函数和析构函数| 查找输出程序| 套装3
c構(gòu)造函數(shù)和析構(gòu)函數(shù)
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 20Explanation:
說明:
In the above program, we created a class Sample with data member X, default constructor, set() and print() member functions.
在上面的程序中,我們創(chuàng)建了帶有數(shù)據(jù)成員 X , 默認(rèn)構(gòu)造函數(shù) , set()和print()成員函數(shù)的類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.
現(xiàn)在轉(zhuǎn)到main()函數(shù) ,這里我們創(chuàng)建了一個對象數(shù)組,這些對象默認(rèn)由構(gòu)造函數(shù)實(shí)例化。 然后使用對象數(shù)組調(diào)用set()和print()函數(shù),然后它將在輸出上方進(jìn)行打印。
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 (->).
上面的程序?qū)a(chǎn)生錯誤,因為S是指針,并且我們使用Dot(。)運(yùn)算符而不是引用運(yùn)算符(->)來調(diào)用set()和print()成員函數(shù)。
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:
輸出:
10Explanation:
說明:
In the above program, we created a class Sample with data member X, default constructor, set() and print() member functions.
在上面的程序中,我們創(chuàng)建了帶有數(shù)據(jù)成員X ,默認(rèn)構(gòu)造函數(shù), set()和print()成員函數(shù)的類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()函數(shù),這里我們聲明了Sample類的指針,并通過malloc()函數(shù)實(shí)例化,然后使用該指針調(diào)用set()和print()函數(shù)。 但是,當(dāng)我們使用malloc()函數(shù)時 ,它將不會調(diào)用構(gòu)造函數(shù)。
Recommended posts
推薦的帖子
C++ Constructor and Destructor | Find output programs | Set 1
C ++構(gòu)造函數(shù)和析構(gòu)函數(shù)| 查找輸出程序| 套裝1
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 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 ++默認(rèn)參數(shù)| 查找輸出程序| 套裝1
C++ Default Argument | Find output programs | Set 2
C ++默認(rèn)參數(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-3.aspx
c構(gòu)造函數(shù)和析構(gòu)函數(shù)
總結(jié)
以上是生活随笔為你收集整理的c构造函数和析构函数_C ++构造函数和析构函数| 查找输出程序| 套装3的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: stl max函数_C ++ STL中带
- 下一篇: 在C ++中检查一个数组是否是另一个数组