Table of Contents
1.Introduction to C++ 4-10
2. Functions 11-20
3. Class and Object 21-36
4. Constructor & Destructor 37-43
5. Operator Overloading 44-49
6. Inheritance 50-61
7. Pointers 62-69
8. Polymorphism 70
9.Templates 71-75
10.Exception Handling 76-78
10. Files and Streams 79-89
1. INTRODUCTION TO C++
Programs
Program
Procedural, Structured, and Object-Oriented Programming
A very Minor Difference between C & C++
The ANSI Standard
Preparing to Program
Compiling the Source Code
.
Creating an Executable File with the Linker
1.
2.
3.
HELLO.CPP Your First C++ Program
HELLO.CPP, the Hello Program.
1:
2:
3:
4: ;
6: ;
7:
Compile Errors
The Parts of a C++ Program
A Simple Program
How cout is used:
A Brief Look at cout and cin
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
The bool data type
2. Functions
What Is a Function?
Declaring and Defining Functions
Declaring the Function
Function Prototypes
Defining the Function
The header and body of a function.
Functions
Local Variables
Global Variables
Function Arguments
Parameters Are Local Variables
Return Values
Default Arguments in Functions
A demonstration of default parameter values.
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:
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:
Special Topics about Functions
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
Classes and Members
Declaring a Class
Defining an Object
Accessing Class Members
.
Assign to Objects, Not to Classes
DO DON'T
DON'T
DON'T
DO
private versus public
Make Member Data Private
public accessor method
The class keyword
Implementing Class Methods
Implementing the methods of a simple class.
1:
2: 3: 4:
5:
6:
7:
8:
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:
Output:
Analysis:
Constructors and Destructors
~
DO DON'T
DON'T
Classes with Other Classes as Member Data
Declaring a complete 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:
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
Nesting of Member Function
Array as Member
Array of Object
struct.
Object as Function Argument:
Scope Resolution Operator
1:
2:
3:
4:
5:
6:
7:
8:
9: 10:
Output:
Static Member Data
Static member data.
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
Static Member Functions
Static member 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:
Output:
Analysis:
Advanced Functions
Overloaded Member Functions
Overloading member 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
The Default Constructor
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.
Constructors with Arguments
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
Overloading the 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:
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
A code snippet showing initialization of member variables.
1:
2:
3: 4: 5: 6:
7:
8:
9: 10: 11:
Output:
5. Operator Overloading
The Counter 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:
Output:
Analysis:
Writing an Increment Function
Adding an increment operator.
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:
Overloading the Prefix Operator
Overloading operator ++.
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
The Addition Operator
The Add ( ) 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:
37:
38:
39:
40:
41: 42:
43: 44:
Output:
Analysis:
Issues in Operator Overloading
Limitations on Operator Overloading
friend Functions and Operator Overloading
Friendly operator +.
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:
Output:
Analysis:
6. Inheritance
Types of inherence
base_class_name
public:
data hiding
protected:
private:
Constructors and Destructors
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:
Passing Arguments to Base Constructors
Overloading constructors in derived 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: 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
Problems with Multiple Inheritance
DO
DO
DO
DON'T
Multi Level Inheritance
Intermediate base class
Virtual Base Class
virtual
Abstract classes
Abstract class
Nesting of Classes: Member Classes
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
Calling the Base Method
DO DO
DON'T
7. Pointers
What Is a Pointer?
pointer
Pointer Names
The Indirection Operator
Pointers, Addresses, and Variables
Examining the Address
DO DO
. DO
Pointers
The this Pointer
Using the this pointer.
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:
Output:
Analysis:
What Is a Reference?
DO DO DON'T
DON'T
Null Pointers and Null References
Passing Function Arguments by Reference
Returning Multiple Values
Returning values with pointers.
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
Memory Allocation Management
new
Pointer variable = new data-type;
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
Run time Polymorphism
9. Templates
Function Templates
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
Templates and Friends
A non-template friend class or function.
A general template friend class or function.
A type-specific template friend class or function.
Static Members and Templates
The Standard Template Library
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
Exceptions and Templates
10. Files and Streams
Streams
The stream class hierarchy
Formatted I/O operations
The width function
The precision() function
The fill() function
The setf() function
The file I/O system
The close function
Reading and writing text files
The file pointers
The seekg() function
The seekp() function
The tellg() function
Functions for I/O operations
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
The read() and write() 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.
Checking the I/O status by using the functions:
Command line arguments
Bugs and Debugging
Breakpoints
Watch Points