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-qualified
Sample() 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構造函數和析構函數