C++ - Data Types
Chapter 4: Data Types
In this chapter, we will learn about data types in C++. We will start by learning about the basic data types in C++ and how to use them. Data types are used to define the type of data that a variable can hold. They are an important concept in programming and are used to ensure that the data is stored and used correctly.
Basic Data Types
C++ provides several basic data types that are used to define the type of data that a variable can hold. Some of the basic data types in C++ include:
- Primitive Data Types: These are the basic data types that are used to define the type of data that a variable can hold. Some of the primitive data types in C++ include:
- Derived Data Types: These are the data types that are derived from other data types.
- User-Defined Data Types: These are the data types that are defined by the user.
In this chapter, we will focus on the primitive data types and how to use them.
Primitive Data Types
C++ provides several primitive data types that are used to define the type of data that a variable can hold. Some of the primitive data types in C++ include:
int
: This is a signed integer data type that can hold values from -2147483648 to 2147483647.float
: This is a floating-point data type that can hold values with a precision of 6 decimal places.double
: This is a floating-point data type that can hold values with a precision of 15 decimal places.char
: This is a character data type that can hold a single character.bool
: This is a boolean data type that can hold eithertrue
orfalse
.void
: This is a data type that is used to indicate that a function does not return a value.
We have some additional data types that are primitives but are less discussed from academic perspective. These include:
short
: This is a signed integer data type that can hold values from -32768 to 32767.long
: This is a signed integer data type that can hold values from -2147483648 to 2147483647.long long
: This is a signed integer data type that can hold values from -9223372036854775808 to 9223372036854775807.unsigned char
: This is an unsigned integer data type that can hold values from 0 to 255.unsigned short
: This is an unsigned integer data type that can hold values from 0 to 65535.unsigned int
: This is an unsigned integer data type that can hold values from 0 to 4294967295.unsigned long
: This is an unsigned integer data type that can hold values from 0 to 4294967295.unsigned long long
: This is an unsigned integer data type that can hold values from 0 to 18446744073709551615.
Code used in video - Data Types
Going through the code
- The
int teaLeaves = 50;
line declares a variable namedteaLeaves
of typeint
and assigns the value50
to it. - The
float waterTemperature = 85.588588;
line declares a variable namedwaterTemperature
of typefloat
and assigns the value85.588588
to it. - The
double priceOfTea = 299.99;
line declares a variable namedpriceOfTea
of typedouble
and assigns the value299.99
to it. - The
char teaGrade = 'A';
line declares a variable namedteaGrade
of typechar
and assigns the value'A'
to it. - The
bool isTeaReady = false;
line declares a variable namedisTeaReady
of typebool
and assigns the valuefalse
to it. - The
cout << waterTemperature << endl;
line prints the value of thewaterTemperature
variable to the console.
Modifier code in video
Going through the code
- The
unsigned smallTeaPack = 1200;
line declares a variable namedsmallTeaPack
of typeunsigned
and assigns the value1200
to it. - The
long long largeTeaStorage = 100000000;
line declares a variable namedlargeTeaStorage
of typelong long
and assigns the value100000000
to it. - The
short teaSample = 25;
line declares a variable namedteaSample
of typeshort
and assigns the value25
to it. - The
cout << largeTeaStorage << endl;
line prints the value of thelargeTeaStorage
variable to the console.
String code in video
Going through the code
- The
string favoriteTea = "Lemon Tea \t";
line declares a variable namedfavoriteTea
of typestring
and assigns the value"Lemon Tea \t"
to it. - The
string description = "Known as \"best\" tea";
line declares a variable nameddescription
of typestring
and assigns the value"Known as \"best\" tea"
to it. - The
cout << favoriteTea << description << endl;
line prints the value of thefavoriteTea
anddescription
variables to the console.
Type Casting code in video
Type casting is the process of converting a value from one data type to another data type.
Going through the code
- The
float teaPrice = 49.99;
line declares a variable namedteaPrice
of typefloat
and assigns the value49.99
to it. - The
int roundedTeaPrice = (int) teaPrice;
line declares a variable namedroundedTeaPrice
of typeint
and assigns the value49
to it. - The
int teaQuantity = 2;
line declares a variable namedteaQuantity
of typeint
and assigns the value2
to it. - The
int totalPrice = teaPrice * teaQuantity;
line declares a variable namedtotalPrice
of typeint
and assigns the value98
to it. - The
cout << totalPrice << endl;
line prints the value of thetotalPrice
variable to the console.
User input code in video
Going through the code
#include <string>
line includes thestring
header file, which provides thestring
data type.- The
string userTea;
line declares a variable nameduserTea
of typestring
and does not assign a value to it. - The
int teaQuantity;
line declares a variable namedteaQuantity
of typeint
and does not assign a value to it. - The
cout << "What would you like to order in tea? \n";
line prints the string “What would you like to order in tea?” to the console. - The
getline(cin, userTea);
line reads a line of input from the console and assigns it to theuserTea
variable. - The
cout << "how many cups of " << userTea << "would you like to have ? ";
line prints the string “how many cups of ” followed by the value of theuserTea
variable followed by the string “would you like to have ? ” to the console. - The
cin >> teaQuantity;
line reads an integer from the console and assigns it to theteaQuantity
variable. - The
cout << teaQuantity;
line prints the value of theteaQuantity
variable to the console. - The
cout << userTea;
line prints the value of theuserTea
variable to the console.
Summary
In this chapter, we have learned about data types in C++. We have also learned about the primitive data types and how to use them. By the end of this chapter, you should have a good understanding of how to use data types in C++.
Keep practicing and exploring more data types in C++. Happy coding!