-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOOPS_Practice_Programs.cpp
More file actions
726 lines (536 loc) · 14.4 KB
/
OOPS_Practice_Programs.cpp
File metadata and controls
726 lines (536 loc) · 14.4 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
// (1) Question: Define a class with a private member and a public member function to access the private member.
#include <iostream>
class MyClass {
private:
int privateMember;
public:
void setPrivateMember(int value) {
privateMember = value;
}
int getPrivateMember() const {
return privateMember;
}
};
int main() {
MyClass obj;
obj.setPrivateMember(42);
std::cout << "Private member value: " << obj.getPrivateMember() << std::endl;
return 0;
}
// (2) Question: Create a class representing a 2D point and calculate the distance between two points.
#include <iostream>
#include <cmath>
class Point2D {
private:
double x, y;
public:
Point2D(double xCoord, double yCoord) : x(xCoord), y(yCoord) {}
double distanceTo(const Point2D& other) const {
return std::sqrt((x - other.x) * (x - other.x) + (y - other.y) * (y - other.y));
}
};
int main() {
Point2D p1(1.0, 2.0);
Point2D p2(4.0, 6.0);
std::cout << "Distance between points: " << p1.distanceTo(p2) << std::endl;
return 0;
}
// (3) Question: Create a class representing a Circle with member functions to calculate its area and circumference.
#include <iostream>
class Circle {
private:
double radius;
public:
Circle(double r) : radius(r) {}
double area() const {
return 3.14159 * radius * radius;
}
double circumference() const {
return 2 * 3.14159 * radius;
}
};
int main() {
Circle c(5.0);
std::cout << "Area: " << c.area() << std::endl;
std::cout << "Circumference: " << c.circumference() << std::endl;
return 0;
}
// (4) Question: Implement a class for a simple Bank Account with deposit and withdraw functions.
#include <iostream>
class BankAccount {
private:
double balance;
public:
BankAccount(double initialBalance) : balance(initialBalance) {}
void deposit(double amount) {
balance += amount;
}
void withdraw(double amount) {
if (amount <= balance)
balance -= amount;
else
std::cout << "Insufficient funds." << std::endl;
}
double getBalance() const {
return balance;
}
};
int main() {
BankAccount account(1000.0);
account.deposit(500.0);
account.withdraw(200.0);
std::cout << "Current balance: " << account.getBalance() << std::endl;
return 0;
}
// (5) Question: Implement a class hierarchy for different shapes (e.g., Circle, Square) with a common base class. Calculate their areas.
#include <iostream>
class Shape {
public:
virtual double area() const = 0;
};
class Circle : public Shape {
private:
double radius;
public:
Circle(double r) : radius(r) {}
double area() const override {
return 3.14159 * radius * radius;
}
};
class Square : public Shape {
private:
double side;
public:
Square(double s) : side(s) {}
double area() const override {
return side * side;
}
};
int main() {
Circle c(5.0);
Square s(4.0);
std::cout << "Area of Circle: " << c.area() << std::endl;
std::cout << "Area of Square: " << s.area() << std::endl;
return 0;
}
// (6) Question: Implement a class representing a Student with a constructor and member functions to set and display student details.
#include <iostream>
#include <string>
class Student {
private:
std::string name;
int rollNumber;
public:
Student(const std::string& n, int roll) : name(n), rollNumber(roll) {}
void display() const {
std::cout << "Name: " << name << ", Roll Number: " << rollNumber << std::endl;
}
};
int main() {
Student student("Alice", 12345);
student.display();
return 0;
}
// (7) Question: Create a class representing a Book with member functions to set and display book details.
#include <iostream>
#include <string>
class Book {
private:
std::string title;
std::string author;
public:
void setTitle(const std::string& t) {
title = t;
}
void setAuthor(const std::string& a) {
author = a;
}
void display() const {
std::cout << "Title: " << title << ", Author: " << author << std::endl;
}
};
int main() {
Book book;
book.setTitle("The Great Gatsby");
book.setAuthor("F. Scott Fitzgerald");
book.display();
return 0;
}
// (8) Question: Create a class representing a Rectangle with member functions to calculate its area and perimeter.
#include <iostream>
class Rectangle {
private:
double length;
double width;
public:
Rectangle(double l, double w) : length(l), width(w) {}
double area() const {
return length * width;
}
double perimeter() const {
return 2 * (length + width);
}
};
int main() {
Rectangle rect(5.0, 3.0);
std::cout << "Area: " << rect.area() << std::endl;
std::cout << "Perimeter: " << rect.perimeter() << std::endl;
return 0;
}
// (9) Question: Implement a class representing a Bank with member functions to add and display account details.
#include <iostream>
#include <vector>
class BankAccount {
private:
std::string accountNumber;
double balance;
public:
BankAccount(const std::string& accNumber, double initialBalance) : accountNumber(accNumber), balance(initialBalance) {}
void display() const {
std::cout << "Account Number: " << accountNumber << ", Balance: " << balance << std::endl;
}
};
class Bank {
private:
std::vector<BankAccount> accounts;
public:
void addAccount(const std::string& accNumber, double initialBalance) {
BankAccount newAccount(accNumber, initialBalance);
accounts.push_back(newAccount);
}
void displayAccounts() const {
for (const BankAccount& acc : accounts) {
acc.display();
}
}
};
int main() {
Bank bank;
bank.addAccount("12345", 1000.0);
bank.addAccount("67890", 500.0);
bank.displayAccounts();
return 0;
}
// (10) Question: Implement a class hierarchy for different vehicles (e.g., Car, Motorcycle) with a common base class. Calculate their maximum speed.
#include <iostream>
class Vehicle {
public:
virtual double maxSpeed() const = 0;
};
class Car : public Vehicle {
public:
double maxSpeed() const override {
return 120.0; // Maximum speed in km/h
}
};
class Motorcycle : public Vehicle {
public:
double maxSpeed() const override {
return 180.0; // Maximum speed in km/h
}
};
int main() {
Car car;
Motorcycle motorcycle;
std::cout << "Max Speed of Car: " << car.maxSpeed() << " km/h" << std::endl;
std::cout << "Max Speed of Motorcycle: " << motorcycle.maxSpeed() << " km/h" << std::endl;
return 0;
}
// (11) Question: Implement a class representing a Date with member functions to set and display the date.
#include <iostream>
class Date {
private:
int day, month, year;
public:
void setDate(int d, int m, int y) {
day = d;
month = m;
year = y;
}
void display() const {
std::cout << "Date: " << day << "/" << month << "/" << year << std::endl;
}
};
int main() {
Date date;
date.setDate(15, 9, 2023);
date.display();
return 0;
}
// (12) Question: Create a class representing a Product with member functions to set and display product details.
#include <iostream>
#include <string>
class Product {
private:
std::string name;
double price;
public:
void setDetails(const std::string& productName, double productPrice) {
name = productName;
price = productPrice;
}
void display() const {
std::cout << "Product: " << name << ", Price: $" << price << std::endl;
}
};
int main() {
Product product;
product.setDetails("Laptop", 999.99);
product.display();
return 0;
}
// (13) Question: Implement a class hierarchy for different animals (e.g., Cat, Dog) with a common base class. Display their sounds.
#include <iostream>
#include <string>
class Animal {
protected:
std::string name;
public:
Animal(const std::string& n) : name(n) {}
virtual std::string sound() const = 0;
};
class Cat : public Animal {
public:
Cat(const std::string& n) : Animal(n) {}
std::string sound() const override {
return "Meow";
}
};
class Dog : public Animal {
public:
Dog(const std::string& n) : Animal(n) {}
std::string sound() const override {
return "Woof";
}
};
int main() {
Cat cat("Whiskers");
Dog dog("Buddy");
std::cout << cat.sound() << " (" << cat.name << ")" << std::endl;
std::cout << dog.sound() << " (" << dog.name << ")" << std::endl;
return 0;
}
// (14) Question: Create a class hierarchy for different geometric shapes (e.g., Circle, Square) with a common base class. Calculate their areas.
#include <iostream>
class Shape {
public:
virtual double area() const = 0;
};
class Circle : public Shape {
private:
double radius;
public:
Circle(double r) : radius(r) {}
double area() const override {
return 3.14159 * radius * radius;
}
};
class Square : public Shape {
private:
double side;
public:
Square(double s) : side(s) {}
double area() const override {
return side * side;
}
};
int main() {
Circle c(5.0);
Square s(4.0);
Shape* shapes[] = { &c, &s };
for (const Shape* shape : shapes) {
std::cout << "Area: " << shape->area() << std::endl;
}
return 0;
}
// (15) Question: Implement a class hierarchy for different employees (e.g., Manager, Programmer) with a common base class. Calculate their salaries.
#include <iostream>
class Employee {
protected:
std::string name;
double baseSalary;
public:
Employee(const std::string& n, double salary) : name(n), baseSalary(salary) {}
virtual double calculateSalary() const = 0;
};
class Manager : public Employee {
private:
double bonus;
public:
Manager(const std::string& n, double salary, double b) : Employee(n, salary), bonus(b) {}
double calculateSalary() const override {
return baseSalary + bonus;
}
};
class Programmer : public Employee {
public:
Programmer(const std::string& n, double salary) : Employee(n, salary) {}
double calculateSalary() const override {
return baseSalary;
}
};
int main() {
Manager manager("Alice", 50000, 10000);
Programmer programmer("Bob", 60000);
Employee* employees[] = { &manager, &programmer };
for (const Employee* emp : employees) {
std::cout << "Salary for " << emp->name << ": $" << emp->calculateSalary() << std::endl;
}
return 0;
}
// (16) Question: Implement a class hierarchy for different shapes (e.g., Circle, Rectangle) with a common base class. Calculate their areas.
#include <iostream>
class Shape {
public:
virtual double area() const = 0;
};
class Circle : public Shape {
private:
double radius;
public:
Circle(double r) : radius(r) {}
double area() const override {
return 3.14159 * radius * radius;
}
};
class Rectangle : public Shape {
private:
double length;
double width;
public:
Rectangle(double l, double w) : length(l), width(w) {}
double area() const override {
return length * width;
}
};
int main() {
Circle c(5.0);
Rectangle r(4.0, 3.0);
Shape* shapes[] = { &c, &r };
for (const Shape* shape : shapes) {
std::cout << "Area: " << shape->area() << std::endl;
}
return 0;
}
// (17) Question: Create a class hierarchy for different fruits (e.g., Apple, Banana) with a common base class. Display their colors.
#include <iostream>
#include <string>
class Fruit {
protected:
std::string color;
public:
Fruit(const std::string& c) : color(c) {}
virtual std::string getColor() const {
return color;
}
};
class Apple : public Fruit {
public:
Apple(const std::string& c) : Fruit(c) {}
std::string getColor() const override {
return color + " apple";
}
};
class Banana : public Fruit {
public:
Banana(const std::string& c) : Fruit(c) {}
};
int main() {
Apple redApple("red");
Banana yellowBanana("yellow");
Fruit* fruits[] = { &redApple, &yellowBanana };
for (const Fruit* fruit : fruits) {
std::cout << "Color: " << fruit->getColor() << std::endl;
}
return 0;
}
// (18) Question: Implement a class hierarchy for different shapes (e.g., Triangle, Square) with a common base class. Calculate their areas.
#include <iostream>
class Shape {
public:
virtual double area() const = 0;
};
class Triangle : public Shape {
private:
double base;
double height;
public:
Triangle(double b, double h) : base(b), height(h) {}
double area() const override {
return 0.5 * base * height;
}
};
class Square : public Shape {
private:
double side;
public:
Square(double s) : side(s) {}
double area() const override {
return side * side;
}
};
int main() {
Triangle t(4.0, 3.0);
Square s(4.0);
Shape* shapes[] = { &t, &s };
for (const Shape* shape : shapes) {
std::cout << "Area: " << shape->area() << std::endl;
}
return 0;
}
// (19) Question: Create a class representing a Car with member functions to set and display car details.
#include <iostream>
#include <string>
class Car {
private:
std::string make;
std::string model;
public:
void setDetails(const std::string& carMake, const std::string& carModel) {
make = carMake;
model = carModel;
}
void display() const {
std::cout << "Car Make: " << make << ", Model: " << model << std::endl;
}
};
int main() {
Car car;
car.setDetails("Toyota", "Camry");
car.display();
return 0;
}
// (20) Question: Implement a class hierarchy for different shapes (e.g., Circle, Rectangle) with a common base class. Calculate their perimeters.
#include <iostream>
class Shape {
public:
virtual double perimeter() const = 0;
};
class Circle : public Shape {
private:
double radius;
public:
Circle(double r) : radius(r) {}
double perimeter() const override {
return 2 * 3.14159 * radius;
}
};
class Rectangle : public Shape {
private:
double length;
double width;
public:
Rectangle(double l, double w) : length(l), width(w) {}
double perimeter() const override {
return 2 * (length + width);
}
};
int main() {
Circle c(5.0);
Rectangle r(4.0, 3.0);
Shape* shapes[] = { &c, &r };
for (const Shape* shape : shapes) {
std::cout << "Perimeter: " << shape->perimeter() << std::endl;
}
return 0;
}