-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathinheritance.cpp
executable file
·66 lines (58 loc) · 1.78 KB
/
inheritance.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
58
59
60
61
62
63
64
65
66
/*
* Inheritance.cpp
*
* Created on: Feb 29, 2012
* Author: behnam
*/
class Base {
public:
int m_nPublic; // can be accessed by anybody
private:
int m_nPrivate; // can only be accessed by Base member functions (but not
// derived classes)
protected:
int m_nProtected; // can be accessed by Base member functions, or derived
// classes.
};
// http://www.learncpp.com/cpp-tutorial/115-inheritance-and-access-specifiers/
// There are three different ways for classes to inherit from other classes:
// public, private, and protected. If you do not choose an inheritance type, C++
// defaults to private inheritance
/* Inherit from Base publicly
Public inheritance is by far the most commonly used type of inheritance.
class Pub: public Base
{
};
// Inherit from Base privately
class Pri: private Base
{
};
// Inherit from Base protectedly
class Pro: protected Base
{
};
class Def: Base // Defaults to private inheritance
{
};
*/
class Derived : public Base {
public:
Derived() {
// Derived's access to Base members is not influenced by the type of
// inheritance used, so the following is always true:
m_nPublic = 1; // allowed: can access public base members from derived class
// m_nPrivate = 2; // not allowed: can not access private base members from
// derived class
m_nProtected =
3; // allowed: can access protected base members from derived class
}
};
// you can't inherit from this class
class CBase final {};
int main() {
Base cBase;
cBase.m_nPublic = 1; // allowed: can access public members from outside class
// cBase.m_nPrivate = 2; // not allowed: can not access private members from
// outside class cBase.m_nProtected = 3; // not allowed: can not access
// protected members from outside class
}