Repository navigation
Expand file tree
/
Copy pathcpp.cpp
More file actions
703 lines (661 loc) · 13.7 KB
/
Copy pathcpp.cpp
File metadata and controls
703 lines (661 loc) · 13.7 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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
#include <iostream>
using namespace std;
/* int main(){
char str[4]="ram";//in cpp we must include space for null charactore.
cout<<str;
} */
/* int main(){
void*vptr;
char *cptr;
//vptr=cptr; valid in c and cpp
//cptr=vptr; valid in c but not in cpp.. cpp cannot do implisit type casting
//cptr=(char*)vptr; valid in both
} */
/* int main(){
int a[5]={1,2};
int &r=a;//array of reference cannot be created.
cout<<r;
} */
/* int main(){
int a[5]={1,2};
int &r=a[3];//reference of particular elemnt of array.
cout<<r;
} */
/*int main(){
int a[5]={1,2};
int (&r)[5]=a;//must include () here ..operatore presidency ..r is reference to whole array a /
cout<<r[1];
}*/
/* int main(){
int i=10;
int &ir=i; //this is valid
int *irp=&ir;
cout<<i<<ir<<*irp;
} */
/* int main(){
int i=10;
int &ir=i;
int &*irp=&ir;//pointer to reference data type is not allow...
cout<<i<<ir<<*irp;
} */
/* int main(){
int i=10;
int *ip=&i;
int *&irp=ip;//reference to pointer is valid ... in reference not inislize address..
cout<<i<<*ip<<*irp;
} */
/*int main(){
int i=10;
float const &ir=i; //must include const key word here otherwise erro is occupie.
;
cout<<i<<ir;
}*/
/* int main(){
const int i=10;
int const &ir=i; //must include const key word here otherwise erro is occupie.
//if both data types are not same then must add const keyword
cout<<i<<ir;
}*/
/* int main(){
int i=10;
int const &ir=i;//if you not here const here then thats also ok..
;
cout<<i<<ir;
}*///...........................................................................
/* int fun(int &num) {
cout << num << endl;
num++;
return num;
}
int main() {
int i = 10;
int res = fun(i);
int & ret_val = fun(i);
fun(i) += 2;
cout << i << " " << res << " " << ret_val << endl;
cout << &i << " " << &res << " " << &ret_val << endl;
return 0;
}*/
/* const int &fun(int &num) {//must add const
cout << num << endl;
num++;
// returning reference to temporary
return num +1;//temp variable ma +1 . then stack will be deleted and temp also deleted sp not returning anything
} // temp=num;
// return temp;
// then do +1 but its lost;
int main() {
int i = 10;
const int &ret_val = fun(i);//const
cout << i << " " << " " << ret_val << endl;
cout << &i << " " << " " << &ret_val << endl;
return 0;
} */
/*
const int &fun(int &num) {
cout << num << endl;
num++;
// returning reference to temporary, hence invalid code
return num++;
//temp=num;
//num++;
//return temp;
}
int main() {
int i = 10;
const int &ret_val = fun(i);
cout << i << " " << " " << ret_val << endl;
cout << &i << " " << " " << &ret_val << endl;
return 0;
} */
/* const int &fun(int &num) {
cout << num << endl;
num++;
return ++num;//change in value of i not create any temp variable before returning
} //if there is num+=1; then also same output
int main() {
int i = 10;
const int &ret_val = fun(i);// everthing goin good
cout << i << " " << " " << ret_val << endl;
cout << &i << " " << " " << &ret_val << endl;
return 0;
} */
/* int main(){
int i;
for(auto i = 3u; i >0; i--)//when its i>=0 the its become cycle
std::cout << i << " ";
}
*/
/* int main(){
auto f = 3.14;
const int &r = f; //f is in double data type r convert in to temp variabl temp have
//have value 3 we are changing f 4.14 so the output will be 4.14 and 3
f = 4.14;
std::cout << f << " " << r;
} */
/* int main(){
int a[5]={1,2,3,4,5};
int*da=(int*)(malloc(10*sizeof(int)));//must do type casting in cpp
if(da==nullptr)
cout<<"no";
else
for(int i=0;i<10;i++){
cin>>da[i];
}
for(int i=0;i<10;i++){
cout<<da[i];
}
free(da);
} */
#include <iostream>
#include <cstdlib>
using namespace std;
/*
int main() {
int *a = (int*) malloc(5 * sizeof(int)); // allocate memory for 5 integers
if (a == nullptr) {
cout << "Memory allocation failed";
return 1;
}
else {
// initialize the array
for (int i = 0; i < 5; i++) {
a[i] = i + 1;
}
int *va = (int*) realloc(a, 10 * sizeof(int)); // reallocate memory for 10 integers
if (va == nullptr) {
cout << "Memory reallocation failed";
free(a);
return 1;
}
else {
a = va; // update the pointer to the new memory location
// read new values into the array
for (int i = 5; i < 10; i++) {
cin >> a[i];
}
// print the array
for (int i = 0; i < 10; i++) {
cout << a[i] << " ";
}
cout << endl;
}
}
free(a); // release the allocated memory
return 0;
}
*/
/* #include<iostream>
int *fun() {
int *num_ptr = new int(7);
cout << *num_ptr << " " << num_ptr << endl;
return num_ptr;
}
int main() {
int *ptr = fun();
cout << *ptr << " " << ptr << endl;
delete ptr;
//ptr is a dangling pointer from here on
//using ptr will result in undefined behaviour
cout << *ptr << " " << ptr << endl;
return 0;
}*/
/* #include<iostream>
int *fun() {
int *num_ptr = new int(7);
cout << *num_ptr << " " << num_ptr << endl;
return num_ptr;
} //new have one pointer which point to thats allocated location and value if we delete that variable then allocate memory will be delted not that pointer
int main() {
int *ptr = fun();
cout << *ptr << " " << ptr << endl;
delete ptr;
//ptr is a dangling pointer from here on
//using ptr will result in undefined behaviour
cout << *ptr << " " << ptr << endl;
return 0;
} */
/* #include<iostream>
std::ostream & have_fun(std::ostream &output)
{
output << "Have Fun";
return output;
}
int main()
{
std::cout << "Hello there,";
std::cout << have_fun << ", you guys!!" << std::endl;
(have_fun(std::cout) << ", you guys!!");
return 0;
} */
/* #include<iostream>
using namespace std;
namespace nspace
{
int x = 11;
void fun()
{
cout << "Hello!" << endl;
}
}
int main()
{
//fun();
nspace::fun();
//cout << x << endl;
cout << nspace::x << endl;
return 0;
}*/
/* using namespace std;
namespace nspace
{
int x = 11;
void fun()
{
cout << "Hello!" << endl;
}
}
using nspace::fun; //using declaration
int main()
{
fun();
nspace::fun();
//cout << x << endl;
cout << nspace::x << endl;
return 0;
}*/
/* #include<iostream>
using namespace std;
namespace nspace
{
int x = 11;
void fun()
{
cout << "Hello!" << endl;
}
}
using namespace nspace; //using directive
int main()
{
fun();
nspace::fun();
cout << x << endl;
cout << nspace::x << endl;
return 0;
} */
/* #include<iostream>
using namespace std;
int x = 10;
namespace nspace
{
void fun()
{
int x = 30;
cout << x << " " << nspace::x << " " << ::x << endl;
}
}
using namespace nspace;
int main()
{
nspace::fun();
cout<<x;
return 0;
} */
/* #include<iostream>
int x = 1;
namespace nspace
{
int x = 2;
int y = 3;
}
int main()
{
int y = 4;
using nspace::x;
std::cout << x << " " << y << " ";
int fun();
fun();
return 0;
}
int fun()
{
std::cout << x;
} */
/* #include<iostream>
namespace test {
int x = 7;
}
namespace test2 {
using test::x;
}
int main() {
std::cout << test2::x;
return 0;
} */
/*
#include<iostream>
int x = 1;
namespace nspace
{
int x = 2;
int y = 3;
int z = 4;
}
int main()
{
int y = 5;
using namespace nspace;
//std::cout << " " << x;
std::cout << " " << ::x;
//std::cout << " " << y;
//std::cout << " " << ::y;
std::cout << " " << z;
//std::cout << " " << ::z;
void fun();
fun();
return 0;
}
void fun()
{
std::cout << " " << x;
}
*/
/* #include<iostream>
int x = 1;
namespace nspace
{
int x = 2;
int y = 3;
int z = 4;
}
using namespace nspace;
int main()
{
int y = 5;
//std::cout << " " << x;
std::cout << " " << ::x;
std::cout << " " << y;
std::cout << " " << ::y;
std::cout << " " << z;
std::cout << " " << ::z;
void fun();
fun();
return 0;
}
void fun()
{
//std::cout << " " << x;
} */
// #include<iostream>
// int x = 1;
// namespace test {
// namespace test2 {
// int x = 2;
// }
// namespace test3 {
// using namespace test2;
// void fun() {
// std::cout << x;
// //std::cout << test::x;
// std::cout << " " << ::x;
// }
// }
// }
// int main() {
// test::test3::fun();
// return 0;
// }
// int main(){
// int /*i*/,j;
// cout<<"i+j="i;
// cin>>i;>>j;
// cout<<"name";
// int x=0,y=1;
// // cout<<y/x;
// cout<<"*\n**\n**\n****";
// char c=;
// int c1=12;
// cout<<"*"<<int(c)<<"*"<<int(c);
// cout<<"*"<<c1<<"*"<<c1;
// bool b=-2.3;
// cout<<b;
// int m;
// int &n=m;
// cout<<m<<n;
// int m=2;
// int &n=m;
// int x=9;
// int &x=m;
// }
// #include <iostream>
// using namespace std;
// // A linked list node
// struct Node {
// int data;
// struct Node* next;
// };
// /* Given a reference to pointer to the head of
// a list, insert a new value x at head */
// void push(struct Node *&head, int x)
// {
// struct Node* new_node = new Node;
// new_node->data = x;
// new_node->next = head;
// head = new_node;
// }
// // This function prints contents of linked
// // list starting from head
// void printList(struct Node* node)
// {
// while (node != NULL) {
// cout << node->data << " ";
// node = node->next;
// }
// }
// /* Driver program to test above functions*/
// int main()
// {
// /* Start with the empty list */
// struct Node* head = NULL;
// push(head, 1);
// push(head, 2);
// push(head, 3);
// printList(head);
// return 0;
// }
// Output:
// void *allocate(int size) {
// void *ptr = malloc(size); // lifetime starts
// if(NULL == ptr) {
// cout << "Memory allocation failed" << endl;
// exit(-1);
// }
// return ptr;
// free(dyn_arr);
// }
// int main() {
// int *dyn_arr = (int *)allocate(10 * sizeof(int));
// for(int i = 0; i < 10; i++)
// cin >> dyn_arr[i];
// for(int i = 0; i < 10; i++)
// cout << dyn_arr[i] << " ";
// cout << endl;
// //lifetime ends
// return 0;
// }
// #include<iostream>
// using namespace std;
// int *fun() {
// int *num_ptr = new int(7);
// cout << *num_ptr << " " << num_ptr << endl;
// return num_ptr;
// }
// int main() {
// int *ptr = fun();
// cout << *ptr << " " << ptr << endl;
// delete ptr;
// //ptr is a dangling pointer from here on
// //using ptr will result in undefined behaviour
// cout << *ptr << " " << ptr << endl;
// return 0;
// }
// #include<iostream>
// using namespace std;
// int &fun() {
// int &num_ref = *(new int(7));
// cout << num_ref << " " << &num_ref << endl;
// return num_ref;
// }
// int main() {
// int &ref = fun();
// cout << ref << " " << &ref << endl;
// delete &ref;
// //ref is a dangling reference from here on
// //using ref will result in undefined behaviour
// cout << ref << " " << &ref << endl;
// return 0;
// }
// int main(){
// }
// #include<iostream>
// using namespace std;
// namespace nspace
// {
// int x=20;
// void fun()
// {
// int x = 30;
// cout << x << " " << " " << ::x << endl;
// }
// }
// int main()
// {
// nspace::fun();
// return 0;
// }
// #include<iostream>
// int x = 1;
// namespace nspace
// {
// int x = 2;
// int y = 3;
// int z = 4;
// }
// int main()
// {
// int y = 5;
// using namespace nspace;
// //error: reference to ‘x’ is ambiguous
// //std::cout << " " << x;
// std::cout << " " << ::x;
// std::cout << " " << y;
// //error: ‘::y’ has not been declared
// //std::cout << " " << ::y;
// std::cout << " " << z;
// //error: ‘::z’ has not been declared
// //std::cout << " " << ::z;
// void fun();
// fun();
// return 0;
// }
// void fun()
// {
// std::cout << " " << x;
// }
// using namespace nspace;
// int main()
// {
// int y = 5;
// //error: reference to ‘x’ is ambiguous
// //std::cout << " " << x;
// std::cout << " " << ::x;
// std::cout << " " << y;
// std::cout << " " << ::y;
// std::cout << " " << z;
// std::cout << " " << ::z;
// void fun();
// fun();
// return 0;
// }
// void fun()
// {
// //error: reference to ‘x’ is ambiguous
// //std::cout << " " << x;
// }
// #include<iostream>
// int x = 1;
// namespace test {
// namespace test2 {
// }
// namespace test3 {
// using namespace test2;
// void fun() {
// std::cout << x;
// //error: ‘x’ is not a member of ‘test’
// //std::cout << test::x;
// std::cout << " " << ::x;
// }
// }
// }
// int main() {
// test::test3::fun();
// return 0;
// }
// #include<iostream>
// // int sum(int n1, int n2, int n3, int n4);
// int sum(int n1, int n2, int n3 = 111, int n4 = 222);
// int main()
// {
// std::cout << "main" << std::endl;
// std::cout << sum(1, 2, 3, 4) << std::endl;
// std::cout << sum(1, 2, 3) << std::endl; // Will get default values from local declaration
// std::cout << sum(1, 2) << std::endl; // Will get default values from local declaration
// void fun(); fun();
// return 0;
// }
// int sum(int n1, int n2, int n3 , int n4 )
// {
// return n1 + n2 + n3 + n4;
// }
// void fun()
// {
// std::cout << "fun" << std::endl;
// std::cout << sum(1, 2, 3, 4) << std::endl;
// std::cout << sum(1, 2, 3) << std::endl; // Default values from definition
// std::cout << sum(1, 2) << std::endl; // Default values from definition
// }
// void fun(float f, int i) {
// cout << "fun1" << endl;
// }
// void fun(int i, int j) {
// cout << "fun2" << endl;
// }
// void fun(int i, float f) {
// cout << "fun3" << endl;
// }
// int main() {
// fun(true, true);
// return 0;
// }
// int main(){
// int a;
// int sqr(int &);
// int n[]={1,2,3,4,5};
// sqr(a);
// //int *p=new;
// //n[1]==[1]n?cout<<"edw":cout<<"hncd";
// }
// int sqr(int &a){
// cout<<"nc";
// }
int main(){
// char c;
// for(c=0;c<256;c++){
// cout<<c;
// }
float a=1.2;
if(a==1.2)
cout<<"fchjk";
else
cout<<"vj";
}