-
Notifications
You must be signed in to change notification settings - Fork 0
/
OOP.cpp
145 lines (112 loc) · 2.1 KB
/
OOP.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
#include<bits/stdc++.h>
using namespace std;
// Constructors type
class student{
public:
int roll;
int age;
};
//user defined Constructors
class add{
int a,b;
public:
add(){
a=10,b=20;
}
int sum(){
return a+b;
}
};
// Parameterized const
class multi{
int a, b;
public:
multi(int i, int j){
a = i;
b = j;
}
int getmulti(){
return a*b;
}
};
// OOP CONCEPTS - Encapsulation, Abstraction,
class oop{
// class Members
int x;
int y;
public: // Access modifier
// Encapsulation and getter, setter
void set(int a,int b){
x=a;
y=b;
}
int get(){
return x,y;
}
// Abstraction
void display(){
cout<<x<<" "<<y<<endl;
}
};
// Inheritance and its type //
// MULTILEVEL inheritance
// Base class
class vehicle{
public:
string brand = "Ford";
void sound(){
cout<<"tutuu";
}
};
// Derived class
class car: public vehicle{
public:
string model = "mustang";
};
// 2nd derived
class fourwheel: public car{
public:
void dis(){
cout<<"this is example of multilevel inheritance."<<endl;
}
};
// MULTIPLE inheritance
class bike : public car, public vehicle{
public:
void disp(){
cout<<"this is example of multiple inheritance."<<endl;
}
};
// HIERARCHICAL inheritance
class cycle: public vehicle{
};
class cycle2: public vehicle{
};
class cycle3 : public cycle2{
};
// HYBRIDE inheritance
class d: public vehicle, public student{
};
int main(){
// Static allocation of data type student or class student object
student s;
cin>>s.roll>>s.age;
cout<<s.roll<<" "<<s.age<<endl;
//Dyanic allocation of same
student *s2 = new student;
s2 -> roll = 32;
s2 -> age = 18;
cout<<s2 -> roll<<' '<<s2 -> age<<endl;
delete s2;
// user defined constructor
add a1;
cout<<a1.sum()<<endl;
// parameter consturctor
multi m1(2,3);
cout<<m1.getmulti();
// object of class oop
oop p1;
// Inheritance obj
car mycar;
mycar.sound();
}