What is C++?
• General-purpose programming language
• Object-oriented programming language
• Extension of the C programming language
• Compiled language that produces efficient, high-performance applications
• Supports both procedural and object-oriented programming paradigms
History and Evolution
• Developed by Bjarne Stroustrup at Bell Labs in 1979
• Originally called "C with Cla"sses
• Renamed to C++ in 1983 (++ is the increment operator in C)
• Major standards:
• C++98/03 (First standardization)
• C++11 (Major update with modern features)
• C++14, C++17, C++20, C++23 (Continued evolution)
Key Features of C++
• Object-oriented programming
• Strong typing
• Memory management control
• Compile-time polymorphism (function overloading)
• Runtime polymorphism (virtual functions)
• Templates for generic programming
• Standard Template Library (STL)
• Exception handling
• Operator overloading
• Multiple inheritance
Basic Structure of a C++ Program
// This is a comment
#include <iostream> // Preprocessor directive
int main() { // Main function - program execution starts here
// Program statements
std::cout << "Hello, World!"; // Output statement
return 0; // Return statement }
Components Explained
• Preprocessor directives: Instructions for the compiler (begin with #)
• Comments: Notes for humans, ignored by compiler
• main() function: Entry point of every C++ program
• Statements: Instructions for the program to execute
• Braces {}: Define blocks of code
• return statement: Indicates program execution status
Data Types in C++
• Fundamental Data Types:
• int: Integer values (-2147483648 to 2147483647)
• float: Single precision floating point (7 digits precision)
• double: Double precision floating point (15 digits precision)
• char: Single character ('A', 'z', '5')
• bool: Boolean values (true or false)
• Type modifiers: short, long, unsigned, signed
• User-defined types: structs, classes, enums
Variables
• Named storage locations in memory
• Must be declared before use
• Syntax: dataType variableName = initialValue;
Examples:
int age = 20;
double salary = 50000.50;
char grade = 'A’;
bool isEnrolled = true;
Basic Input/Output
#include <iostream>
int main() {
std::cout << "Hello, C++!";
std::cout << "Value: " << 42;
return 0;
#include <iostream>
int main() {
int number;
std::cout << "Enter a number: ";
std::cin >> number;
std::cout << "You entered: " << number;
return 0;
}
Using namespace std: Simplifies code by removing need for std:: prefix
using namespace std; // Now we can use cout instead of std::cout
Compilation Process
• Write code in a text editor
• Save with .cpp extension
• Compiler translates source code to object code
• Linker combines object code with libraries
• Executable file is created
• Program can now be run
Basic Operators
• Arithmetic: +, -, *, /, %
• Relational: ==, !=, >, <, >=, <=
• Logical: &&, ||, !
• Assignment: =, +=, -=, *=, /=, %=
• Increment/Decrement: ++, --
• Bitwise: &, |, ^, ~, <<, >>
Control Structures
• Conditional Statements:
• if, if-else, switch
• Loops:
• for, while, do-while
• Jump Statements:
• break, continue, goto, return
What is a Variable?
• Definition: A variable is a named storage location in memory that can hold a value.
• Purpose: To store data that can be used and manipulated in a program
Variable Declaration
Syntax:
data_type variable_name;
Example:
int age;
Variable Initialization
• Definition: Assigning a value to a variable at the time of declaration.
Example: int age = 25;
Data Types in C++
Common Data Types:
• int – integer (whole numbers)
• float – floating point number (decimals)
• double – double precision floating point number
• char – character (single character)
• bool – boolean (true or false)
• string – sequence of characters (text)
Variable Naming Conventions
Rules:
• Must start with a letter or underscore (_)
• Can contain letters, numbers, and underscores
• Case sensitive. “age” and “Age” are different.
Best Practices:
- Use meaningful names (e.g. “studentAge” instead of “a”)
Scope of Variables
Definition:
The region of the program where a variable is accessible.
Types:
Local Variables: Declared inside a function or block.
Global Variables: Declared outside all functions
Constants
- a variable whose value cannot be changed after initialization.
• Syntax:
- const data_type constant_name = value;
Example
- const float PI = 3.14;
Introduction to Loops
• What are Loops?
• Loops are control structures that allow you to repeat a block of code multiples
times
• Essential for reducing code repetition and handling repetitive tasks
• Three main types of loops in C++:
1. for loop
2. while loop
3. do…while loop
For Loop - Basic Structure
Components explained:
o Initialization: Sets the loop variable initial value
o Condition: Determines how long the loop continues
o Increment/Decrement: Changes the loop variable after each iteration