-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample.cpp
More file actions
406 lines (332 loc) · 6.91 KB
/
sample.cpp
File metadata and controls
406 lines (332 loc) · 6.91 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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#include<iostream>
using namespace std;
// int main(){
// int num;
// cout << "Enter Number :" << endl;
// cin >> num;
// cout << "Entered Number is : " << num;
// return 0;
// }
// int main(){
// int num=10;
// int &ref=num;
// cout<<num<<","<<ref<<",";
// ref=40;
// cout<<num<<","<<ref<<endl;
// }// → 10,10,40,40
//In Below code r is reference to whom?
// int main(){
// int num=123;
// int &ref=num;
// int *p;
// p=#
// int * &q=p;
// int z;
// p=&z;
// int &r=*q;
// }// → r reference to z
// int main(){
// int data=250;
// int &r=data;
// int &z=r;
// cout<<data<<","<<r<<","<<z<<",";
// z=60;
// cout<<data<<","<<r<<","<<z<<endl;
// }// → 250,250,250,60,60,60
// int main(){
// int a[5]={10,20,30,40,50};
// for(int i=0;i<5;i++)
// cout<<a[i]<<",";
// int &r=a[2];
// r=120;
// for(int i=0;i<5;i++)
// cout<<a[i]<<",";
// } // → 10,20,30,40,50,10,20,120,40,50
// void Inc(int x, int &y){
// x++;
// y++;
// }
// int main(){
// int a=10,b=20;
// cout<<a<<","<<b<<",";
// Inc(b,a);
// cout<<a<<","<<b<<endl;
// }// → 10,20,11,20
// void Print(int &x){
// cout<<x<<endl;
// }
// int main(){
// Print(10);
// }// → lvalue error becouse actual arg is constant
// void Print(const int &x){
// cout<<x<<endl;
// }
// int main(){
// Print(20);
// }// → 20
// int &Add(int &a,int &b){
// int c=a+b;
// return c;
// }
// int main(){
// int n1=10,n2=20,n3;
// n3=Add(n1,n2);
// cout<<n3<<endl;
// }// → segmentation fault
// int & Add(int &a,int &b){
// static int c=a+b;
// return c;
// }
// int main(){
// int n1=10,n2=20,n3;
// n3=Add(n1,n2);
// cout<<n3<<endl;
// }// → 30
// int & Add(int &a,int &b){
// static int c=a+b;
// return c;
// }
// int main(){
// int n1=10,n2=20,n3,n4,a=100;
// n3=Add(n1,n2);
// n4=Add(n1,a);
// cout<<n3<<",";
// cout<<n4<<endl;
// }// → 30,30 Initialization happens only once, not every function call.
// void increment(int &ref) {
// ref++;
// }
// int main() {
// int x = 5;
// increment(x);
// cout << x << endl;
// return 0;
// }// → 6
// void print(int &ref) {
// cout << ref << endl;
// }
// int main() {
// print(5);
// return 0;
// }// → error
// int main()
// {
// int *arr = new int[4];
// for(int i=0;i<4;i++)
// cin>>*(arr + i);
// cout << *(arr + 2);
// delete[] arr;
// }
// int & fun( ){
// static int x=10;
// return x;
// }
// int main(){
// fun()=30;
// cout<<fun()<<endl;
// } // → 30
// int main(){
// bool flag1=true;
// cout<<sizeof(flag1)<<endl;
// }// → 1
// int main(){
// int x=10,y=20;
// int &ref=x;
// cout<<ref<<" "<<x<<" ";
// ref=y;
// cout<<ref<<" "<<x<<endl;
// } // → 10 10 20 20
// class ABC{
// int data1,data2;
// public:
// void setData(int a=0,int b=0){
// data1=a;
// data2=b;
// }
// void Print(){
// cout<<data1<<","<<data2<<",";
// }
// };
// int main(){
// ABC obj1,obj2;
// obj1.setData(10,20);
// obj2.setData();
// obj1.Print();
// obj2.Print();
// } // → 10,20,0,0
// class ABC{
// int data1,data2;
// void Inc(){
// ++data1;
// data2++;
// }
// public:
// void setData(int a=0,int b=0){
// data1=a;
// data2=b;
// Inc();
// }
// void Print(){
// cout<<data1<<","<<data2<<",";
// }
// };
// int main(){
// ABC obj1,obj2;
// obj1.setData(10,20);
// obj2.setData();
// obj1.Print();
// obj2.Print();
// } // → 11,21,1,1
// class ABC{
// int data1,data2;
// const int data3;
// static int x;
// public:
// ABC() :data3(5){
// data1=20;
// data2=10;
// x++;
// }
// };
// int ABC::x;
// int main(){
// ABC obj;
// cout<<sizeof(obj)<<endl;
// }//12
// int & fun(){
// static int x=10;
// return x;
// }
// int main(){
// fun()=30;
// cout<<fun()<<endl;
// }//30
// int main(){
// bool flag1=true;
// cout<<sizeof(flag1)<<endl;
// }//1
// int main(){
// int x=10,y=20;
// int &ref=x;
// cout<<ref<<" "<<x<<" ";
// ref=y;
// cout<<ref<<" "<<x<<endl;
// }// 10 10 20 20
// class Test {
// int x;
// public:
// Test(int a) : x(a){}
// friend void swapValues(Test &t1, Test &t2);
// };
// void swapValues(Test &t1, Test &t2) {
// int temp = t1.x;
// t1.x = t2.x;
// t2.x = temp;
// }
// int main() {
// Test a(5), b(10);
// swapValues(a, b);
// cout << "a=" << a.x << " b=" << b.x;
// }//error
// class B;
// class A{
// private:
// int a;
// public:
// A(){
// a = 0;
// }
// friend class B; // Friend Class
// };
// class B{
// private:
// int b;
// public:
// void showA(A& x) {
// std::cout << "A::a=" << x.a;
// }
// };
// int main() {
// A a;
// B b;
// b.showA(a);
// }//A::a=0
// class Num {
// int val;
// public:
// Num(int v) : val(v) {} //Num(int v) {val = v;}
// friend Num operator*(Num n1, Num n2);
// void print(){
// cout << val;
// }
// };
// Num operator*(Num n1, Num n2) {
// return Num(n1.val * n2.val);
// }
// int main() {
// Num a(3), b(4);
// Num c = a * b;
// c.print();
// }//12
// class Test {
// int x;
// public:
// Test(int a) : x(a) {} // Test(int a){x=a;}
// friend void change(Test &t);
// };
// void change(Test &t){
// t.x += 5;
// }
// int main() {
// Test obj(10);
// change(obj);
// cout << "Value: " << obj.x;
// }//error
// namespace ABC{
// int data=10;
// }
// namespace XYZ{
// int data=40;
// }
// int main(){
// int data=100;
// cout<<"data:"<<data<<endl;
// }//data:100
// namespace ABC{
// int x=10;
// void Display(){
// cout<<"ABC Display:"<<endl;
// }
// }
// namespace XYZ{
// float x=2.5;
// void Display(){
// cout<<"XYZ Display:"<<endl;
// }
// }
// using namespace ABC;
// int main(){
// cout<<x<<endl;
// XYZ::Display();
// }//10\n XYZ Display:
// namespace ABC{
// int x=10;
// void Display(){
// cout<<"ABC Display:"<<endl;
// }
// }
// namespace XYZ{
// float x=2.5;
// void Display(){
// cout<<"XYZ Display:"<<endl;
// }
// }
// using namespace ABC;
// int main(){
// cout<<x<<endl;
// XYZ::Display();
// }//10\n XYZ Display:
int main(){int x=10,y=20;int &ref=x;cout<<ref<<" "<<x<<" ";ref=y;cout<<ref<<" "<<x<<endl;}
/*
Inheritance definition
*/