-
Notifications
You must be signed in to change notification settings - Fork 160
/
struct.cpp
42 lines (31 loc) · 930 Bytes
/
struct.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
/*
# struct
Structs in C++ are very similar to classes: support access modifiers,
inheritance, constructors, templates, etc.
The major difference between them is that the default access modifier for structs
is public, while for classes it is private.
This is why structs are used on many simple short language examples:
no public line is needed.
The Google C++ style guide recommends using struct only if there is no constructors,
and classes otherwise.
<http://stackoverflow.com/questions/2750270/c-c-struct-vs-class>
*/
#include "common.hpp"
template<class T>
struct BaseStruct {
T i;
BaseStruct(T i) : i(i) {}
protected:
int iProtected;
private:
int iPrivate;
};
struct DerivedStruct : BaseStruct<int> {
DerivedStruct(int i) : BaseStruct(i) {
iProtected = i;
}
};
int main() {
struct DerivedStruct s(1);
assert(s.i == 1);
}