C++ Programming
Table of Contents
1. Introduction to C++ Programming
2. History and Evolution of C++
3. Features of C++
4. Applications of C++
5. Program Development Life Cycle
6. Structure of a C++ Program
7. Tokens in C++
8. Keywords and Identifiers
9. Variables and Constants
10. Data Types in C++
11. Type Modifiers
12. Operators in C++
13. Input and Output Streams
14. Control Structures
15. Decision Making (if, if-else, nested if)
16. Switch Statement
17. Looping Structures
18. for Loop
19. while Loop
20. do-while Loop
21. Break and Continue Statements
22. goto Statement (Concept & Use)
23. Arrays in C++
24. One-Dimensional Arrays
25. Two-Dimensional Arrays
26. Character Arrays and Strings
27. Functions in C++
28. User-Defined Functions
29. Function Prototypes
30. Parameter Passing Techniques
1. Introduction to C++ Programming
C++ is a powerful, general-purpose programming language that is widely used for developing
both system-level and application-level software. It was designed to combine the efficiency and
control of low-level programming with the flexibility and abstraction of high-level programming.
C++ supports procedural programming, where programs are written as a sequence of
instructions, and object-oriented programming (OOP), where programs are organized around
objects and classes. This dual nature makes C++ a middle-level language.
One of the strongest features of C++ is its ability to manage memory manually using pointers.
This gives programmers fine control over system resources, which is why C++ is preferred for
performance-critical applications such as operating systems, game engines, and embedded
systems.
Key Points
Fast execution speed
Supports structured and object-oriented programming
Used in real-world, large-scale software
Detailed Example
#include <iostream>
using namespace std;
int main() {
cout << "C++ is a powerful programming language" << endl;
return 0;
}
Explanation:
#include <iostream> allows input/output operations
using namespace std; avoids writing std:: again and again
main() is the starting point of program execution
cout displays output on the screen
2. History and Evolution of C++
C++ was developed by Bjarne Stroustrup in 1979 at Bell Laboratories. The main goal was to
enhance the C programming language by adding object-oriented features while maintaining high
performance.
Initially, the language was named C with Classes because it introduced the concept of classes to
the C language. In 1983, it was renamed C++ (where ++ means increment), symbolizing an
improvement over C.
Over time, C++ evolved through various standardized versions:
C++98: First official standard
C++03: Minor improvements
C++11: Major update introducing modern features
C++14, C++17: Performance and library improvements
C++20: Advanced features like concepts and ranges
Each version improved safety, readability, and performance.
Example
Before C++, programmers used procedural code only. With C++, developers can create reusable
components using classes, improving software design.
3. Features of C++
C++ provides many features that make it suitable for professional software development.
Major Features
1. Object-Oriented Programming – supports classes, objects, inheritance, polymorphism,
encapsulation, and abstraction
2. High Performance – closer to hardware, fast execution
3. Portability – programs can run on multiple platforms
4. Rich Library Support – Standard Template Library (STL)
5. Memory Management – manual and dynamic memory control
6. Reusability – code reuse through functions and classes
Detailed Example
class Student {
public:
int id;
void display() {
cout << "Student ID: " << id;
}
};
Explanation:
This example demonstrates OOP in C++ where data and functions are combined in a class.
4. Applications of C++
C++ is used in a wide range of real-world applications due to its speed, efficiency, and
flexibility.
Areas of Application
Operating Systems (Windows, Linux kernels)
Game Development (game engines and graphics)
Embedded Systems (microcontrollers)
Database Systems
Scientific Simulations
Compilers and Interpreters
Example
C++ is used in game engines where real-time performance and memory control are critical.
5. Program Development Life Cycle
The Program Development Life Cycle (PDLC) describes the step-by-step process of developing
a program systematically.
Steps
1. Problem Analysis – Understand the problem clearly
2. Algorithm Design – Develop a logical solution
3. Flowchart/Pseudocode – Visual representation
4. Coding – Writing program in C++
5. Compilation – Check syntax errors
6. Execution – Run the program
7. Testing and Debugging – Remove errors
8. Documentation & Maintenance – Update and maintain
Detailed Example
Problem: Find sum of two numbers
Algorithm:
1. Start
2. Input two numbers
3. Add numbers
4. Display sum
5. End
This systematic approach ensures correctness and efficiency.
6. Structure of a C++ Program
A typical C++ program consists of the following components:
1. Header Files: Include necessary libraries using #include
2. Namespace: Usually using namespace std;
3. main() Function: Entry point of the program
4. Variable Declarations: Define data storage
5. Executable Statements: Logic of the program
6. Return Statement: Ends the program execution
Proper structure improves readability and maintainability.
Example
#include <iostream>
using namespace std;
int main() {
int a = 5;
cout << a;
return 0;
}
7. Tokens in C++
Tokens are the smallest meaningful units in a C++ program. They are classified into the
following types:
Keywords: Reserved words (int, float, if, else)
Identifiers: Names of variables and functions
Constants: Fixed values
Operators: Symbols that perform operations
Punctuators: Special symbols like ; , { }
Example
int sum = a + b;
int is a keyword, sum is an identifier, = and + are operators, and ; is a punctuator.
8. Keywords and Identifiers
In C++, keywords are reserved words that have special meaning to the compiler. These words
are predefined and cannot be used for naming variables, functions, or any other user-defined
entities. Examples of keywords include int, float, if, else, for, and while.
Identifiers are names given by the programmer to identify variables, functions, arrays, or
objects. Identifiers help make the program readable and understandable.
Rules for Identifiers
Must start with a letter or underscore
Cannot start with a number
Cannot be a keyword
No spaces allowed
Example
int marks; // marks is an identifier
float total; // total is an identifier
9. Variables and Constants
A variable is a named memory location used to store data that can change during program
execution. Variables allow programs to work with dynamic data. Each variable has a data type
that defines what kind of data it can store.
A constant is a fixed value whose value cannot be changed once it is defined. Constants are used
to store values that remain the same throughout the program, such as mathematical constants.
Importance
Variables make programs flexible
Constants improve program safety and readability
Example
int age = 20; // variable
const float PI = 3.14; // constant
10. Data Types in C++
Data types specify the type of data a variable can hold and how much memory it will occupy.
Choosing the correct data type improves memory usage and program efficiency.
Common Data Types
int: Stores whole numbers
float: Stores decimal numbers
double: Stores large decimal values
char: Stores a single character
bool: Stores true or false
Example
int number = 10;
float price = 99.5;
char grade = 'A';
bool isPass = true;
11. Type Modifiers
Type modifiers are used to modify the size and range of basic data types. They help control how
much memory a variable uses and what range of values it can store.
Common Type Modifiers
short: Uses less memory
long: Uses more memory
signed: Stores both positive and negative values
unsigned: Stores only positive values
Example
unsigned int count = 100;
long int population = 1000000;
12. Operators in C++
Operators are symbols that perform specific operations on operands such as variables and
constants. They are essential for performing calculations and making decisions.
Types of Operators
Arithmetic Operators: +, -, *, /, %
Relational Operators: <, >, <=, >=, ==, !=
Logical Operators: &&, ||, !
Assignment Operators: =, +=, -=
Example
int a = 10, b = 5;
int sum = a + b;
if(a > b) cout << "a is greater";
13. Input and Output Streams
Input and output streams are used to interact with the user. C++ uses stream-based input and
output through the iostream library.
cin is used to take input from the user
cout is used to display output on the screen
Example
int x;
cout << "Enter a number: ";
cin >> x;
cout << "You entered: " << x;
14. Control Structures
Control structures determine the order in which statements in a program are executed. They
allow the program to make decisions and repeat tasks.
Types of Control Structures
Selection (if, if-else, switch)
Iteration (loops)
Jump statements (break, continue)
Example
if(x > 0)
cout << "Positive number";
15. Decision Making (if, if-else, nested if)
Decision-making statements allow the program to execute different blocks of code based on
conditions. They are used when a program needs to make choices.
if executes code when condition is true
if-else provides two choices
nested if allows multiple conditions
Example
int marks = 75;
if(marks >= 80)
cout << "A+";
else if(marks >= 60)
cout << "A";
else
cout << "Fail";
16. Switch Statement
The switch statement selects one case from many options.
Example
switch(day) {
case 1: cout << "Monday"; break;
}
17. Looping Structures
Loops repeat statements multiple times.
Types:
for
while
do-while
18. for Loop
Used when the number of iterations is known.
Example
for(int i=1; i<=5; i++)
cout << i;
19. while Loop
Executes while condition is true.
Example
int i=1;
while(i<=5) {
cout << i;
i++;
}
20. do-while Loop
Executes at least once.
Example
int i=1;
do {
cout << i;
i++;
} while(i<=5);
21. Break and Continue Statements
break exits the loop
continue skips iteration
Example
if(i==3) break;
22. goto Statement
goto transfers control to another part of program (not recommended).
Example
goto label;
23. Arrays in C++
An array stores multiple values of same type.
Example
int arr[3] = {1,2,3};
24. One-Dimensional Arrays
Stores data in a single list.
Example
int marks[5];
25. Two-Dimensional Arrays
Stores data in table form.
Example
int mat[2][2];
26. Character Arrays and Strings
Used to store text.
Example
char name[10] = "Ali";
27. Functions in C++
Functions divide program into smaller parts.
Example
void show() { cout << "Hello"; }
28. User-Defined Functions
Functions created by programmer.
Example
int add(int a, int b) { return a+b; }
29. Function Prototypes
Function declaration before main.
Example
int add(int, int);
30. Parameter Passing Techniques
Methods of passing values to functions.
Types:
Call by value
Call by reference
Example
void fun(int &x) { x++; }