C++ Types
This page says what kind of value each box can hold. The input page ended with pine, then cone, the word you typed, and the program read that word into second. That two-line screen is the step this page builds on. You can still see the same shape of result, two words under each other, and the same comment above the work. The first box still holds the word pine, and the comment // the word stays pine still sits above the declarations. The compiler is already on the machine, and the build command still uses g++ -std=c++17.
This module changes the example: pine.cpp becomes types.cpp, because four boxes of four types do not fit the two-name file from the earlier pages. The file has no keyboard read on this page. Every box gets its value in the file itself.
A type is the kind of value a box can keep. The line int count = 2; says that count is an int, which holds whole numbers, and puts 2 into it. The line double ratio = 0.5; says that ratio is a double, which holds decimal numbers. The line bool ready = true; says that ready is a bool, which holds true or false. The line std::string word = "pine"; says that word is a std::string, which holds text. The type comes first, then the name, then an equals sign, then the value, the same shape as the declarations on the variables page.
Each type keeps a different kind of value, and the compiler checks the type when it builds the file. int, double, and bool are built into C++. std::string comes from the standard library, so the file keeps #include
A type also tells you what you can do with a box. A double can take part in division with decimals, and an int divides whole numbers only. The next pages use that difference, so the type you choose now matters later. This page only names the types and prints them, and it does no arithmetic.
Create types.cpp in the folder where pine.cpp used to be. Keep the comment above the declarations and keep main as a free function with its braces. Each box gets one declaration and one writing line, in the order shown below. The checked file is below. The file name on disk is types.cpp, and the program the build creates is named types, so the run command is ./types.
#include <iostream>
#include <string>
int main()
{
// the word stays pine
std::string word = "pine";
int count = 2;
double ratio = 0.5;
bool ready = true;
std::cout << word << std::endl;
std::cout << count << std::endl;
std::cout << ratio << std::endl;
std::cout << ready << std::endl;
return 0;
}
Rebuild with g++ -std=c++17 types.cpp -o types and then run ./types. If g++ is missing and clang++ is the compiler you installed, swap only the tool name: clang++ -std=c++17 types.cpp -o types. The screen shows four lines: pine, then 2, then 0.5, then 1. Each line comes from one writing line, and each writing line prints one box. The first line is the text box. The second is the whole number. The third is the decimal. The fourth is the bool.
Count the lines against the file, not against memory. The first writing line prints word, the second prints count, the third prints ratio, and the fourth prints ready. If a line is missing, check the writing line for that box first, then check that its declaration sits above it.
The table lists what each box holds and what the screen shows for it. Read it once, then compare your run with it. The bool line is the one most people do not expect, so the table gives it its own row. Each row names the type first, then the value, then the screen result. A row that disagrees with your run points to one line in the file, and that line is the place to look first.
📊 Four boxes and four lines
| Piece | What it holds |
|---|---|
| Word | pine, a std::string of text |
| Count | 2, an int of whole numbers |
| Ratio | 0.5, a double of decimals |
| Ready | 1, a bool printed as a number |
The bool line shows 1 and not the word true, because std::cout prints a bool as a number by default. The source says true, and the box holds true, but the screen shows 1. The bool does not print the word true unless the program asks for that form. This page does not ask for it, so 1 is the correct screen result for ready. A bool holds exactly two values, true and false. Printed as a number, true becomes 1 and false becomes 0. The screen is a number here, and the box is still a yes or no value.
Now change the count line to int count = 2.7; and rebuild. The screen shows 2 on the second line, not 2.7. An int has no place for the part after the decimal point, so the value is cut down to 2 when it is stored. The decimal box still keeps 0.5 exactly, because it is a double. Put the line back to 2 and the screen returns to 2. Every change gives a screen result you can explain from its type. The compiler may warn about the change, depending on the flags you use, but the default build still runs the file. Read any warning as a note about the line you just changed, and do not treat it as a new lesson. The checked file keeps int count = 2; so the four lines stay pine, 2, 0.5, and 1.
The next page is strings. Its job is to work with text: how long a string is, how to join two strings, and how to find a letter inside one. This page stops before any text method. You leave with four boxes of four types, the screen showing four lines, and no method call yet. Count the lines on the screen before you leave. There are four, and the last one is 1. Keep the file as the checked version, with pine in the first box and the bool in the last one, so the next page starts from the same four boxes. The next page does not change the types on these lines.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top