C++ Tutorial for Beginners - Learn C++ in 1 Hour
Introduction to C++
- General-purpose programming language developed by Bjarne Stroustrup.
- Extension of C with object-oriented programming features.
- Used in system programming, game development, embedded systems, and high-performance
applications.
Basic Syntax
- Every C++ program must have a main() function.
- Statements end with a semicolon (;).
- Example:
- #include <iostream>
- using namespace std;
-
- int main() {
- cout << "Hello World!";
- return 0;
- }
Variables and Data Types
- Primitive types: int, float, double, char, bool.
- Declaration example: int age = 20;
- Constants: const double PI = 3.14;
Operators
- Arithmetic: +, -, *, /, %
- Relational: ==, !=, <, >, <=, >=
- Logical: &&, ||, !
- Assignment: =, +=, -=, *=, /=
Control Flow
- If-else statement for conditional execution.
- Switch-case for multiple condition branches.
- Loops:
- - for loop
- - while loop
- - do-while loop
Functions
- Functions help in modularity and reusability.
- Definition example:
- int add(int a, int b) {
- return a + b;
- }
- Call by value vs. call by reference.
Arrays
- Collection of elements of the same type.
- Example: int numbers[5] = {1, 2, 3, 4, 5};
Pointers
- Store memory addresses.
- Example:
- int x = 10;
- int* ptr = &x;
- cout << *ptr; // prints 10
Object-Oriented Programming Basics
- Classes and Objects:
- class Car {
- public:
- string brand;
- void drive() { cout << "Driving"; }
- };
- Core principles: Encapsulation, Inheritance, Polymorphism.