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.
#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.