Programming in CPP
Programming in CPP
2. Functions 11-20
6. Inheritance 50-61
7. Pointers 62-69
8. Polymorphism 70
9.Templates 71-75
Programs
Program
Preparing to Program
.
Creating an Executable File with the Linker
1.
2.
3.
Compile Errors
A Simple Program
How cout is used:
Comments
Flexible Declarations
1.
2.
3.
4.
5.
6.
7.
8.
9.
Manipulators
1.
2.
3.
4.
5.
6.
Typecasting
void Pointers
References
The const qualifier
What Is a Function?
Local Variables
Global Variables
Function Arguments
Return Values
Analysis:
Overloading Functions
Function overloading polymorphism
A demonstration of function polymorphism.
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:
Output:
Analysis:
Inline Functions
Demonstrates an inline function.
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:
Output:
Analysis:
Recursion
Friend Functions
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.
Friend classes
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.
3. Class and Object
Declaring a Class
Defining an Object
DO DON'T
DON'T
DON'T
DO
30:
31:
32:
33: 34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44;
45:
Output:
Analysis:
DO DON'T
DON'T
39:
40:
41: 42:
43: 44: 45:
46:
47:
48:
49:
50:
51:
52:
53: 54:
RECT.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:
Output:
Analysis:
Structures
DO DO
DO
Array as Member
Array of Object
struct.
Output:
1: 2:
3: 4:
5: 6:
7:
8:
9:
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:
Output:
Analysis:
DO DO
DON'T
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:
Output:
Analysis:
Advanced Functions
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:
Analysis:
Choosing Between Default Values and Overloaded Functions
4. Constructor & Destructor
A simple constructor
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19. 20.
Constructors with default arguments
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.
Copy Constructors
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.
Destructors
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
output:
Overloading Constructors
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:
Output:
Analysis:
Initializing Objects
1:
2:
3: 4: 5: 6:
7:
8:
9: 10: 11:
Output:
5. Operator Overloading
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:
Output:
Analysis:
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:
29:
30:
31: 32:
Analysis:
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:
Output:
Analysis:
DO DO
DON'T
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:
Output:
Analysis:
Output:
Analysis:
6. Inheritance
Types of inherence
base_class_name
public:
data hiding
protected:
private:
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:
Output:
Analysis:
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: 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:
122:
123:
124:
125:
126: 127:
Output:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
Analysis:
Multiple Inheritance
Multiple inheritances.
Output:
Analysis:
Declaring Multiple Inheritance
DO
DO
DO
DON'T
virtual
Abstract classes
Abstract class
Overriding Functions
overriding
signature
Overriding a base class method in a derived class.
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:
Output:
Analysis:
Overloading Versus Overriding
DO DO
DON'T
7. Pointers
What Is a Pointer?
pointer
Pointer Names
DO DO
. DO
Pointers
The this Pointer
What Is a Reference?
DO DO DON'T
DON'T
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:
Output:
Analysis:
DO . DO
DO
DON'T
new
delete
delete pointer-variable;
new
Virtual Function
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.
Pure Virtual Functions
8. Polymorphism
Static binding
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
Output:
Class templates
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.
Friend Functions
Pointers to Functions
10. Exception Handling
Exceptions
try Block
catch Block
Catching Exceptions
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.
Exception Hierarchies
Streams
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
Breakpoints
Watch Points