-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchapter_20_inheritance.cpp
More file actions
111 lines (86 loc) · 2.99 KB
/
chapter_20_inheritance.cpp
File metadata and controls
111 lines (86 loc) · 2.99 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 20: INHERITANCE
* =============================================================================
*
* C++ Development Course - Consolidated Examples
* Original Author: Faranak Rajabi
*
* This file contains 5 code example(s) from Chapter 20.
*
* 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: Pass by address allows optional parameters
// ============================================================================
/*
// Pass by address allows optional parameters
void getValue ( int input , int * output ) {
if ( output ) { // Check for null
* output = input * 2;
}
9 }
// Comparison of all three methods
void passByValue ( std :: string str ) {
std :: cout << " By value : " << str << ’\ n ’;
str = " Modified " ; // Doesn ’t affect original
15 }
void p as sB yR e fe re nc e ( std :: string & str ) {
std :: cout << " By reference : " << str << ’\ n ’;
str = " Modified by reference " ; // Affects original
20 }
*/
// ============================================================================
// EXAMPLE 2: Function: passByAddress
// ============================================================================
/*
void passByAddress ( std :: string * str ) {
if (! str ) return ; // Safety check
*/
// ============================================================================
// EXAMPLE 3: Using optional output parameter
// ============================================================================
int main () {
// Using optional output parameter
int result {};
getValue (21 , & result ) ;
std :: cout << " Result : " << result << ’\ n ’;
// ============================================================================
// EXAMPLE 4: Function: enable
// ============================================================================
/*
class Widget {
public :
void enable () {
enabled = true ;
}
*/
// ============================================================================
// EXAMPLE 5: Function: disable
// ============================================================================
/*
void disable () {
enabled = false ;
}
*/
// ============================================================================
// MAIN FUNCTION
// ============================================================================
// Uncomment the examples above and add your test code here
int main() {
std::cout << "Chapter 20: Inheritance" << std::endl;
// Your test code here
return 0;
}