1. Introduction to C++
C++ is a general-purpose programming language developed by Bjarne Stroustrup as an
extension of the C language. It supports procedural, object-oriented, and generic
programming.
Developed by: Bjarne Stroustrup
Year: 1983
Paradigm: Multi-paradigm (procedural, object-oriented, generic)
Compilation: Compiled language, typically using compilers like GCC, MSVC, etc.
2. Basic Syntax
A simple C++ program structure:
cpp
Copy code
#include <iostream> // Header file for input-output
int main() {
std::cout << "Hello, World!"; // Print to console
return 0; // Return statement
}
Key elements:
#include <iostream>: This is a preprocessor directive that includes the I/O library.
int main(): Entry point of every C++ program.
std::cout: Output stream to print data.
return 0;: Indicates successful program termination.
3. Data Types
C++ supports various built-in data types:
Data Type Description Size
int Integer numbers 4 bytes
float Floating-point 4 bytes
double Double precision 8 bytes
char Character 1 byte
bool Boolean (true/false) 1 byte
void Empty 0 bytes
4. Variables and Constants
Variables are used to store data for processing. Syntax to declare variables:
, cpp
Copy code
int age = 25; // Integer variable
float height = 5.9; // Float variable
Constants are immutable values:
cpp
Copy code
const int PI = 3.14;
5. Operators
Arithmetic Operators
Operator Example Description
+ a + b Addition
- a - b Subtraction
* a * b Multiplication
/ a / b Division
% a % b Modulo (remainder)
Relational Operators
Operator Example Description
== a == b Equal to
!= a != b Not equal to
> a > b Greater than
< a < b Less than
Logical Operators
Operator Example Description
&& a && b Logical AND
` `
! !a Logical NOT
6. Control Flow
Conditional Statements
1. if statement:
cpp
Copy code
if (condition) {
// code block
}
2. else statement: