C++ Basics
1. Basic Structure of a C++ Program:
A simple C++ program includes the following elements:
#include <iostream> // Preprocessor directive for input/output operations
int main() { // Main function where execution begins
std::cout << "Hello, World!"; // Output statement
return 0; // Return 0 indicates successful execution
}
2. Comments:
Comments help explain the code and are ignored by the compiler.
- Single-line comment: // This is a comment
- Multi-line comment: /* This is a multi-line comment */
3. Data Types:
C++ provides several basic data types to represent different types of data:
- int: Integer type (e.g., int x = 5;)
- float: Floating-point type (e.g., float y = 5.99;)
- double: Double precision floating-point type (e.g., double z = 19.99;)
- char: Character type (e.g., char letter = 'A';)
- bool: Boolean type (e.g., bool isCodingFun = true;)
4. Variables and Constants:
Variables are used to store data. They must be declared with a type before use.
int x = 10; // Declare and initialize an integer variable
const int y = 20; // Declare a constant that cannot be changed
5. Input and Output:
- Input: std::cin is used for input.
int age;
std::cin >> age; // Get user input
- Output: std::cout is used for output.
std::cout << "Hello, World!";
6. Operators:
C++ provides various operators for performing operations on data:
- Arithmetic Operators: +, -, *, /, %
- Assignment Operator: =
- Comparison Operators: ==, !=, <, >, <=, >=
- Logical Operators: && (AND), || (OR), ! (NOT)
7. Control Structures:
- Conditional Statements:
- if, else if, else
if (x > 5) {
std::cout << "x is greater than 5";
} else {
std::cout << "x is not greater than 5";
}
- Switch Statements:
switch (x) {
case 1: std::cout << "x is 1"; break;
case 2: std::cout << "x is 2"; break;
default: std::cout << "x is not 1 or 2";
}
- Loops:
- for loop:
for (int i = 0; i < 5; i++) {
std::cout << i << " ";
}
- while loop:
int i = 0;
while (i < 5) {
std::cout << i << " ";
i++;
}
- do-while loop:
int i = 0;
do {
std::cout << i << " ";
i++;
} while (i < 5);
8. Functions:
Functions allow you to organize your code into reusable blocks.
int add(int a, int b) {
return a + b;
}
int main() {
int sum = add(5, 3);
std::cout << "Sum: " << sum;
return 0;
}
9. Arrays:
Arrays store multiple values of the same type in a single variable.
int numbers[5] = {1, 2, 3, 4, 5};
std::cout << numbers[0]; // Outputs the first element
10. Vectors:
Vectors are dynamic arrays provided by the C++ Standard Library.
#include <vector>
std::vector<int> numbers = {1, 2, 3, 4, 5};
numbers.push_back(6); // Add an element at the end
std::cout << numbers[5]; // Outputs 6
11. Pointers:
Pointers store the memory address of a variable.
int x = 10;
int* ptr = &x; // Store the address of x in ptr
std::cout << *ptr; // Outputs the value of x (dereferencing)
12. Classes and Objects:
C++ is an Object-Oriented Programming (OOP) language, allowing the use of
classes and objects.
class Car {
public:
std::string brand;
int year;
void display() {
std::cout << "Brand: " << brand << ", Year: " << year;
}
};
int main() {
Car myCar;
myCar.brand = "Toyota";
myCar.year = 2020;
myCar.display();
return 0;
}
13. File I/O:
You can read from and write to files using fstream:
#include <fstream>
int main() {
std::ofstream myfile("example.txt");
myfile << "Writing to file";
myfile.close();
std::ifstream readfile("example.txt");
std::string line;
while (getline(readfile, line)) {
std::cout << line;
}
readfile.close();
return 0;
}
14. Standard Template Library (STL):
STL provides a collection of generic classes and functions such as vector, map,
queue, etc., for common data structures and algorithms.
15. Namespaces:
Namespaces prevent name conflicts in large programs. The standard library uses
the std namespace.
using namespace std;