-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchapter_13_control_flow.cpp
More file actions
111 lines (86 loc) · 3.01 KB
/
chapter_13_control_flow.cpp
File metadata and controls
111 lines (86 loc) · 3.01 KB
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
/*=============================================================================
* CHAPTER 13: CONTROL FLOW
* =============================================================================
*
* C++ Development Course - Consolidated Examples
* Original Author: Faranak Rajabi
*
* This file contains 5 code example(s) from Chapter 13.
*
* USAGE:
* - Review each example section
* - Uncomment the code you want to test
* - Compile: g++ -std=c++17 -Wall thisfile.cpp -o program
* - Run: ./program
*
* NOTE:
* Some examples are code snippets designed to illustrate specific
* concepts and may need additional context to compile.
*
=============================================================================*/
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
// ============================================================================
// EXAMPLE 1: Function: process
// ============================================================================
/*
namespace mylib {
inline namespace v2 { // Current version
void process () { /* new implem entation */ }
}
namespace v1 { // Old version still available
void process () { /* old implem entation */ }
}
*/
// ============================================================================
// EXAMPLE 2: 3. dynamic_cast - safe runtime downcasting
// ============================================================================
/*
// 3. dynamic_cast - safe runtime downcasting
Base * base = new Derived () ;
Derived * derived = dynamic_cast < Derived * >( base ) ;
if ( derived ) {
derived - > derivedOnly () ; // Safe !
}
delete base ;
*/
// ============================================================================
// EXAMPLE 3: Class: Complex
// ============================================================================
/*
class Complex {
private :
double real , imag ;
*/
// ============================================================================
// EXAMPLE 4: Can access protected members
// ============================================================================
/*
class TextBox : public Widget {
public :
void resize ( int w , int h ) {
// Can access protected members
width = w ;
// OK : protected member
height = h ; // OK : protected member
*/
// ============================================================================
// EXAMPLE 5: Outside the class hierarchy , protected acts like private
// ============================================================================
int main () {
TextBox box ;
// Outside the class hierarchy , protected acts like private
// box . width = 200;
// ERROR : protected member
// box . enabled = false ; // ERROR : private member
// ============================================================================
// MAIN FUNCTION
// ============================================================================
// Uncomment the examples above and add your test code here
int main() {
std::cout << "Chapter 13: Control Flow" << std::endl;
// Your test code here
return 0;
}