C++ Programming Tutorial

Introduction to C++

C++ is a powerful general-purpose programming language created by Bjarne Stroustrup as an extension of the C programming language. It offers both high-level and low-level programming features.

#include <iostream>

int main() {
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

C++ Basics

Learn the fundamental concepts of C++ programming, including syntax, structure, and basic operations.

#include <iostream>
using namespace std;

int main() {
    // This is a comment
    int number = 42;
    cout << "The number is: " << number << endl;
    return 0;
}

Variables & Data Types

C++ supports various data types including integers, floating-point numbers, characters, and more.

int age = 25;                  // Integer
double price = 99.99;          // Double
char grade = 'A';              // Character
bool isStudent = true;         // Boolean
string name = "John";          // String

Control Flow

Control flow statements allow you to control the execution flow of your program.

int number = 10;

if (number > 0) {
    cout << "Positive number" << endl;
} else if (number < 0) {
    cout << "Negative number" << endl;
} else {
    cout << "Zero" << endl;
}

// Loop example
for (int i = 0; i < 5; i++) {
    cout << i << " ";
}

Functions

Functions are blocks of code that perform specific tasks and can be reused throughout your program.

// Function declaration
int add(int a, int b) {
    return a + b;
}

// Function usage
int result = add(5, 3);    // result = 8

Classes & Objects

C++ is an object-oriented programming language that uses classes and objects to create complex programs.

class Student {
private:
    string name;
    int age;
    
public:
    Student(string n, int a) {
        name = n;
        age = a;
    }
    
    void display() {
        cout << "Name: " << name << endl;
        cout << "Age: " << age << endl;
    }
};