-
Notifications
You must be signed in to change notification settings - Fork 0
/
oops.cpp
1015 lines (952 loc) · 19.9 KB
/
oops.cpp
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
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include<bits/stdc++.h> #include<bits/stdc++.h>
using namespace std;
///OOPS
///better to create in different files for main and class
/*class Student
{
int age;
public:
int rollnumber;
char name[50];
void display()
{
cout<<age<<" "<<rollnumber;
}
int getAge()
{
return age;
}
void setAge(int a,int password)
{
if(password!=123)
{
return;
}
age=a;
}
};
int main()
{
//create object statically
Student s1;
Student s2,s3,s4;
//create object dynamically
Student *s5=new Student;
//s1.age=24;
s1.setAge(24);
s1.rollnumber=1929174;
s1.display();
cout<<"Enter Name:"<<endl;
cin.getline(s1.name,50);
cout<<"Name:"<<s1.name<<endl;
cout<<"Age:"<<s1.getAge()<<endl;
cout<<"Roll number:"<<s1.rollnumber<<endl;
//(*s5).age=20;
(*s5).rollnumber=1382923;
//s5->age=20;
s5->setAge(32);
s5->rollnumber=1382923;
s5->display();
cin.getline(s5->name,50);
}*/
/*class Student
{
int age;
public:
int Roll;
///default user created constructor
Student()
{
cout<<"Default Constructor called"<<endl;
}
///parameterized user created constructor
Student(int r)
{
Roll=r;
cout<<"parameterized constructor called"<<endl;
}
Student(int Roll)
{
this->Roll=Roll; //here it is mandatory because we have to diffrentiate between
//parameter roll number and property roll number
cout<<"parameterized constructor called"<<endl;
}
Student(int r,int a)
{
this->Roll=r; //optional
this->age=a; //optional
cout<<"Parameterized constructor 2 called"<<endl;
cout<<"This:"<<this<<endl;
}
void display()
{
cout<<age<<" "<<Roll<<endl;
}
int getAge()
{
return age;
}
void setAge(int a)
{
age=a;
}
~Student()
{
cout<<"Destructor called"<<endl;
}
};*/
/*int main()
{
Student S(1929174);
S.display();
/*Student S1(10,100);
cout<<"Address of S1:"<<&S1<<endl;
Student s1;
s1.display();
Student s2;
s2.display();
Student *s3=new Student;
}*/
/*class Student
{
int age;
public:
int Roll;
///default user created constructor
Student()
{
cout<<"Default Constructor called"<<endl;
}
///parameterized user created constructor
Student(int r)
{
Roll=r;
cout<<"parameterized constructor called"<<endl;
}
Student(int Roll)
{
this->Roll=Roll; //here it is mandatory because we have to diffrentiate between
//parameter roll number and property roll number
cout<<"parameterized constructor called"<<endl;
}
Student(int r,int a)
{
this->Roll=r; //optional
this->age=a; //optional
cout<<"Parameterized constructor 2 called"<<endl;
cout<<"This:"<<this<<endl;
}
void display()
{
cout<<age<<" "<<Roll<<endl;
}
int getAge()
{
return age;
}
void setAge(int a)
{
age=a;
}
~Student()
{
cout<<"Destructor called"<<endl;
}
};*/
/*int main()
{
Student S(1929174);
S.display();
/*Student S1(10,100);
cout<<"Address of S1:"<<&S1<<endl;
Student s1;
s1.display();
Student s2;
s2.display();
Student *s3=new Student;
s3->display();
Student s4(1929100);
s4.display();
Student *s5=new Student(11278172,43);
s5->display();
return 0;
}*/
///copy constructor
/*int main()
{
Student s1(10,100); //parameterized cons
cout<<"S1:";
s1.display();
Student *s2=new Student(s1); //copy cons inbuilt
cout<<"S2:";
s2->display();
Student s3(*s2);
cout<<"S3:";
s3.display();
return 0;
}*/
///copy assignment operator
/*int main()
{
Student s1(10,100);
cout<<"s1:";
s1.display();
Student s2(20,200);
s2=s1;
cout<<"s2:";
s2.display();
Student *s3=new Student(90,230);
*s3=s2;
cout<<"s3:";
s3->display();
delete s3;
return 0;
}*/
///FRACTION CLASS
/*class Fraction
{
int numerator;
int denominator;
public:
Fraction()
{
cout<<"Default constructor"<<endl;
}
Fraction(int numerator,int denominator)
{
this->numerator=numerator; //mandatory
this->denominator=denominator;
}
void PrintFraction()
{
cout<<"Fraction is:"<<endl;
cout<<this->numerator<<"/"<<this->denominator<<endl;
}
void add(Fraction const &f2) //to prevent extra space and time complexity we pass
//by reference
{
int lcm = denominator*f2.denominator; //optional this'
int x = lcm/denominator;
int y = lcm/f2.denominator;
int num=x*numerator + y*f2.numerator;
this->numerator=num;
this->denominator=lcm;
simplify();
}
void simplify()
{
//inbuilt fuction for gcd:__gcd(a,b);
int gcd=1;
int j=min(this->numerator,this->denominator);
for(int i=1;i<=j;i++)
{
if(numerator%i==0 && denominator%i==0)
{
gcd=i;
}
}
numerator/=gcd;
denominator/=gcd;
display();
}
void display()
{
cout<<"Addition of fraction:"<<endl;
cout<<numerator<<"/"<<denominator<<endl;
}
};
int main()
{
Fraction f0;
Fraction f1(10,2);
Fraction f2(20,4);
f1.PrintFraction();
f2.PrintFraction();
f1.add(f2);
Fraction *f3=new Fraction(f2);
Fraction *f4=new Fraction(34,12);
f3->PrintFraction();
f4->PrintFraction();
f3->add(*f4);
return 0;
}*/
///COMPLEX NUMBER CLASS
/*class Complex
{
int real;
int imaginary;
public:
Complex(int real,int imaginary)
{
this->real=real;
this->imaginary=imaginary;
}
void print()
{
cout<<"Complex Number:"<<endl;
cout<<real<<"+"<<imaginary<<"i"<<endl;
}
void Addedprint()
{
cout<<"Added Complex Number:"<<endl;
cout<<real<<"+"<<imaginary<<"i"<<endl;
}
Complex operator+(Complex const &c2)
{
int realadd=real+c2.real;
int imaginaryadd=imaginary+c2.imaginary;
Complex cnew(realadd,imaginaryadd);
return cnew;
//cnew.Addedprint();
}
Complex add(Complex const &c2)
{
int realadd=real+c2.real;
int imaginaryadd=imaginary+c2.imaginary;
}
};
int main()
{
Complex c1(3,4);
Complex c2(8,5);
c1.print();
c2.print();
//c1.add(c2);
Complex c4=c1+c2;
c4.Addedprint();
return 0;
}
///Shallow and deep copies and our own copy constructor creation
/*class student
{
int age;
public:
char *name;
//own copy constructor creation
student(student const &s) //inbuilt copy constructor code
{
this->age=s.age;
//this->name=s.name; //shallow copy
this->name=new char[strlen(s.name)+1]; //deep copy
strcpy(this->name,s.name);
}
student(int age,char *name)
{
this->age=age;
//this->name=name; shallow copy(avoid this)
///deep copy(create new and copy the previous array
this->name=new char[strlen(name)+1];
strcpy(this->name,name);
}
void display()
{
cout<<name<<" "<<age<<endl;
}
};
int main()
{
char a[]="abcd";
student s1(20,a);
s1.display();
///deep copy demo
/a[3]='e';
student s2(32,a);
s2.display();
s1.display();
///our own copy constructor demo
student s2(s1) ;
s2.name[0]='z';
s2.display();
s1.display();
}*/
///Initialization List
/*class student
{
public:
int age;
const int roll;
int &x;
student(int roll,int age):roll(roll),age(age),x(this->age)
{
//this->roll=roll; //wrong as this has already been allocated by the garbage value
}
void display()
{
cout<<age<<" "<<roll<<" "<<x;
}
};
int main()
{
student s1(3223,45);
s1.display();
}*/
///constant function
/*class Fraction
{
int numerator;
int denominator;
public :
Fraction()
{
}
Fraction(int numerator, int denominator)
{
this -> numerator = numerator;
this -> denominator = denominator;
}
int getNumerator() const
{
return numerator;
}
int getDenominator() const
{
return denominator;
}
void setNumerator(int n)
{
numerator = n;
}
void setDenominator(int d) {
denominator = d;
}
void print()
{
cout << this -> numerator << " / " << denominator << endl;
}
void simplify()
{
int gcd = 1;
int j = min(this -> numerator, this -> denominator);
for(int i = 1; i <= j; i++)
{
if(this -> numerator % i == 0 && this -> denominator % i == 0)
{
gcd = i;
}
}
this -> numerator = this -> numerator / gcd; //numerator/=gcd
this -> denominator = this -> denominator / gcd;
}
Fraction add(Fraction const &f2) const
{
int lcm = denominator * f2.denominator;
int x = lcm / denominator;
int y = lcm / f2.denominator;
int num = x * numerator + (y * f2.numerator);
//numerator = num;
//denominator = lcm;
Fraction f3(num,lcm); ///para cons
f3.simplify();
return f3;
}
///operator overloading
Fraction operator+(Fraction const &f2) const
{
int lcm = denominator * f2.denominator;
int x = lcm / denominator;
int y = lcm / f2.denominator;
int num = x * numerator + (y * f2.numerator);
//numerator = num;
//denominator = lcm;
Fraction f3(num,lcm); ///para cons
f3.simplify();
return f3;
}
Fraction multiply(Fraction const &f2)
{
this->numerator = numerator * f2.numerator;
this->denominator = denominator * f2.denominator;
//Fraction fnew(numerator,denominator);
simplify();
return *this;
}
Fraction operator*(Fraction const &f2) const
{
int num = numerator * f2.numerator;
int den = denominator * f2.denominator;
Fraction fnew(num,den);
fnew.simplify();
return fnew;
}
bool operator==(Fraction const &f2) const
{
return(this->numerator==f2.numerator && this->denominator==f2.denominator);
}
//pre-increment
Fraction& operator++()
{
numerator+=denominator;
simplify();
return *this;
}
//post-increment
Fraction operator++(int)
{
Fraction fnew(this->numerator,this->denominator);
fnew.simplify();
this->numerator+=this->denominator;
simplify();
return fnew;
}
//+= overloading
Fraction& operator+=(Fraction const &f2)
{
int lcm = denominator * f2.denominator;
int x = lcm / denominator;
int y = lcm / f2.denominator;
int num = x * numerator + (y * f2.numerator);
numerator = num;
denominator = lcm;
simplify();
return *this;
}
};
int main()
{
Fraction f1(10,2);
Fraction f2(15,4);
Fraction f11=f1++;
Fraction f3=f1.add(f2);
Fraction f4=f3+f2;
Fraction f5=f1.multiply(f2);
Fraction f6=f5*f1;
Fraction f7(1,5);
Fraction f8(1,5);
Fraction f9=++f8;
Fraction f10=++(++f1);
f1.print();
f2.print();
f3.print();
f4.print();
cout<<"Multiplication:"<<endl;
f5.print();
cout<<"Multiplication using operator overloading:"<<endl;
f6.print();
cout<<"Equal or not?"<<endl;
f7.print();
f8.print();
if(f7==f8)
{
cout<<"Equal"<<endl;
}
else cout<<"Not Equal"<<endl;
//cout<<"Increment:"<<endl;
f9.print();
f1.print();
++(++f1);
f1.print();
f10.print();
cout<<"Post increment:"<<endl;
f11.print();
f1.print();
cout<<"+=:"<<endl;
f1+=f2;
f1.print();
}*/
///Dynamic Array(Vector implementation)
/*class DynamicArray
{
int *data;
int capacity;
int NextIndex;
public:
DynamicArray()
{
data=new int[5];
capacity=5;
NextIndex=0;
}
DynamicArray(DynamicArray const &d2)
{
this->NextIndex=d2.NextIndex;
this->capacity=d2.capacity;
this->data=new int[d2.capacity];
for(int i=0;i<d2.NextIndex;i++)
{
this->data[i]=d2.data[i];
}
}
int getCapacity() const
{
cout<<"Length:"<<endl;
cout<<capacity<<endl;
}
void print() const
{
for(int i=0;i<NextIndex;i++)
{
cout<<data[i]<<" ";;
}
cout<<endl;
}
void add(int element)
{
data[NextIndex]=element;
NextIndex++;
if(NextIndex==capacity)
{
int *NewData=new int[capacity*2];
for(int i=0;i<capacity;i++)
{
NewData[i]=data[i];
}
delete []data;
data=NewData;
capacity*=2;
}
data[NextIndex]=element;
NextIndex++;
}
int getelement(int i) const
{
if(i>=0 && i<NextIndex)
{
cout<<data[i]<<endl;;
}
else
{
cout<<"Invalid Index"<<endl; //return -1
}
}
void addelement(int i,int element)
{
if(i>=0 && i<NextIndex)
{
data[i]=element;
}
else if(i==NextIndex)
{
add(element);
}
else
{
cout<<"Invalid Index!"<<endl;
}
}
DynamicArray operator=(DynamicArray const &d2)
{
this->NextIndex=d2.NextIndex;
this->capacity=d2.capacity;
this->data=new int[d2.capacity];
for(int i=0;i<d2.NextIndex;i++)
{
this->data[i]=d2.data[i];
}
}
};
int main()
{
DynamicArray d1,d3;
d1.add(3);
d1.add(10);
d1.add(5);
d1.add(6);
d1.add(7);
d1.print();
DynamicArray d2(d1);
d3=d1;
d2.addelement(0,8);
d1.print();
d2.print();
d3.addelement(0,4);
d3.print();
d1.print();
cout<<"Search for element at index"<<endl;
d1.getelement(0);
cout<<"Insert element at desired index:"<<endl;
d1.addelement(0,5);
d1.getCapacity();
}*/
///Dynamic Memory Allocation
/*int main()
{
cout<<"Enter number of elements:"<<endl;
int n;
cin>>n;
int *ar=new int[n];
for(int i=0;i<n;i++)
{
cin>>ar[i];
}
delete []ar;
for(int i=0;i<n;i++)
{
cout<<ar[i]<< " ";
}
}*/
///Dynamic Memory Allocation(2D array)
/*int main()
{
int m,n;
cin>>m>>n;
int** p=new int*[m];
for(int i=0;i<m;i++)
{
p[i]=new int[n];
for(int j=0;j<n;j++)
{
cin>>p[i][j];
}
}
//p[1][2]=99;
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
cout<<p[i][j]<<" ";
}
cout<<endl;
}
for(int i=0;i<m;i++) //remember to delete
{
delete []p[i];
}
delete []p;
return 0;
}*/
/*int main()
{
int ar[]={1,2,3,4,5,6};
int s=sizeof(ar)/sizeof(ar[0]);
cout<<s<<endl;
}*/
///Swastika(pattern)
/*void Swastika(int n)
{
int k=((n+1)/2);
for(int i=1;i<=((n+1)/2)-1;i++)
{
cout<<"*";
for(int i=1;i<=((n+1)/2)-2;i++)
{
cout<<" ";
}
for(int j=1;j<=k;j++)
{
cout<<"*";
}
cout<<endl;
k=1;
}
for(int i=1;i<=n;i++)
{
cout<<"*";
}
cout<<endl;
for(int i=1;i<=((n+1)/2)-2;i++)
{
for(int i=1;i<=((n+1)/2)-1;i++)
{
cout<<" ";
}
for(int i=1;i<=1;i++)
{
cout<<"*";
}
for(int i=1;i<=((n+1)/2)-2;i++)
{
cout<<" ";
}
for(int i=1;i<=1;i++)
{
cout<<"*";
}
cout<<endl;
}
for(int k=0;k<=((n+1)/2)-1;k++)
{
cout<<"*";
}
for(int i=1;i<=((n+1)/2)-2;i++)
{
cout<<" ";
}
for(int i=1;i<=1;i++)
{
cout<<"*";
}
}
int main()
{
Swastika(7);
}*/
///INHERITANCE
/*class Vehicle {
private :
int maxSpeed;
protected :
int numTyres;
public :
string color;
//Vehicle(){
// cout<<"Vehicle default constructor"<<endl;
//}
Vehicle(int z){
cout<<"Vehicle parameterised constructor"<<endl;
maxSpeed = z;
}
~Vehicle(){
cout<<"Vehicle destructor"<<endl;
}
};
class Car : public Vehicle{
public:
int numGears;
Car(int x,int y): Vehicle(x) {
numGears = y;
cout<<"Car parameterised construtor"<<endl;
}
~Car(){
cout<<"Car destructor"<<endl;
}
void print(){
cout<<"Num Tyres : "<<numTyres<<endl;
cout<<"Color : "<<color<<endl;
cout<<"Num Gears :"<<numGears<<endl;
// cout<<"Max speed : "<<maxSpeed<endl;
}
};
class HondaCity:public Car{
public:
HondaCity(int x,int y):Car(x,y) {
cout<<"HondaCity constructor"<<endl;
}
~HondaCity(){
cout<<"HondaCity destructor"<<endl;
}
};
int main() {
// Vehicle v;
//v.color = "Blue";
//v.maxSpeed = 4;
//v.numTyres = 4;
HondaCity h(4,5);
//Car c(5);
//c.color = "Black";
//c.numTyres = 4;
// c.numGears = 5;
return 0;
}*/
///MULTIPLE INHERITANCE
/*class Teacher
{
public:
Teacher()
{
cout<<"Teacher Constructor"<<endl;
}
string name;
int age;
void print()
{
cout<<"teacher"<<endl;
cout<<"teacher 2"<<name<<endl;
}
};
class Student
{
public:
Student()
{
cout<<"Student Constructor"<<endl;
}
string name;
void print()
{
cout<<"Student"<<endl;
}
};
class TA : public Teacher, public Student
{
public:
void print()
{
cout<<"TA"<<endl;
}
};
int main()
{
TA a;
a.print();
a.Teacher::name = "abcd";
a.Student::print();
TA b;
b.print();
b.Teacher::name = "shantanu";
b.Student::print();
return 0;
}*/
///HYBRID INHERITANCE
///Diamond Prob
/*class Vehicle
{
private :
int maxSpeed;
protected :
int numTyres;
public :
string color;
//Vehicle(){
//cout<<"Vehicle default constructor"<<endl;
//}
Vehicle(int z)
{
cout<<"Vehicle parameterised constructor "<<z<<endl;
maxSpeed = z;
}
void print()
{
cout<<"Vehicle"<<endl;
}
~Vehicle()
{
cout<<"Vehicle destructor"<<endl;
}
};
class Sum {
public:
int add(int num1,int num2){
return num1 + num2;
}
int add(int num1, int num2, int num3){
return num1 + num2 + num3;
}
};
int main(void) {
//Object of class Sum
Sum obj;
//This will call the second add function
cout<<obj.add(10, 20, 30)<<endl;
//This will call the first add function
cout<<obj.add(11, 22);
return 0;
}
class Complex {
private:
int real, imag;
public:
Complex(int r = 0, int i = 0)
{
real = r;
imag = i;
}
// This is automatically called
// when '+' is used with between
// two Complex objects
Complex operator+(Complex const& obj)
{
Complex res;
res.real = real + obj.real;
res.imag = imag + obj.imag;
return res;
}
void print() { cout << real << " + i" << imag << endl; }
};