C++ Codes
C++ Codes
Introduction to C++
What is C++?
C++ is a general-purpose programming language created by Bjarne
Stroustrup as an extension of the C programming language. It
supports both procedural and object-oriented programming, making it
a versatile language.
Basic Syntax:
cpp
Copy code
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
Data Types:
Common data types include int (integer), float (floating-point
number), double (double-precision floating-point), char (character),
and bool (Boolean).
Example:
cpp
Copy code
int age = 25;
float height = 5.9;
char grade = 'A';
bool is Student = true;
Variable Declaration:
3. Operators
Arithmetic Operators: +, -, *, /, %
Relational Operators: ==, !=, >, <, >=, <=
Logical Operators: &&, ||, !
Example:
cpp
Copy code
int a = 10, b = 20;
int sum = a + b; // Addition
bool is Equal = (a == b); // Comparison
4. Control Structures
Conditional Statements:
o if, else if, else for branching logic.
Example:
cpp
Copy code
int number = 10;
if (number > 0) {
std::cout << "Positive number" << std::endl;
} else {
std::cout << "Non-positive number" << std::endl;
}
Loops:
for, while, and do-while loops for repeating actions.
Example:
cpp
Copy code
for (int i = 0; i < 5; i++) {
std::cout << "Count: " << i << std::endl;
}
5. Functions
Defining Functions:
o A function is a reusable block of code. Functions can take parameters
and return values.
Example:
cpp
Copy code
int add(int a, int b) {
return a + b;
}
int main() {
int sum = add(5, 3);
std::cout << "Sum: " << sum << std::endl;
return 0;
}
Function Overloading:
o You can define multiple functions with the same name but different
parameters.
Arrays:
o An array is a collection of elements of the same type.
Example:
cpp
Copy code
int numbers[5] = {1, 2, 3, 4, 5};
std::cout << "First number: " << numbers[0] << std::endl;
Strings:
o Strings in C++ are arrays of characters terminated with a null
character (\0), or they can be managed using the std::string class.
Example:
cpp
Copy code
std::string name = "Alice";
std::cout << "Name: " << name << std::endl;
int main() {
Dog myDog;
myDog.name = "Rex";
myDog.bark();
return 0;
}
Pointers:
A pointer is a variable that holds the memory address of another
variable.
Example:
cpp
Copy code
int value = 10;
int *ptr = &value;
std::cout << "Value: " << *ptr << std::endl;
Dynamic Memory Allocation:
o Using new and delete to allocate and free memory dynamically.
Containers:
o Vectors, Lists, Maps, etc., that help manage collections of data.
Iterators:
o Objects that allow traversing through elements in containers.
Algorithms:
o Functions like sort, find, reverse, etc., for common operations on
containers.
9. Advanced Topics
File Handling:
o Reading from and writing to files using streams.
Exception Handling:
o Using try, catch, and throw to manage errors gracefully.
Templates:
o Creating generic functions and classes.
Multithreading:
o Running multiple threads in parallel.
Resources for Learning C++
Books:
o "Programming: Principles and Practice Using C++" by Bjarne
Stroustrup.
o "The C++ Programming Language" by Bjarne Stroustrup.
Online Tutorials:
o Codecademy
o GeeksforGeeks
Practice:
o LeetCode, HackerRank, and Codewars for coding challenges.