← C++ tutorial

C++

Variables & Data Types

Like Java, C++ variables need an explicit type: int age = 21;.

Example: string, int, and output together

#include <iostream>
#include <string>
using namespace std;

int main() {
    string name = "Ada";
    int age = 21;
    cout << name << " is " << age << " years old." << endl;
    return 0;
}
Ada is 21 years old.

Common types: int, double, bool, and string (from the <string> header, notice it needs its own separate include, unlike iostream). Chaining multiple << together, as above, is the standard C++ way to build up a printed line from several separate pieces.

Example
#include <iostream>
#include <string>
using namespace std;

int main() {
    string name = "Ada";
    int age = 21;
    cout << name << " is " << age << " years old." << endl;
    return 0;
}
Output
Ada is 21 years old.
Ready to go further?

These free tutorials are a preview of what a Code Secure Academy cohort covers, project work and all.

Explore our tracks
Don't miss out

Ready to build something real?

Create a free account to enroll in a track, and get notified the moment a cohort opens.

Create free account