-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14_multi_state.cpp
57 lines (51 loc) · 972 Bytes
/
14_multi_state.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
//
// Created by xuejun on 23-5-20.
//
//父类指针指向子类
//通过c++多态实现函数功能封装,外部只能调用父类暴露的接口,无法对子类方法进行访问
#include <iostream>
#include <memory>
using namespace std;
//region multi-state abstract class
class Infer
{
public:
virtual void forward()=0;
};
class InferImp:public Infer
{
public:
void forward() override
{
cout<<"InferImp forward func has been called"<<endl;
}
void new_func()
{
cout<<"A func belong to InferImp"<<endl;
}
};
shared_ptr<Infer> Test_func()
{
shared_ptr<InferImp> p1(new InferImp());
return p1;
}
//endregion
class Desk
{
public:
virtual void print(){}
};
class Desk2:public Desk
{
public:
void print(){cout<<"Desk2 print"<<endl;}
void get(){cout<<"Call get fun"<<endl;}
};
int main()
{
Desk* p2 = new Desk;
p2 = new Desk2;
p2->print();
// auto p = Test_func();
// p->forward();
}