Repository navigation
Expand file tree
/
Copy pathtemplate.cpp
More file actions
711 lines (609 loc) · 16.5 KB
/
Copy pathtemplate.cpp
File metadata and controls
711 lines (609 loc) · 16.5 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
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
#include <iostream>
using namespace std;
// template<typename T>
// T product(T num1, T num2) {
// return num1 * num2;
// }
// int main() {
// cout << product(2, 3) << endl;
// cout << product(1.1f, 2.2f) << endl;
// cout << product(1.1, 2.2) << endl;
// return 0;
// }
// int main() {
// cout << product(2, 3) << endl;
// cout << product(1.1f, 2.2f) << endl;
// // error: no matching function for call to ‘product(int, double)
// // note: deduced conflicting types for parameter ‘T’ (‘int’ and ‘double’)
// // cout << product(3, 4.5) << endl;
// cout << product<int>(3, 4.5) << endl;
// cout << product<float>(3, 4.5) << endl;
// // string s1 = "Hello ", s2 = "world!\n";
// // error: no match for ‘operator*’
// // cout << product(s1, s2) << endl;
// return 0;
// }
// template<typename T>
// T product(T num1, T num2) {
// return num1 * num2;
// }
#include <iostream>
// template<typename T>
// T product(T num1, T num2); // Template function declaration
// int main() {
// std::cout << product(2, 3) << std::endl;
// std::cout << product(1.1f, 2.2f) << std::endl;
// std::cout << product(1.1, 2.2) << std::endl;
// return 0;
// }
// template<typename T>
// T product(T num1, T num2) { // Template function definition
// return num1 * num2;
// }
// template<typename T>
// T product(T num1, T num2) {
// cout << "Template function called\n";
// return num1 * num2;
// }
// template<typename T>
// T product(T num1, T num2, T num3) {
// cout << "template function with three argumets called\n";
// return num1 * num2 * num3;
// }
// float product(float num1, float num2) {
// cout << "Non-template function called\n";
// return num1 * num2;
// }
// int product(int num1, int num2, int num3) {
// cout << "Non-template function with three argumets called\n";
// return num1 * num2 * num3;
// }
// int main() {
// cout << product(2, 3) << endl;
// cout << product(1.1f, 2.2f) << endl;
// cout << product<float>(1.1f, 2.2f) << endl;
// cout << product<>(1.1f, 2.2f) << endl;
// cout << product(1.1, 2.2) << endl;
// // Not an exact match
// cout << product(1, 2, 3) << endl;
// return 0;
// }
// template<typename T>
// T product(T num1, int num2, T num3) {
// cout << "Template function called\n";
// return num1 * num2 * num3;
// }
// int main() {
// cout << product(2.5, 3, 2.5) << endl;
// cout << product(2.5, '3', 2.5) << endl;
// // error: no matching function for call to ‘product(double, double, int)’
// // note: deduced conflicting types for parameter ‘T’ (‘double’ and ‘int’)
// // cout << product(2.5, 3.5, 2) << endl;
// return 0;
// }
// template<typename T2,typename T1 >
// T2 product(T2 num1, T2 num2, T1 num3) {
// cout << "Template function called\n";
// return num1 * num2 * num3;
// }
// int main() {
// // error: no matching function for call to ‘product(double, int, double)’
// // note: deduced conflicting types for parameter ‘T2’ (‘double’ and ‘int’)
// cout << product<int>(2.5, 3, 2.5) << endl;
// cout << product<int,int>(2.5, 3.5, 2.5) << endl;
// cout << product<int,int>(2.5, 3.5, 2) << endl;
// return 0;
// }
// template<typename T1>
// double average(T1 arr[], int n) {
// double result;
// for(int i = 0; i < n; i++) {
// result += arr[i];
// }
// result /= n;
// return result;
// }
// int main() {
// float arr[5] = {1.1, 2.2, 3.3, 4.4, 5.5};
// cout << average(arr, 5);
// return 0;
// }
// template<typename T1, typename T2>
// T2 average(T1 arr[], int n) {
// T2 result;
// for(int i = 0; i < n; i++) {
// result += arr[i];
// }
// result /= n;
// return result;
// }
// int main() {
// float arr[5] = {1.1, 2.2, 3.3, 4.4, 5.5};
// // error: no matching function for call to ‘average(float [5], int)’
// // couldn't deduce template parameter ‘T2’
// cout << average<float,double>(arr, 5);
// return 0;
// }
// template<typename T1, typename T2>
// T1 sum(T1 arg1, T2 arg2) {
// return arg1 + arg2;
// }
// class Complex {
// double re, im;
// public:
// Complex(double re, double im) {
// this->re = re;
// this->im = im;
// }
// Complex operator+(Complex c) {
// return Complex(re + c.re, im + c.im);
// }
// friend std::ostream& operator<<(std::ostream &, Complex);
// };
// std::ostream& operator<<(std::ostream& output, Complex c) {
// output << "(" << c.re << ", " << c.im << ")";
// return output;
// }int main() {
// // Content written in “” is taken as char pointer here is the pointer arithmetic case
// std::cout << sum(1.2, 2) << std::endl;
// std::cout << sum("Hello ", 2) << std::endl;
// // error: no match for ‘operator+’
// // std::cout << sum(string("Hello "), 2) << std::endl;
// std::cout << sum<string,string>(("Hello "), ("world!"))
// << std::endl;
// Complex c1(1.1, 2.2), c2(3.3, 4.4);
// std::cout << sum(c1, c2) << std::endl;
// return 0;
// }
// template<typename T2 = int,typename T1=int>
// T1 sum(T1 arg1, T2 arg2 = 5.2) {
// return arg1 + arg2;
// }
// int main() {
// cout << sum(1.2) << endl;
// cout << sum<double, double>(1.2) << endl;
// cout << sum(1.2, 2.2) << endl;
// return 0;
// }
// class StackOverflowException: public std::exception {
// public:
// virtual const char* what() const throw()
// {
// return "StackOverflowException\n";
// }
// };
// class StackUnderflowException: private std::exception {
// public:
// virtual const char* what() const throw()
// {
// return "StackUnderflowException\n";
// }
// };
// template <typename T>
// class Stack
// {
// T *arr;
// int top;
// int capacity;
// public:
// Stack(int size) {
// arr = new T[size];
// capacity = size;
// top = -1;
// }
// void push(T element) {
// if(top == capacity - 1){
// StackOverflowException so;
// throw so;
// }
// arr[++top] = element;
// }
// T pop() {
// if(top == -1){
// StackUnderflowException su;
// throw su;
// }
// return arr[top--];
// }
// ~Stack(){
// delete [] arr;
// }
// };
// int main()
// {
// Stack<string> str_stack(3);
// try {
// str_stack.push(string("ABC"));
// str_stack.push(string("PQR"));
// str_stack.push(string("XYZ"));
// str_stack.push(string("LMN"));
// cout << str_stack.pop() << endl;
// cout << str_stack.pop() << endl;
// cout << str_stack.pop() << endl;
// }
// catch(std::exception &e) {
// cout << e.what();
// }
// return 0;
// }
// template<typename T1, typename T2>
// T1 sum(T1 arg1, T2 arg2) {
// return arg1 + arg2;
// }
// class Complex {
// double re, im;
// public:
// Complex(double re, double im) {
// this->re = re;
// this->im = im;
// }
// Complex operator+(Complex c) {
// return Complex(re + c.re, im + c.im);
// }
// friend std::ostream& operator<<(std::ostream &, Complex);
// };
// std::ostream& operator<<(std::ostream& output, Complex c) {
// output << "(" << c.re << ", " << c.im << ")";
// return output;
// }int main() {
// // Content written in “” is taken as char pointer here is the pointer arithmetic case
// std::cout << sum(1.2, 2) << std::endl;
// std::cout << sum<string>("Hello ", 2) << std::endl;
// // error: no match for ‘operator+’
// // std::cout << sum(string("Hello "), 2) << std::endl;
// // std::cout << sum(string("Hello "), string("world!"))
// // << std::endl;
// std::cout << sum<string,string>(("Hello "),("world!"));
// // << std::endl;
// Complex c1(1.1, 2.2), c2(3.3, 4.4);
// std::cout << sum(c1, c2) << std::endl;
// return 0;
// }
// template<typename T1, typename T2>
// T1 sum(T1 arg1, int arg2 = 5) {
// return arg1 + arg2;
// }
// int main() {
// // error: no matching function for call to ‘sum(double)’
// // note: couldn't deduce template parameter ‘T2’
// // cout << sum(1) << endl;
// cout << sum<double,int>(1.2) << endl;
// // cout << sum(string("Hello "), string("world!")) << endl;
// cout<<sum<int,double>(4,10.1);
// // could not convert ‘5’ from ‘int’ to ‘std::string’
// // cout << sum<string, string>(string("Hello ")) << endl;
// return 0;
// }
// #include <iostream>
// using namespace std;
// int main() {
// int ar[6] = {22, 5, 23, 43, 54, 65};
// int *p, i;
// p = ar;
// i = *p++ - *p--;
// cout << i;
// return 0;
// }
// #include <iostream>
// using namespace std;
// class B
// {
// float by;
// public:
// B()
// {
// by = 15.5011;
// }
// virtual void show()
// {
// cout << by << endl;
// }
// };
// class D : public B
// {
// float dr;
// public:
// D()
// {
// dr = 10.0;
// }
// void show()
// {
// cout << dr << endl;
// }
// };
// int main()
// {
// B ob, *ptr;
// D obl;
// ptr = &ob;
// cout << sizeof(*ptr) << endl;
// ptr = &obl;
// cout << sizeof(*ptr) << endl;
// return 0;
// }
// #include <iostream>
// class Base {
// char a,b;
// virtual void func() {}
// };
// class Derived : public Base {
// void func() {}
// };
// int main() {
// Base bo;
// Derived derivedObj;
// Base* basePtr;
// basePtr = &bo;
// basePtr = &derivedObj;
// Derived* derivedPtr = &derivedObj;
// std::cout << "Size of base class pointer: " << sizeof(basePtr) << " bytes" << std::endl;
// std::cout << "Size of derived class: " << sizeof(Derived) << " bytes" << std::endl;
// return 0;
// }
// #include<iostream>
// #include<string>
// #include<iomanip>
// using std::cin;
// using std::cout;
// using std::endl;
// using std::string;
// using std::fixed;
// using std::setprecision;
// #define PI 3.14
// /* write your code here */
// class Shape{
// public:
// Shape(){}
// virtual double area()=0;
// virtual double peri()=0;
// };
// class Square:public Shape{
// double x;
// public:
// Square(double m){
// x=m;
// }
// double area(){
// return x*x;
// }
// double peri(){
// return 4*x;
// }
// };
// class Rectangle:public Shape{
// double x,y;
// public:
// Rectangle(double m, double n){
// x=m;
// y=n;
// }
// double area(){
// return x*y;
// }
// double peri(){
// return 2*(x+y);
// }
// };
// class Circle:public Shape{
// double x;
// public:
// Circle(double r){
// x=r;
// }
// double area(){
// return PI*x*x;
// }
// // double peri(){
// // return 2*PI*x;
// // }
// };
// int main() {
// int n;
// cin >> n;
// Shape **sp = new Shape*[n];
// char shape_type;
// double num1, num2;
// for(int i = 0; i < n; i++) {
// getchar();
// cin >> shape_type;
// switch(shape_type) {
// case 'S':
// cin >> num1;
// sp[i] = new Square(num1);
// break;
// case 'R':
// cin >> num1 >> num2;
// sp[i] = new Rectangle(num1, num2);
// break;
// case 'C':
// cin >> num1;
// sp[i] = new Circle(num1);
// break;
// }
// }
// for(int i = 0; i < n; i++) {
// cout << fixed << setprecision(2);
// cout << sp[i]->area() << " ";
// cout << sp[i]->peri() << endl;
// }
// return 0;
// }#include <iostream>
// class MyClass {
// private:
// int data;
// public:
// MyClass(int value) : data(value) {}
// void printData() {
// std::cout << "Data: " << data << std::endl;
// }
// void swapData(MyClass* other) {
// int temp = data;
// data = other->data;
// other->data = temp;
// }
// };
// int main() {
// MyClass obj1(5);
// MyClass obj2(10);
// std::cout << "Before swapping:" << std::endl;
// obj1.printData();
// obj2.printData();
// obj1.swapData(&obj2);
// std::cout << "After swapping:" << std::endl;
// obj1.printData();
// obj2.printData();
// return 0;
// }
// #include <iostream>
// class Sorter {
// public:
// virtual void sort(int arr[], int size) {
// cout<<"base";
// // Base implementation sorts in descending order
// for (int i = 0; i < size - 1; i++) {
// for (int j = 0; j < size - i - 1; j++) {
// if (arr[j] < arr[j + 1]) {
// int temp = arr[j];
// arr[j] = arr[j + 1];
// arr[j + 1] = temp;
// }
// }
// }
// }
// };
// class AscendingSorter : public Sorter {
// public:
// void sort(int arr[], int size) override {
// // Override the sort method to sort in ascending order
// for (int i = 0; i < size - 1; i++) {
// for (int j = 0; j < size - i - 1; j++) {
// if (arr[j] > arr[j + 1]) {
// int temp = arr[j];
// arr[j] = arr[j + 1];
// arr[j + 1] = temp;
// }
// }
// }
// }
// };
// int main() {
// int arr[] = { 5, 2, 8, 1, 9, 3 };
// int size = sizeof(arr) / sizeof(arr[0]);
// Sorter* sorter = new Sorter();
// sorter->sort(arr, size);
// std::cout << "Array sorted in descending order: ";
// for (int i = 0; i < size; i++) {
// std::cout << arr[i] << " ";
// }
// std::cout << std::endl;
// AscendingSorter ascendingSorter;
// Sorter* virtualSorter = &ascendingSorter;
// virtualSorter->sort(arr, size);
// std::cout << "Array sorted in ascending order: ";
// for (int i = 0; i < size; i++) {
// std::cout << arr[i] << " ";
// }
// std::cout << std::endl;
// return 0;
// }
// #include <iostream>
// int factorial(int n) {
// if (n == 0 || n == 1) {
// return 1;
// } else {
// return n * factorial(n - 1);
// }
// }
// int main() {
// int num;
// std::cout << "Enter a number: ";
// std::cin >> num;
// int (*factorialPtr)(int) = &factorial;
// int result = factorialPtr(num);
// std::cout << "Factorial of " << num << " is " << result << std::endl;
// return 0;
// }
// template<class A, B>
// #include<iostream>
// #include<iomanip>
// using std::cin;
// using std::cout;
// using std::endl;
// class Complex {
// float real, imaginary;
// public:
// Complex(float real, float imaginary) {
// this->real = real;
// this->imaginary = imaginary;
// }
// void print() {
// cout << std::fixed << std::setprecision(2);
// cout << real << " " << imaginary << endl;
// }
// Complex operator*(float num) {
// this->real *= num;
// this->imaginary *= num;
// return *this;
// }
// };
// int main() {
// Complex c1(1.1, 2.2);
// // will be evaluated as (c1 * 2) * 3
// // c1 * 2 will return Complex object
// c1 * 3*2;
// c1.print();
// return 0;
// }
// template<typename T=int>
// void product(T num1, int num2) {
// cout <<"t";
// }
// template<typename T1 , typename T2>
// void product(T1 num1, T2 num2) {
// cout << "template function called\n";
// }
// void product(int num1=9, int num2=8) {
// cout << "Non-template function with three argumets called\n";
// ;
// }
// int main() {
// product(2, 3) ;
// product(1, 2.2) ;
// product(1.1, 2) ;
// product() ;
// product(1) ;
// // Not an exact match
// product(1.1, 2.2) ;
// return 0;
// }
#include <iostream>
using namespace std;
class Shape {
protected:
int LineStyle;
int FillColour;
public:
virtual void draw() {
cout << "Shape";
}
};
class Circle : public Shape {
private:
int Radius;
int CentrePoint;
public:
void draw() {
cout << "Circle is drawn";
}
};
int main() {
Shape SomeShape;
Shape* PorShape;
Circle Ring;
SomeShape.draw(); // Output: Shape
Ring.draw(); // Output: Circle is drawn
PorShape = &Ring;
PorShape->draw(); // Output: Circle is drawn
return 0;
}