This repository was archived by the owner on Jun 2, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDsaOOP.cpp
More file actions
138 lines (99 loc) · 2.08 KB
/
DsaOOP.cpp
File metadata and controls
138 lines (99 loc) · 2.08 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
#include <iostream>
using namespace std;
// Class and Object
class Car {
public:
string brand;
int year;
void display() {
cout << "Brand: " << brand << ", Year: " << year << endl;
}
};
// Encapsulation (Hiding internal details and exposing necessary details)
class Student {
private:
int age; // hidden
public:
void setAge(int a) {
if (a > 0)
age = a;
}
int getAge() {
return age;
}
};
// Inheritance
class Animal {
public:
void speak() {
cout << "Animal speaks" << endl;
}
};
class Dog : public Animal {
public:
void bark() {
cout << "Dog barks" << endl;
}
};
// Compile time Polymorphism
class Print {
public:
void show(int a) {
cout << "Integer: " << a << endl;
}
void show(string s) {
cout << "String: " << s << endl;
}
};
// Runtime polymorphism
class Mammal {
public:
virtual void sound() {
cout << "Animal sound" << endl;
}
};
class Cat : public Mammal {
public:
void sound() override {
cout << "Meow" << endl;
}
};
//Abstraction
class Shape {
public:
virtual void draw() = 0;
};
class Circle : public Shape {
public:
void draw() override {
cout << "Drawing Circle" << endl;
}
};
int main() {
Car myCar; // object
myCar.brand = "Toyota";
myCar.year = 2020;
myCar.display(); // calling method ;
//Encapsulation
Student s;
s.setAge(20);
cout << "Student Age: " << s.getAge() << endl;
// inheritance
Dog d;
d.speak(); // inherited from Animal
d.bark(); // own method
//compile polymorphism (method overloading)
Print p;
p.show(5);
p.show("Hello");
// runtime polymorphism
Mammal* a;
Cat c;
a = &c;
a->sound();
// Abtraction
Shape* shape = new Circle();
shape->draw(); // abstraction in action
delete shape;
return 0;
}