WikiGalaxy

Personalize

C++ Introduction

What is C++?

C++ is a general-purpose programming language created by Bjarne Stroustrup as an extension of the C programming language. It is known for its versatility and performance, making it popular in software development.


#include 
using namespace std;

int main() {
    cout << "Welcome to C++ Programming!";
    return 0;
}
    

Console Output:

Welcome to C++ Programming!

Basic Syntax

Understanding C++ Syntax

C++ syntax is similar to C but with added features like classes and objects. It supports procedural, object-oriented, and generic programming paradigms.


#include 
using namespace std;

int main() {
    int number = 10;
    cout << "The number is: " << number;
    return 0;
}
    

Console Output:

The number is: 10

Variables and Data Types

Defining Variables

Variables are used to store data in C++. They have a type that defines the kind of data they can hold, such as int, float, char, etc.


#include 
using namespace std;

int main() {
    int age = 25;
    float salary = 50000.50;
    char grade = 'A';
    cout << "Age: " << age << ", Salary: " << salary << ", Grade: " << grade;
    return 0;
}
    

Console Output:

Age: 25, Salary: 50000.50, Grade: A

Control Structures

Conditional Statements

C++ supports conditional statements like if, else if, and else to control the flow of the program based on conditions.


#include 
using namespace std;

int main() {
    int number = 5;
    if (number > 0) {
        cout << "The number is positive.";
    } else {
        cout << "The number is not positive.";
    }
    return 0;
}
    

Console Output:

The number is positive.

Loops

Using Loops in C++

Loops are used to execute a block of code repeatedly. The most common loops in C++ are for, while, and do-while loops.


#include 
using namespace std;

int main() {
    for (int i = 1; i <= 5; i++) {
        cout << "Iteration: " << i << endl;
    }
    return 0;
}
    

Console Output:

Iteration: 1

Iteration: 2

Iteration: 3

Iteration: 4

Iteration: 5

Functions

Defining Functions

Functions are blocks of code that perform a specific task. They help in making the code modular and reusable.


#include 
using namespace std;

void greet() {
    cout << "Hello, welcome to C++!";
}

int main() {
    greet();
    return 0;
}
    

Console Output:

Hello, welcome to C++!

Arrays

Working with Arrays

Arrays are used to store multiple values of the same type in a single variable. They are useful for managing collections of data.


#include 
using namespace std;

int main() {
    int numbers[5] = {1, 2, 3, 4, 5};
    for (int i = 0; i < 5; i++) {
        cout << "Number: " << numbers[i] << endl;
    }
    return 0;
}
    

Console Output:

Number: 1

Number: 2

Number: 3

Number: 4

Number: 5

Pointers

Understanding Pointers

Pointers are variables that store memory addresses. They are powerful tools in C++ for dynamic memory management and efficient data handling.


#include 
using namespace std;

int main() {
    int value = 42;
    int* ptr = &value;
    cout << "Value: " << value << ", Pointer: " << ptr << ", Dereferenced: " << *ptr;
    return 0;
}
    

Console Output:

Value: 42, Pointer: 0x7ffee3b7f9c4, Dereferenced: 42

Classes and Objects

Object-Oriented Programming

C++ supports object-oriented programming, allowing developers to create classes and objects to model real-world entities and relationships.


#include 
using namespace std;

class Car {
public:
    string brand;
    int year;
    void display() {
        cout << "Brand: " << brand << ", Year: " << year;
    }
};

int main() {
    Car car1;
    car1.brand = "Toyota";
    car1.year = 2020;
    car1.display();
    return 0;
}
    

Console Output:

Brand: Toyota, Year: 2020

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025