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 eithertrueorfalse.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
#include <iostream>using namespace std;
int main(){ int teaLeaves = 50; float waterTemperature = 85.588588; double priceOfTea = 299.99; char teaGrade = 'A'; bool isTeaReady = false;
cout << waterTemperature << endl;
return 0;}Let’s go through the code line by line:
- The
int teaLeaves = 50;line declares a variable namedteaLeavesof typeintand assigns the value50to it. - The
float waterTemperature = 85.588588;line declares a variable namedwaterTemperatureof typefloatand assigns the value85.588588to it. - The
double priceOfTea = 299.99;line declares a variable namedpriceOfTeaof typedoubleand assigns the value299.99to it. - The
char teaGrade = 'A';line declares a variable namedteaGradeof typecharand assigns the value'A'to it. - The
bool isTeaReady = false;line declares a variable namedisTeaReadyof typebooland assigns the valuefalseto it. - The
cout << waterTemperature << endl;line prints the value of thewaterTemperaturevariable to the console.
Modifier Code in Video
#include <iostream>using namespace std;
int main(){ unsigned smallTeaPack = 1200; long long largeTeaStorage = 100000000; short teaSample = 25;
cout << largeTeaStorage << endl;
return 0;}Let’s go through the code line by line:
- The
unsigned smallTeaPack = 1200;line declares a variable namedsmallTeaPackof typeunsignedand assigns the value1200to it. - The
long long largeTeaStorage = 100000000;line declares a variable namedlargeTeaStorageof typelong longand assigns the value100000000to it. - The
short teaSample = 25;line declares a variable namedteaSampleof typeshortand assigns the value25to it. - The
cout << largeTeaStorage << endl;line prints the value of thelargeTeaStoragevariable to the console.
String Code in Video
#include <iostream>#include <string>using namespace std;
int main(){ string favoriteTea = "Lemon Tea \t"; string description = "Known as \"best\" tea";
cout << favoriteTea << description << endl;
return 0;}Let’s go through the code line by line:
- The
string favoriteTea = "Lemon Tea \t";line declares a variable namedfavoriteTeaof typestringand assigns the value"Lemon Tea \t"to it. - The
string description = "Known as \"best\" tea";line declares a variable nameddescriptionof typestringand assigns the value"Known as \"best\" tea"to it. - The
cout << favoriteTea << description << endl;line prints the value of thefavoriteTeaanddescriptionvariables 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.
#include <iostream>using namespace std;
int main(){ float teaPrice = 49.99; int roundedTeaPrice = (int) teaPrice;
int teaQuantity = 2; int totalPrice = teaPrice * teaQuantity;
cout << totalPrice << endl;
return 0;}Let’s go through the code line by line:
- The
float teaPrice = 49.99;line declares a variable namedteaPriceof typefloatand assigns the value49.99to it. - The
int roundedTeaPrice = (int) teaPrice;line declares a variable namedroundedTeaPriceof typeintand assigns the value49to it. - The
int teaQuantity = 2;line declares a variable namedteaQuantityof typeintand assigns the value2to it. - The
int totalPrice = teaPrice * teaQuantity;line declares a variable namedtotalPriceof typeintand assigns the value98to it. - The
cout << totalPrice << endl;line prints the value of thetotalPricevariable to the console.
User input code in Video
#include <iostream>#include <string>
using namespace std;
int main(){ string userTea; int teaQuantity;
cout << "What would you like to order in tea? \n"; getline(cin, userTea);
//ask for quantity cout << "how many cups of " << userTea << "would you like to have ? "; cin >> teaQuantity;
cout << teaQuantity; cout << userTea;
return 0;}Let’s go through the code line by line:
#include <string>line includes thestringheader file, which provides thestringdata type.- The
string userTea;line declares a variable nameduserTeaof typestringand does not assign a value to it. - The
int teaQuantity;line declares a variable namedteaQuantityof typeintand 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 theuserTeavariable. - 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 theuserTeavariable 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 theteaQuantityvariable. - The
cout << teaQuantity;line prints the value of theteaQuantityvariable to the console. - The
cout << userTea;line prints the value of theuserTeavariable 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.
Start your journey with ChaiCode
All of our courses are available on chaicode.com. Feel free to check them out.