Data TypesLesson 7
Author : Mike Dane
Last Updated : November, 2017
As we write more and more complex programs in C++, we'll want to work with different types of data (data types). Maybe in one program we'll print out someone's favorite movie, in another we'll keep track of how much money a particular product costs, and maybe in another we'll ask someone if they're an organ donor. In everyday life we deal with all different types of data, so why should it be any different for programs?
Many programming languages, including C++ allow you to work with and keep track of only a specific few types of data. Now that might not sound like a lot, but when you think about it, almost all data can be broken down into one of three data types.
- Text (often called strings or chars) - Names, addresses, the text on this page, etc.
- Numbers - Monetary amounts, dates, distances, etc.
- integers - whole numbers and
- floats (also called doubles) - decimal numbers
- True/False Values (often called booleans) - If someone is an Amazon Prime member, if someone is in the military, if a tv show has been cancelled, etc. This type of data may be less intuitive to a new programmer, but trust me you'll learn to love true/false logic.
Now because these are three distinct types of information, C++ is going to handle them a bit differently. There will be certain things we can do with numbers that we can't do with strings or text (like division, multiplication, etc). There will also be a whole range of things we can do with boolean values (true/false) that we can't do with strings or numbers.
Anytime you're writing a program that's keeping track of different pieces of information, you always want to be aware of what data types you're using. For more specific information about how these data types work, and how to use them in C++, check out the video above where we'll dive into them more specifically!
// #include <string> string name = "Mike"; // string of characters, not primitive char testGrade = 'A'; // single 8-bit character. // you can make them unsigned by adding "unsigned" prefix short age0 = 10; // atleast 16-bits signed integer int age1 = 20; // atleast 16-bits signed integer (not smaller than short) long age2 = 30; // atleast 32-bits signed integer long long age3 = 40; // atleast 64-bits signed integer float gpa0 = 2.5f; // single percision floating point double gpa1 = 3.5; // double-precision floating point long double gpa2 = 3.5; // extended-precision floating point bool isTall; // 1 bit -> true/false isTall = true; name = "John"; cout << "Your name is " << name << endl;