Default Constructor or No argument program in C++.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
| /*C++ program to demonstrate example of Default Constructor or No argument.*/#include <iostream>using namespace std;//Class declaration.class Demo{ //Private block to declare data member( X,Y ) of integer type. private: int X; int Y; //Public block of member function to access data members. public: //Declaration of default or no argument constructor to initialize data members. Demo (); //To take input from user. void Input(); //To display output onn screen. void Display(); };//End of class//Definition of constructor.Demo:: Demo(){ X = 0; Y = 0;}//Definition of Input() member function.void Demo:: Input(){ cout << "Enter Value of X: "; cin >> X; cout << "Enter Value of Y: "; cin >> Y;}//Definition of Display() member function.void Demo:: Display(){ cout << endl << "X: " << X; cout << endl << "Y: " << Y << endl;}int main(){ Demo d ; //Ctor autometically call when object is created. //Display value of data member. cout << endl <<"Method 1: " << endl; cout << "Value after initialization : " ; d.Display(); d.Input(); cout << "Value after User Input : "; d.Display(); //We can also create object like this Demo d1 = Demo(); //Display value of data member. cout << endl << "Method 2: " << endl; cout << "Value after initialization : "; d1.Display(); return 0;} |
Method 1:
Value after initialization :
X: 0
Y: 0
Enter Value of X: 23
Enter Value of Y: 24
Value after User Input :
X: 23
Y: 24
Method 2:
Value after initialization :
X: 0
Y: 0
Destructors program in C++.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
| /*C++ program to demonstrate example of destructors.*/#include <iostream>using namespace std;//Class Declarationclass Demo{ private: //Private Data member section int X,Y; public://Public Member function section //Default or no argument constructor. Demo() { X = 0; Y = 0; cout << endl << "Constructor Called"; } //Destructor called when object is destroyed ~Demo() { cout << endl << "Destructor Called" << endl; } //To take user input from console void getValues() { cout << endl <<"Enter Value of X : "; cin >> X; cout << "Enter Value of Y : "; cin >> Y; } //To print output on console void putValues() { cout << endl << "Value of X : " << X; cout << endl << "Value of Y : " << Y << endl; }};//main function : entry point of programint main(){ Demo d1; d1.getValues(); cout << endl <<"D1 Value Are : "; d1.putValues(); Demo d2; d2.getValues(); cout << endl <<"D2 Value Are : "; d2.putValues(); //cout << endl ; return 0;} |
Constructor Called
Enter Value of X : 10
Enter Value of Y : 20
D1 Value Are :
Value of X : 10
Value of Y : 20
Constructor Called
Enter Value of X : 30
Enter Value of Y : 40
D2 Value Are :
Value of X : 30
Value of Y : 40
Destructor Called
Destructor Called