-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstructor.cpp
More file actions
317 lines (263 loc) · 5.51 KB
/
Copy pathconstructor.cpp
File metadata and controls
317 lines (263 loc) · 5.51 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
// Constructor Simple
#include<iostream>
using namespace std;
class Complex{
int a, b;
public:
// Creating a constructor
Complex(void); // constructor decleration
void printNumber(){
cout << "Your number is " << a << " + " << b << "i" << endl;
}
};
Complex :: Complex(void){
a = 10;
b = 0;
}
int main(){
Complex c;
c.printNumber();
return 0;
}
* Property of Constructor
- Constructor is a special member function with the same name as of the class
- It is used to initialize the object of its class
- It is declearde in the puclic section of the class
- They are automatically invoked(Called) whenever the object is created
- Do not have return types and they can not return values
- It can have default arguments
- We can not refer to their address
// -----------> Default and Parameterized Constructor
// Default Constructor
# include<iostream>
using namespace std;
class S7T{
int a, b;
public:
S7T(void);
void printNum(){
cout << "The sum of " << a << " + " << b << "i" << endl;
}
};
S7T :: S7T(void){
a = 9;
b = 8;
}
int main(){
S7T K;
K.printNum();
return 0;
}
// Parameterized Constructor
#include <iostream>
using namespace std;
class S7T
{
int a, b;
public:
S7T(int, int);
void printNum()
{
cout << "The sum of " << a << " + " << b << "i" << endl;
}
};
S7T ::S7T(int x, int y)
{
a = x;
b = y;
}
int main()
{
// Implicit call
S7T K(5, 8);
// Explicit call
S7T KS = S7T(5, 8);
K.printNum();
KS.printNum();
return 0;
}
// Point Constructor
#include<iostream>
using namespace std;
class Point{
int x, y;
public:
Point(int a, int b){
x = a;
y = b;
}
void displayPoint() {
cout << "The point is (" << x << "," << y << ")" << endl;
}
};
int main(){
Point p(1, 4);
p.displayPoint();
Point q(5, 9);
q.displayPoint();
return 0;
}
// Constructor Overloading
#include<iostream>
using namespace std;
class S7K{
int a, b;
public:
S7K(int x, int y){
a = x;
b = y;
}
S7K(int x){
a = x;
b = 0;
}
void printNum(){
cout << "The sum of " << a << " + " << b << "i" << endl;
}
};
int main(){
S7K s(4, 7);
s.printNum();
S7K k(7);
k.printNum();
return 0;
}
// Constructor with default arguments
#include<iostream>
using namespace std;
class Simple{
int data1;
int data2;
public:
Simple(int a, int b = 9){
data1 = a;
data2 = b;
}
void printData(){
cout << "The value of "<< data1 <<" and "<< data2 << endl;
cout << "The sum of A + B = " << data1 + data2 << endl;
}
};
int main(){
Simple s(3);
s.printData();
return 0;
}
// Dynamic Initialization of Object Using Constructor
#include<iostream>
using namespace std;
class BankDeposit{
int principle;
int years;
float intrestRate;
float returnValue;
public:
BankDeposit(){}
BankDeposit(int p, int y, float r);
BankDeposit(int p, int y, int r);
void show();
};
BankDeposit :: BankDeposit(int p, int y, float r){
principle = p;
years = y;
intrestRate = r;
returnValue = principle;
for (int i = 0; i < y; i++){
returnValue = returnValue * (1 + intrestRate);
}
}
BankDeposit :: BankDeposit(int p, int y, int r)
{
principle = p;
years = y;
intrestRate = float(r) / 100;
returnValue = principle;
for (int i = 0; i < y; i++)
{
returnValue = returnValue * (1 + intrestRate);
}
}
void BankDeposit :: show(){
cout << "Principle amount was " << principle
<<". Return value after "<<years
<<" years is: "<<returnValue<<endl;
}
int main(){
BankDeposit bd1, bd2, bd3;
int p, y;
float r;
int R;
bd3.show();
cout << "Enter the value of p, y, r: ";
cin >> p >> y >> r;
bd1 = BankDeposit(p, y, r);
bd1.show();
cout << "Enter the value of p, y, R: ";
cin >> p >> y >> R;
bd2 = BankDeposit(p, y, R);
bd2.show();
return 0;
}
// Copy Constructor
// Note: When no copy constructor is found, compiler supplies its own copy constructor
#include<iostream>
using namespace std;
class Number{
int a;
public:
Number(){
a = 0;
}
Number(int num){
a = num;
}
// Copy Constructor
Number(Number & obj){
cout << "Copy Constructor called!!" << endl;
a = obj.a;
}
void display(){
cout << "The number for this object is " << a << endl;
}
};
int main(){
Number x, y, z(57);
x.display();
y.display();
z.display();
Number z1(x);
z1.display();
return 0;
}
// Destructor in C++
*/
#include<iostream>
using namespace std;
// -> Destructor never takes an argument nor does it return any value
int count = 0;
class num{
public:
num()
{
count++;
cout << "This is the time when constructor is called for object number" << count << endl;
}
~num(){
cout << "This is the time when my destructor is called for object number" << count << endl;
count--;
}
};
int main(){
cout << "We are inside our main function" << endl;
cout << "Creating first objects N1" << endl;
num N1;
{
cout << "Entering the block" << endl;
cout << "Creating two or more objects" << endl;
num N2, N3;
cout << "Exiting the block"<<endl;
}
cout << "Back to main" << endl;
return 0;
}