c构造函数和析构函数
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.
在上面的程序中,我们创建了一个带有数据成员X和Y的Sample类,两个构造函数一个是默认构造函数 ,另一个是参数化构造函数 。
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.
默认的构造函数将以0初始化数据成员。 在这里,我们自己分配数据成员,而不是局部参数。 然后,数据成员X和Y将包含垃圾值。
Look to the main() function, here we created two objects S1 and S2.
看一下main()函数,在这里我们创建了两个对象S1和S2 。
S1 initialized with the default constructor so the data members of S1 will be initialized with 0.
S1使用默认构造函数初始化,因此S1的数据成员将使用0初始化。
S2 object will call the parameterized constructor, so data members of S2 will contain garbage values.
S2对象将调用参数化的构造函数,因此S2的数据成员将包含垃圾值。
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调用print()函数时,它将在控制台屏幕上打印“ 0 0” 。 当我们使用S2调用print()函数时,它将在控制台屏幕上打印垃圾值。
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.
在上面的程序中,我们创建了一个带有数据成员X和Y的Sample类,我们创建了两个构造函数,一个是默认构造函数,另一个是参数化构造函数。
The default constructor will initialize the data members by 0, and the parameters constructor will initialize data members by specified values.
默认构造函数将通过0初始化数据成员,而参数构造函数将通过指定的值初始化数据成员。
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()函数。 在这里,我们创建了S1 , S2和S3的 3个对象。 S1由默认构造初始化,而S2和S3由参数化构造函数初始化。 S2和S3以不同的方式用于通过参数化构造函数创建对象,但是两者具有相同的效果。
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()函数打印所有对象的值。
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.
在上面的程序中,我们创建了一个Sample类,其中包含两个数据成员X和Y ,一个默认构造函数, set()和print()成员函数。
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()函数中,我们创建了大小为2的对象数组,以及一个以数组对象的基地址初始化的指针。 然后使用指针调用set()和print()函数,这样将正确调用函数,并打印分配的值。
Recommended posts
推荐的帖子
C++ Constructor and Destructor | Find output programs | Set 2
C ++构造函数和析构函数| 查找输出程序| 套装2
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-1.aspx
c构造函数和析构函数