• Home
  • JavaScript
    • Introduction to JavaScript
    • JavaScript Developer
  • TypeScript
    • Introduction to TypeScript
    • TypeScript Developer
  • React.js
    • Introduction to React
    • React Developer
  • Python
    • Introduction to Python
    • Python Developer
  • C++
    • Introduction to C++
    • C++ Developer
  • C Language
    • Introduction to C
    • C Developer
  • Rust
    • Introduction to Rust
    • Rust Developer
  • Zig
    • Introduction to Zig
    • Zig Developer
  • Linux Shell
    • Introduction to the Linux Shell
    • Linux Shell Developer
  • Interactive Training
  • Pricing
  • Brainstorm
STEMTrainingGrounds
  • Courses
    • Home
    • JavaScript
      • Introduction to JavaScript
      • JavaScript Developer
    • TypeScript
      • Introduction to TypeScript
      • TypeScript Developer
    • React
      • Introduction to React
      • React Developer
    • Python
      • Introduction to Python
      • Python Developer
    • C++
      • Introduction to C++
      • C++ Developer
    • C Language
      • Introduction to C
      • C Developer
    • Rust
      • Introduction to Rust
      • Rust Developer
    • Zig
      • Introduction to Zig
      • Zig Developer
    • Linux Shell
      • Introduction to the Linux Shell
      • Linux Shell Developer
  • Interactive Training
  • Pricing
  • Reading Grounds

Quick Links

  • About Us
  • Pricing
  • Partnership
  • Brainstorm
  • Terms
  • Privacy
  • Refunds

Courses

  • JavaScript
    • Introduction to JavaScript
    • JavaScript Developer
  • TypeScript
    • Introduction to TypeScript
    • TypeScript Developer
  • React
    • Introduction to React
    • React Developer
  • Python
    • Introduction to Python
    • Python Developer
  • C++
    • Introduction to C++
    • C++ Developer
  • C Language
    • Introduction to C
    • C Developer
  • Rust
    • Introduction to Rust
    • Rust Developer
  • Zig
    • Introduction to Zig
    • Zig Developer
  • Linux Shell
    • Introduction to the Linux Shell
    • Linux Shell Developer

Newsletter

Subscribe to our free monthly newsletter, for a quick update on Python, JavaScript, and React news

© 2025 - 2026 STEMTrainingGrounds. All Rights Reserved.

C++ Developer -

Lesson 2 of 22

Lesson Progress: 0%

Lesson Progress: 0%
Lesson Incomplete
← Previous: Output With cout
Lesson 2 of 22
Next: Basic Data Types →

A variable in C++ is a named container that stores data. You can think of it as a labeled box that holds a value. Every variable has a type, which tells C++ what kind of data it can hold. Common types include int for whole numbers and string for text. You give a variable a name and a value when you declare it. The value can be changed later if needed. Variables let you store, reuse, and manipulate data throughout your program. This lesson will show you how to create and use variables in C++.

Code Example
#include <iostream>
using namespace std;

int main() {
    int score = 95;
    cout << score;
    return 0;
}

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Cpp Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • int score = 95; declares an integer variable named score and gives it the value 95.
  • The type int tells C++ that this variable will hold a whole number.
  • The equal sign = assigns the value on the right to the variable on the left.
  • cout << score; prints the value stored in the variable.
  • The output is 95, which is the value of score.
Code Example
#include <iostream>
using namespace std;

int main() {
    string name = "Alice";
    int age = 12;
    cout << name << " is " << age;
    return 0;
}

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Cpp Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • You can declare multiple variables of different types in one program.
  • string name = "Alice"; creates a text variable and sets it to Alice.
  • int age = 12; creates a number variable and sets it to 12.
  • cout << name << " is " << age; prints both variables together.
  • The output is Alice is 12.
Code Example
#include <iostream>
using namespace std;

int main() {
    int points = 10;
    points = 25;
    cout << points;
    return 0;
}

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Cpp Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • A variable can be changed after it is declared by assigning a new value.
  • int points = 10; creates the variable and sets it to 10.
  • points = 25; changes the value to 25, replacing the old value.
  • When you print points, it shows the current value, which is 25.
  • The old value 10 is gone after the reassignment.
Code Example
#include <iostream>
using namespace std;

int main() {
    int a = 15;
    int b = 4;
    cout << a + b;
    return 0;
}

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Cpp Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • Variables can be used in arithmetic expressions.
  • int a = 15; and int b = 4; create two variables.
  • cout << a + b; adds their values and prints the result.
  • The expression a + b is evaluated to 19 before printing.
  • You can use variables anywhere you would use a plain number.
Code Example
#include <iostream>
using namespace std;

int main() {
    int length = 8;
    int width = 5;
    cout << length * width;
    return 0;
}

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Cpp Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • Variables make it easy to perform calculations with named values.
  • int length = 8; and int width = 5; store the dimensions.
  • cout << length * width; multiplies them to find the area.
  • The expression length * width gives 40.
  • Using variables makes the code easier to read and change later.
Code Example
#include <iostream>
using namespace std;

int main() {
    string city = "Paris";
    string country = "France";
    cout << city << ", " << country;
    return 0;
}

Instructions

▼ ← Click the triangle to hide or reveal instructions.

Cpp Code Editor

Task Incomplete

Editor Input:

Editor Output:

Click "Run Code" to see the output here
  • Variables can hold text using the string type.
  • string city = "Paris"; stores the text Paris.
  • string country = "France"; stores the text France.
  • cout << city << ", " << country; prints both strings with a comma between them.
  • The output is Paris, France.

Test Incomplete

What is a variable in C++?

Question #

1/15

Score

0/0 - 0.0 %