-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathoop-demo.h
More file actions
43 lines (34 loc) · 881 Bytes
/
oop-demo.h
File metadata and controls
43 lines (34 loc) · 881 Bytes
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
#ifndef __OOPDEMO__
#define __OOPDEMO__
#include <string>
#include <iostream>
// 继承要搭配虚函数 函数继承的函数继承的是调用权
class Shape {
public:
// 纯虚函数 要求子类必须重新定义 override
virtual void draw() const = 0;
// 虚函数 希望子类重新定义 并且有默认行为
virtual void error(const std::string& msg);
// 非虚函数 不希望子类去定义
int objectID() const;
private:
double l;
};
// 继承
class CDocument {
public:
void onOpenFile() {
std::cout << "check file status" << std::endl;
Serialize();
std::cout << "close file" << std::endl;
}
virtual void Serialize() {};
};
class MyDocument : public CDocument{
public:
virtual void Serialize() {
std::cout << "MyDocument Serializing..." << std::endl;
}
};
// 委托+继承
#endif