Repository navigation
Expand file tree
/
Copy pathpolymorphism.cpp
More file actions
340 lines (292 loc) · 7.44 KB
/
Copy pathpolymorphism.cpp
File metadata and controls
340 lines (292 loc) · 7.44 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
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
#include<exception>
#include <cstring>
using namespace std;
//!____________________________________________1 ex________________________________________
// int main(){
// int *ptr,**ptr1 ,a;
// a=6;
// ptr=&a;
// ptr1=&ptr;
// cout<<ptr<<endl;
// cout<<ptr1<<endl;
// cout<<*ptr<<endl;
// cout<<*ptr1<<endl;
// cout<<**ptr1<<endl;
// ptr+=2;
// cout<<ptr<<endl;
// ptr1+=2;
// cout<<ptr1<<endl;
// }
//!________________________________2ex__________________________________________________________
// int main(){
// int a=4;
// int *ptr=&a;
// cout<<*ptr;
// cout<<*ptr+*ptr;
// }
//!_______________________3 ex___________________________________________________________________________
// int main(){
// int a[]={1,2,3,4,5};
// int *ptr;
// for(int i=0;i<5;i++){
// cout<<a[i];
// }
// ptr=a;
// cout<<*ptr;
// ptr++;
// cout<<*ptr;
// ptr--;
// cout<<*ptr;
// ptr+=2;
// cout<<*ptr;
// ptr++;
// cout<<*ptr;
// }
// //!____________________________________4 ex_____________________________________________________
// int main(){
// int n;
// cin>>n;
// int a[n];
// for(int i=0;i<n;i++){
// cin>>a[i];
// }
// int *ptr;
// ptr=a;
// int m;
// cin>>m;
// for(int i=0;i<n;i++){
// if(*ptr==m){
// cout<<i;
// break;
// }
// else if(i==n)
// cout<<"NOT FOUND";
// ptr++;
// }
// }
// //!____________________________________5 ex_____________________________________________________
// int main(){
// int i=0;
// char *ptr[7]{"neha","gudu","datu","papa","muma"};
// char str[25];
// cin>>str;
// for( i=0;i<5;i++){
// if(!strcmp(str,ptr[i])){
// cout<<"FOUND";
// break;
// }
// }
// if(i==5)
// cout<<"NOT FOUND";
// }
// //!____________________________________6 ex_____________________________________________________
// int main(){
// char str[]="neha patel y";
// int l=strlen(str);
// char *sub=new char[l];
// cin>>sub;
// int l1=strlen(sub);
// int k;
// for(int i=0;i<l;i++){
// k=i;
// for(int j=0;j<l1;j++){
// if(str[k]==sub[j]){
// if(j==l1-1){
// cout<<"FOUND";
// exit(0);
// }
// k++;
// }
// else break;
// }
// }
// if(k==l-1)
// cout<<"NOT FOUND";
// }
//!____________________________________7 ex_____________________________________________________
// typedef void (*fun)(int,int);
// void add(int i,int j){
// cout<<i+j;
// }
// void sub(int i,int j){
// cout<<i-j;
// }
// int main(){
// fun ptr;
// ptr=&add;
// add(4,5);
// ptr=⊂
// sub(3,2);
// }
// //? othe one
// #include <iostream>
// void add(int i, int j) {
// std::cout << i + j;
// }
// void sub(int i, int j) {
// std::cout << i - j;
// }
// int main() {
// void (*ptr)(int, int); // Define a function pointer type
// ptr = &add; // Assign address of add function to the function pointer
// add(4, 5); // Call add function directly
// ptr(4, 5); // Call add function using function pointer
// ptr = ⊂ // Assign address of sub function to the function pointer
// sub(3, 2); // Call sub function directly
// ptr(3, 2); // Call sub function using function pointer
// return 0;
// }
// //!____________________________________8 ex_____________________________________________________
// class i{
// int p,c;
// public:
// i(){}
// void set(int x,int y){
// p=x; c=y;
// }
// void get(){
// cout<<p<<c;
// }
// };
// int main(){
// i *p=new i[2];
// i *d=p;
// int a,b;
// for(int i=0;i<2;i++){
// cin>>a>>b;
// p->set(a,b);
// p++;
// }
// for(int i=0;i<2;i++){
// d->get();
// d++;
// }
// }
//!____________________________________9 ex_____________________________________________________
// class city{
// int l;
// char *name;
// public:
// city(){
// }
// void set(){
// char str[30];
// cin>>str;
// int l1=strlen(str);
// name=new char [l1+1];
// strcpy(name,str);
// }
// void get(){
// cout<<name;
// }
// };
// int main(){
// city *ptr[10];
// int n=1;
// int op=1;
// do{
// ptr[n]=new city;
// ptr[n]->set();
// n++;
// cin>>op;
// }
// while(op);
// for(int i=0;i<n;i++){
// ptr[i]->get();
// }
// }
//!____________________________________10 ex_____________________________________________________
// class p{
// char name[10];
// int age;
// public:
// p(char *n ,int a){
// strcpy(name,n);
// age=a;
// }
// p operator >(p pa){
// if(age>pa.age)
// return *this;
// else return pa;
// }
// void display(){
// cout<<name<<age;
// }
// };
// int main(){
// p p1("neha",18),p2("gudu",22),p3("datu",13);
// p pp=p1>p2;
// p ppp=p1>p3;
// pp.display();
// ppp.display();
// }
//!____________________________________11 ex_____________________________________________________
// #include <iostream>
// using namespace std;
// class Base {
// public:
// virtual void display() {
// cout << "Base class" << endl;
// }
// };
// class Derived : public Base {
// public:
// void display() override {
// cout << "Derived class" << endl;
// }
// };
// int main() {
// Base* basePtr; // Pointer to the base class
// Base baseobj;
// Derived derivedObj; // Create an object of the derived class
// basePtr = &baseobj; // Point the base pointer to the derived object
// basePtr = &derivedObj;
// basePtr->display(); // Calls the derived class display() function
// return 0;
// }
#include <iostream>
using namespace std;
class Base {
protected:
int baseVar;
public:
Base(int value) : baseVar(value) {}
void display() {
cout << "Base class variable: " << baseVar << endl;
}
void info() {
cout << "Base class info" << endl;
}
};
class Derived : public Base {
private:
int derivedVar;
public:
Derived(int baseValue, int derivedValue)
: Base(baseValue), derivedVar(derivedValue) {}
void display() {
cout << "Derived class variable: " << derivedVar << endl;
}
void info() {
cout << "Derived class info" << endl;
}
};
int main() {
Base* basePtr; // Pointer to the base class
Derived* derivedPtr; // Pointer to the derived class
Derived derivedObj(10, 20); // Create an object of the derived class
basePtr = &derivedObj; // Point the base pointer to the derived object
derivedPtr = &derivedObj; // Point the derived pointer to the derived object
basePtr->display(); // Calls the base class display() function
// derivedPtr->display(); // Calls the derived class display() function
basePtr->info(); // Calls the derived class info() function
// derivedPtr->info(); // Calls the derived class info() function
basePtr->Base::display(); // Access base class member using base pointer
return 0;
}