-
Notifications
You must be signed in to change notification settings - Fork 3k
/
main.cpp
121 lines (108 loc) · 3.41 KB
/
main.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/***************************************************************************
* @file main.cpp
* @author Alan.W
* @date 23 Jan 2014
* @remark This code is for the exercises from C++ Primer 5th Edition
* @note
***************************************************************************/
//
// Exercise 15.18:
// Given the classes from page 612 and page 613, and assuming each object
// has the type specified in the comments, determine which of these assignments
// are legal. Explain why those that are illegal aren’t allowed:
//
// Base *p = &d1; // d1 has type Pub_Derv -- legal --right
// p = &d2; // d2 has type Priv_Derv -- illegal --right
// p = &d3; // d3 has type Prot_Derv -- illegal --right
//
// p = &dd1; // dd1 has type Derived_from_Public -- legal --right
// p = &dd2; // dd2 has type Derived_from_Private -- illegal --right
// p = &dd3; // dd3 has type Derived_from_Protected -- illegal --right
//
// User code may use the derived-to-base conversion only if D inherits
// publicly from B. User code may not use the conversion if D inherits
// from B using either protected or private.
//
// Exercise 15.19:
// Assume that each of the classes from page 612 and page 613 has a member
// function of the form:
//
// void memfcn(Base &b) { b = *this; }
//
// For each class, determine whether this function would be legal.
//
// Member functions and friends of D can use the conversion to B regardless
// of how D inherits from B. The derived-to-base conversion to a direct base
// class is always accessible to members and friends of a derived class.
// Hence, the 3 below are all legal:
//
// Pub_Derv -- legal --right
// Priv_Derv -- legal --right
// Prot_Derv -- legal --right
//
// Member functions and friends of classes derived from D may use the
// derived-to-base conversion if D inherits from B using either public or
// protected. Such code may not use the conversion if D inherits privately
// from B.Hence:
// Derived_from_Public -- legal --right
// Derived_from_Private -- illegal --right
// Derived_from_Protected -- legal --right
//
// Exercise 15.20:
// Write code to test your answers to the previous two exercises.
//
#include <iostream>
#include <string>
#include "quote.h"
#include "bulk_quote.h"
#include "limit_quote.h"
#include "disc_quote.h"
class Base
{
public:
void pub_mem(); // public member
protected:
int prot_mem; // protected member
private:
char priv_mem; // private member
};
struct Pub_Derv : public Base
{
void memfcn(Base &b) { b = *this; }
};
struct Priv_Derv : private Base
{
void memfcn(Base &b) { b = *this; }
};
struct Prot_Derv : protected Base
{
void memfcn(Base &b) { b = *this; }
};
struct Derived_from_Public : public Pub_Derv
{
void memfcn(Base &b) { b = *this; }
};
struct Derived_from_Private : public Priv_Derv
{
//void memfcn(Base &b) { b = *this; }
};
struct Derived_from_Protected : public Prot_Derv
{
void memfcn(Base &b) { b = *this; }
};
int main()
{
Pub_Derv d1;
Base *p = &d1;
Priv_Derv d2;
//p = &d2;
Prot_Derv d3;
//p = &d3;
Derived_from_Public dd1;
p = &dd1;
Derived_from_Private dd2;
//p =& dd2;
Derived_from_Protected dd3;
//p = &dd3;
return 0;
}