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 20Explanation:
說明:
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:
輸出:
10Explanation:
說明:
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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: stl max函数_C ++ STL中带
- 下一篇: 在C ++中检查一个数组是否是另一个数组