c构造函数和析构函数_C ++构造函数和析构函数| 查找输出程序| 套装2
c構(gòu)造函數(shù)和析構(gòu)函數(shù)
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.
上面的代碼將產(chǎn)生語法錯(cuò)誤,因?yàn)樵趍ain()函數(shù)中,我們創(chuàng)建了必須由默認(rèn)構(gòu)造函數(shù)實(shí)例化的對(duì)象數(shù)組 ,但是在類中,未定義默認(rèn)構(gòu)造函數(shù)。
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 40Explanation:
說明:
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.?
在上面的程序中,我們創(chuàng)建了一個(gè)Sample類,其中包含兩個(gè)數(shù)據(jù)成員 X和Y ,一個(gè)參數(shù)化的構(gòu)造函數(shù) , set()和print()成員函數(shù)。
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.
現(xiàn)在進(jìn)入main()函數(shù),我們創(chuàng)建了一個(gè)對(duì)象數(shù)組,該數(shù)組將由參數(shù)化構(gòu)造函數(shù)實(shí)例化。 并創(chuàng)建了一個(gè)使用數(shù)組對(duì)象的基地址初始化的指針。 然后使用指針調(diào)用set()和print()函數(shù)。
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++.
上面的程序?qū)a(chǎn)生錯(cuò)誤,因?yàn)槲覀儾荒茉贑 ++中將構(gòu)造函數(shù)創(chuàng)建為const成員函數(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 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 ++默認(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 ++類和對(duì)象| 查找輸出程序| 套裝1
C++ Class and Objects | Find output programs | Set 2
C ++類和對(duì)象| 查找輸出程序| 套裝2
C++ Class and Objects | Find output programs | Set 3
C ++類和對(duì)象| 查找輸出程序| 套裝3
C++ Class and Objects | Find output programs | Set 4
C ++類和對(duì)象| 查找輸出程序| 套裝4
C++ Class and Objects | Find output programs | Set 5
C ++類和對(duì)象| 查找輸出程序| 套裝5
翻譯自: https://www.includehelp.com/cpp-tutorial/constructor-and-destructor-find-output-programs-set-2.aspx
c構(gòu)造函數(shù)和析構(gòu)函數(shù)
總結(jié)
以上是生活随笔為你收集整理的c构造函数和析构函数_C ++构造函数和析构函数| 查找输出程序| 套装2的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java ByteArrayInputS
- 下一篇: ipv6路由协议配置_IPV6寻址,标头