-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabstract_interface.cpp
More file actions
83 lines (73 loc) · 2.19 KB
/
Copy pathabstract_interface.cpp
File metadata and controls
83 lines (73 loc) · 2.19 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
/*
--with the help of abstract class, we can implement the interface in C++.
--with the help of interface we can create an appropriate base class with the help of that base class we can create mulptiple derived classes.
--interface in C++ is implemented using abstract class.
--interface is a class which work as a base class for child classes.
--if inside a class, a function is declared as pure virtual function, then the class is called abstract class.
--because our abstract class is not providing the implementation of the pure virtual function. sop, we cannot create the object of the abstract class.
--we can only create the object of the derived class.
--each class can have their own implementation of the pure virtual function.
*/
/*
#include<bits/stdc++.h>
using namespace std;
class Strategy{
public:
virtual void submitOrder()=0;
};
class Strategy1:public Strategy{
public:
void submitOrder(){
cout<<"Strategy1"<<endl;
}
};
class Strategy2:public Strategy{
public:
void submitOrder(){
cout<<"Strategy2"<<endl;
}
};
*/
/*
--if we have a normal virtaul function in the base class, then we can provide the implementation of the function in the base class itself.
--A base class pointer can point to objects of derived classes.
--A pointer of the base class type (Strategy*) can point to objects of derived classes (Strategy1, Strategy2).
---When you call a virtual method through the base class pointer, the derived class's implementation is executed (if it exists).
class Strategy{
public:
virtual void submitOrder()=0;
vitual void modifyOrder(){
cout<<"Modify Order"<<endl;
}
};
class Strategy1:public Strategy{
public:
void submitOrder(){
cout<<"Strategy1"<<endl;
}
void modifyOrder(){
cout<<"Modify Order Strategy1"<<endl;
}
};
class Strategy2:public Strategy{
public:
void submitOrder(){
cout<<"Strategy2"<<endl;
}
void modifyOrder(){
cout<<"Modify Order Strategy2"<<endl;
}
};
int main(){
Strategy*s;
Strategy1 s1;
Strategy2 s2;
s=&s1;
s->submitOrder();
s->modifyOrder();
s=&s2;
s->submitOrder();
s->modifyOrder();
return 0;
}
*/