Repository navigation
Expand file tree
/
Copy pathcpp.html
More file actions
918 lines (809 loc) · 31.5 KB
/
Copy pathcpp.html
File metadata and controls
918 lines (809 loc) · 31.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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>cpp resources</title>
<link rel="stylesheet" href="styles.css">
<style>/* Reset default margin and padding */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
/* Body styles */
body {
font-family: 'Roboto', sans-serif;
background-color: #1a1a1a;
color: #fff;
line-height: 1.6;
margin: 0;
padding: 0;
}
/* Navigation styles */
.navigation {
width: 250px;
background-color: #282828;
position: fixed;
top: 0;
bottom: 0;
overflow-y: auto;
padding-top: 60px;
}
.navigation ul {
list-style: none;
padding-left: 20px;
}
.navigation ul li {
margin-bottom: 10px;
}
.navigation ul li a {
color: #fff;
text-decoration: none;
font-size: 1.1em;
padding: 10px 20px;
border-radius: 5px;
transition: background-color 0.3s;
}
.navigation ul li a:hover {
background-color: #333;
}
/* Main content styles */
.main-content {
margin-left: 250px;
padding: 20px;
}
.main-content h2 {
font-size: 2em;
margin-bottom: 20px;
}
.main-content p {
font-size: 1.1em;
margin-bottom: 20px;
}
/* Footer styles */
.footer {
background-color: #282828;
color: #fff;
text-align: center;
padding: 10px 0;
position: fixed;
bottom: 0;
width: 100%;
}
/* Smooth scrolling */
html {
scroll-behavior: smooth;
}
</style>
</head>
<body>
<!-- Navigation -->
<nav class="navigation">
<ul>
<li><a href="#prac1">PRACTICAL 1</a></li>
<li><a href="#prac2">PRACTICAL 2</a></li>
<li><a href="#prac3">PRACTICAL 3</a></li>
<li><a href="#prac4">PRACTICAL 4</a></li>
<li><a href="#prac5">PRACTICAL 5</a></li>
<li><a href="#prac6">PRACTICAL 6</a></li>
<li><a href="#prac7">PRACTICAL 7</a></li>
<li><a href="#prac8">PRACTICAL 8</a></li>
<li><a href="#prac9">PRACTICAL 9</a></li>
<!-- Add more sections as needed -->
</ul>
</nav>
<!-- Main content -->
<main class="main-content">
<section id="prac1">
<h2>BASIC C++ PROGRAM's</h2>
<pre><code>
1. write a cpp program to create a simple calculator
#include<iostream>
using namespace std;
int main(){
char op;
float num1, num2;
cout<<"enter two number's:"<<endl;
cin>>num1>>num2;
cout<<"enter the operator"<<endl;
cin>>op;
switch(op){
case '+': cout<<"addition:"<<num1+num2<<endl;
break;
case '-': cout<<"subtraction:"<<num1-num2<<endl;
break;
case '*': cout<<"multiplication:"<<num1*num2<<endl;
break;
case '/': cout<<"division:"<<num1/num2<<endl;
break;
default: cout<<"invalid operator!"<<endl;
}
return 0;
}
2.write a cpp program to convert seconds into hours , minutes and seconds.
#include<iostream>
using namespace std;
int main(){
int ts;
float hr, sec, min;
cout<<"enter total seconds:"<<endl;
cin>>ts;
hr = ts / 3600;
min = (ts % 3600) / 60;
sec = (ts % 3600) % 60;
cout<<"seconds in hours: "<<hr<<endl;
cout<<"seconds in minutes: "<<min<<endl;
cout<<"seconds in seconds: "<<sec<<endl;
return 0;
}
3.write a cpp program to find a volume of square, cone, rectangle
#include<iostream>
using namespace std;
int main(){
float a, b, c, d, e, f;
cout<<"enter a side :"<<endl;
cin>>a;
cout<<"volume of square :"<<a*a<<endl;
cout<<"enter radius and height :"<<endl;
cin>>c>>b;
cout<<"volume of cone :"<<(3.14*c*c*b)<<endl;
cout<<"enter rectangle values:"<<endl;
cin>>d>>e>>f;
cout<<"volume of rectangle :"<<d*e*f<<endl;
return 0;
}
</code></pre>
<!-- Add more content as needed -->
</section>
<section id="prac2">
<h2>CONDITIONAL STATEMENTS PROGRAM'S</h2>
<pre><code>
1.write a cpp program to find the greatest of 3 numbers
#include<iostream>
using namespace std;
int main(){
int a,b,c;
cout<<"enter a: "<<endl;
cin>>a;
cout<<"enter b: "<<endl;
cin>>b;
cout<<"enter c: "<<endl;
cin>>c;
if(a>b&&a>c){
cout<<"a is greater than b & c"<<endl;
}
else if(b>a&&b>c){
cout<<"b is greater than a & c"<<endl;
}
else{
cout<<"c is greater than b & a"<<endl;
}
return 0;
}
2.write a cpp program ro find total no. of even and odd numbers
#include<iostream>
using namespace std;
int main(){
//write a code to print the number of even number till the number
int n,evencount,oddcount;
evencount=0;
oddcount=0;
cout<<"enter the number: "<<endl;
cin>>n;
for(int i=1;i<=n;i++){
if(i%2==0){
evencount=evencount+1;
}
else{
oddcount=oddcount+1;
}
}
cout<<"number of even: "<<evencount<<endl;
cout<<"number of odd: "<<oddcount<<endl;
return 0;
}
3.write a cpp program ro generate prime number between 1 and n , where n is input
#include<iostream>
using namespace std;
int main(){
int n;
int chk=0;
cout<<"enter the number: "<<endl;
cin>>n;
for(int i=1;i<=n;i++)
{
for(int j=2;j<i;j++)
{
if(i%j==0)
{
chk++;
break;
}
}
if(chk==0&&i!=1)
{
cout<<i<<endl;
}
chk=0;
}
return 0;
}
</code></pre>
<!-- Add more content as needed -->
</section>
<section id="prac3">
<h2>CLASSES AND OBJECTS</h2>
<pre><CODE>
1. cpp program using classes and object student to print name of student , roll no. display the same.
#include<iostream>
using namespace std;
class student{
public:
string name;
int roll_no;
void input(){
cout<<"enter name: "<<endl;
cin>>name;
cout<<"enter roll no: "<<endl;
cin>>roll_no;
}
void display()
{
cout<<"name is: "<<name<<endl;
cout<<"roll no is: "<<roll_no<<endl;
}
};
int main(){
student s;
s.input();
s.display();
return 0;
}
2.cpp program for bank employee to print name of the a/c no and balance. display the same also display balanace , withdraw and deposit
#include<iostream>
using namespace std;
class bank
{
public:
string name;
int acc;
float bala;
void input(){
cout<<"enter name :"<<endl;
cin>>name;
cout<<"enter a/c no:"<<endl;
cin>>acc;
cout<<"Balance :"<<endl;
cin>>bala;
}
void funuc(){
int choi;
float add,sub,ans;
cout<<"enter 1 for withdraw,or else 2 for deposit: "<<endl;
cin>>choi;
switch(choi){
case 1:
cout<<"acc no: "<<acc<<endl;
cout<<"current bala:"<<bala<<endl;
cout<<"enter the amt:"<<endl;
cin>>sub;
cout<<"balance after withdrawl:"<<bala-sub<<endl;
break;
case 2:
cout<<"acc no: "<<acc<<endl;
cout<<"current bala:"<<bala<<endl;
cout<<"enter the amt:"<<endl;
cin>>add;
cout<<"balance after deposit:"<<bala+add<<endl;
break;
}
}
};
int main()
{
bank Adi;
Adi.input();
Adi.funuc();
return 0;
}
3.write a cpp program to design a class havind static member function names showcount(), which has property of displaying the number of object created of the class
#include<iostream>
using namespace std;
class test{
private:
int code;
static int count;
public:
void set_code(){
code=++count;
}
void show_code(){
cout<<"object number:"<<code<<endl;
}
static void show_count(){
cout<<"count: "<<count<<endl;
}
};
int test::count;
int main(){
test t1, t2, t3;
t1.set_code();
t1.show_code();
t2.set_code();
t2.show_code();
t3.set_code();
t3.show_code();
return 0;
}
4.write a cpp program to find maximum out of two numbers using friend function.
#include<iostream>
using namespace std;
class b; // Forward declaration
class a{
int n;
public:
a(int x){
n = x;
}
friend void greatest(a a1, b b1);
};
class b{
int n1;
public:
b(int x){
n1 = x;
}
friend void greatest(a a1, b b1);
};
void greatest(a a1, b b1){
if(a1.n > b1.n1){
cout<<"number in class a is greatest: "<<a1.n<<endl;
}
else if(a1.n < b1.n1){
cout<<"number in class b is greatest: "<<b1.n1<<endl;
}
else{
cout<<"both numbers are equal"<<endl;
}
}
int main(){
cout<<"Program to find greatest of 2 numbers in 2 different classes using friend function"<<endl;
int n1, n2;
cout<<"Enter number for class a: "<<endl;
cin>>n1;
a a1(n1);
cout<<"Enter number for class b: "<<endl;
cin>>n2;
b b1(n2);
greatest(a1, b1);
cout<<"/n";
return 0;
5.write a cpp progra using copy constructor to copy data of an objecto to another object
#include<iostream>
using namespace std;
class student{
public:
int rollcall;
float cgpa;
student(){
//default constructor
rollcall=0;
cgpa=0.00;
}
student(int x,float y){
//parameter constructor
rollcall=x;
cgpa=y;
}
student(student& a){
//copy constructor
rollcall=a.rollcall;
cgpa=a.cgpa;
}
};
int main(){
student om(14,95.35);
student aditya(om);
student appa;
cout<<"om's rollno:"<<om.rollcall<<endl;
cout<<"om's cgpa:"<<om.cgpa<<endl;
cout<<"aditya's rollno:"<<aditya.rollcall<<endl;
cout<<"aditya's cgpa:"<<aditya.cgpa<<endl;
cout<<"appa's rollno:"<<appa.rollcall<<endl;
cout<<"appa's cgpa:"<<appa.cgpa<<endl;
return 0;
}
6.write a cpp program to allocate memory dynamically for an object of a given class using class constructor
#include<iostream>
using namespace std;
class dynamic{
const char*p;
public:
dynamic(){
p=new char[6];
p="geeks";
}
void display(){
cout<<p<<endl;
}
};
int main(){
dynamic d1;
d1.display();
return 0;
}
</CODE></pre>
</section>
<section id="prac4">
<h2>INHERITANCE </h2>
<PRE><CODE>
1.write a cpp program to illustrate single inheritance.
<#include<iostream>
using namespace std;
class Vehicle
{
public:
Vehicle()
{
cout << "This is a Vehicle\n";
}
};
class Car: public Vehicle
{
};
int main()
{
Car obj;
return 0;
}
2.write a program to illustrate multiple inheritance.
<#include <iostream>
using namespace std;
class Vehicle
{
public:
Vehicle()
{
cout << "This is a Vehicle\n";
} };
class Four Wheeler
{
public:
FourWheeler()
{
cout << "This is a 4 wheeler Vehicle\n";
}};
class Car: public Vehicle, public FourWheeler {
};
int main()
{
Car obj;
return 0;
}
3. write a cpp program to illustrate multi level inheritance.
<#include <iostream>
using namespace std;
class Vehicle
{
public:
Vehicle() { cout << "This is a Vehicle\n"; }
};
class fourWheeler: public Vehicle
{
public:
FourWheeler()
{
cout << "Objects with 4 wheels are vehicles\n";
}
};
class Car public fourWheeler
{
public:
Car()
{ cout << "Car has 4 Wheels\n"; }
};
int main()
{
Car obj;
return 0;
}
4.write a cpp program to illustrate hiearchichal in heritance
<#include <iostream>
using namespace std;
class Vehicle {
public:
Vehicle() { cout << "This is a Wehicle\n"; }
};
class Car: public Vehicle
{
};
class Bus: public Vehicle
{
};
int main()
{
Car obj1;
Bus obj2;
return 0;
}
</CODE></PRE>
</section>
<section id="prac5">
<h2>HYBRID INHERITENCE</h2>
<PRE><CODE>
#include<iostream>
using namespace std;
class student{
public:
int rollno = 15;
void fun1(){
cout << "roll no is:\t" << rollno << endl;
}
};
class test : public student{
public:
int sub1 = 25;
int sub2 = 30;
void fun2(){
cout << "marks of sub1 and sub2 are:\t" << sub1 << "\t" << sub2 << endl;
}
};
class sport{
public:
int score = 35;
void fun3(){
cout << "score is:" << score << endl;
}
};
class result : public sport, public test{
public:
void fun4(){
int total = sub1 + sub2 + score;
cout << "total score is:\t" << total << endl;
}
};
int main(){
result r1;
r1.fun1();
r1.fun2();
r1.fun3();
r1.fun4();
return 0;
}
</CODE></PRE>
</section>
<section id="prac6">
<h2>POINTERS</h2>
<PRE><CODE>
#include<iostream>
using namespace std;
class person
{
char name[20];
int age;
public:
void getdata(){
cout << "enter name:" << endl;
cin >> name;
cout << "enter age:" << endl;
cin >> age;
}
void putdata(){
cout << "\n\n details about eldest person as follows:" << endl;
cout << "\n name=" << name << "\n age=" << age << endl;
}
person &Compare(person &p1){
if(p1.age > this->age)
return p1;
else
return *this;
}
};
int main(){
person x, y, z;
x.getdata();
y.getdata();
z = x.Compare(y);
z.putdata();
return 0;
}
</CODE></PRE>
</section>
<section id="prac7">
<h2>VIRTUAL FUNCTIONS</h2>
<PRE><CODE>
#include<iostream>
using namespace std;
class base{
public:
void display(){
cout << "\ndisplay base" << endl;
}
virtual void show(){
cout << "\n show base" << endl;
}
};
class derived : public base{
public:
void display(){
cout << "\ndisplay derived" << endl;
}
void show(){
cout << "\n show derived" << endl;
}
};
int main(){
base b;
derived d;
base *bptr;
cout << "\n bptr points to base:\n" << endl;
bptr = &b;
bptr->display();
bptr->show();
cout << "\n\nbptr points to derived:\n" << endl;
bptr = &d;
bptr->display();
bptr->show();
return 0;
}
</CODE></PRE>
</section>
<section id="prac8">
<h2>EXCEPTION HANDLING</h2>
<PRE><CODE>
1. write a cpp program to implement the exception handling with multiple catch statement.
<#include<iostream>
using namespace std;
void test(int x)
{
try
{
if (x > 0)
throw x;
else
throw 'x';
}
catch (int x)
{
cout << "Catch a integer and that integer is:" << x<<endl;
}
catch (char x)
{
cout << "Catch a character and that character is:" << x<<endl;
}
int main()
{
cout << "Testing multiple catches: \n";
test(10);
test(0);
}
2.write a cpp program to implement the exception handling with rethrowing in exception.
<#include<iostream>
using namespace std;
void sub(int i, int j)
{
try
{
if(i==0)
{
throw i;
}
else
cout<<"Subtraction result is: "<<i-j<<endl;
}
catch(int i)
{
cout<<"Exception caught inside sub()\n"; throw;
}
};
int main()
{
try
{
sub(8,4);
sub(0,8);
}
catch(int k)
{
cout<<"Exception caught inside main()\n";
}
return 0;
}
</CODE></PRE>
</section>
<section id="prac9">
<h2>STRING MANIPULATION's</h2>
<PRE><CODE>
1 write a c++ code to show conversion from int to string.
<#include <iostream>
<#include<sstream>
using namespace std;
int main()
{
int k;
cout<<"Enter an integer value";
cin>>k;
stringstream ss;
ss<<k;
string s;
ss>>s;
cout<<"String representation of an integer value is: "<<s;
return 0;
}
2 write a c++ program to show conersion of string to integer.
<#include <iostream>
<#include <sstream>
using namespace std;
int main()
{
string str1;
cout<<"Enter an string value: ";
cin>>str1;
int intdata;
stringstream obj;
obj << str1; // insert dato into obj
obj << intdata; // fetch integer type data
cout << " The representation of the string to integer type data is: << intdata << endl;
return 0;
}
3 write a c++ program implementing basic operation of class ios i.e setf, unsetf, precision, etc.
<#include <iostream>
<#include <fstream>
using namespace std;
int main()
{
cout << "Example for formatted 10" << endl;
cout << "Default: " << endl;
cout << 123 << endl;
cout << "width(10): " << endl;
cout.width(10);
cout << 123 << endl;
cout << "width(6) and fill('*'): " << endl;
cout.width(6);
cout.fill('*');
cout << 123 << endl;
cout.precision(5);
cout << "precision (5) ---> " << 123.4567890 << endl;
cout << "precision (5) ---> " <<<<<< 9.876543210 << endl;
cout << "setf (showpos): " << endl;
cout.setf(ios::showpos);
cout << 123 << endl;
cout << "unsetf (showpos): " << endl;
cout.unsetf(ios::showpos);
cout << 123 << endl;
return 0;
}
4 write a cpp program to copy the contents of file to another.
<#include<iostream>
<#include<fstream>
using namespace std;
int main()
{
fstream source, dest;
char sfile [15], dfile[15];
char ch;
cout<<"Enter the source file name"<<endl;
cin>>sfile;
source.open(sfile,ios::in);
cout<<"Enter the destination file name"<<endl;
cin>>dfile;
dest.open(dfile,ios::out);
while(source.eof()==0)
{
source.get(ch);
dest.put(ch);
}
source.close();
dest.close();
}
</CODE></PRE>
</section>
<!-- Add more sections as needed -->
</main>
<!-- Footer -->
<footer class="footer">
<p>© 2024 PL/SQL Documentation</p>
</footer>
</body>
</html>