Skip to content

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.

Data Types

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 either true or false.
  • 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

#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;
}

Going through the code

  • The int teaLeaves = 50; line declares a variable named teaLeaves of type int and assigns the value 50 to it.
  • The float waterTemperature = 85.588588; line declares a variable named waterTemperature of type float and assigns the value 85.588588 to it.
  • The double priceOfTea = 299.99; line declares a variable named priceOfTea of type double and assigns the value 299.99 to it.
  • The char teaGrade = 'A'; line declares a variable named teaGrade of type char and assigns the value 'A' to it.
  • The bool isTeaReady = false; line declares a variable named isTeaReady of type bool and assigns the value false to it.
  • The cout << waterTemperature << endl; line prints the value of the waterTemperature variable 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;
}

Going through the code

  • The unsigned smallTeaPack = 1200; line declares a variable named smallTeaPack of type unsigned and assigns the value 1200 to it.
  • The long long largeTeaStorage = 100000000; line declares a variable named largeTeaStorage of type long long and assigns the value 100000000 to it.
  • The short teaSample = 25; line declares a variable named teaSample of type short and assigns the value 25 to it.
  • The cout << largeTeaStorage << endl; line prints the value of the largeTeaStorage variable 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;
}

Going through the code

  • The string favoriteTea = "Lemon Tea \t"; line declares a variable named favoriteTea of type string and assigns the value "Lemon Tea \t" to it.
  • The string description = "Known as \"best\" tea"; line declares a variable named description of type string and assigns the value "Known as \"best\" tea" to it.
  • The cout << favoriteTea << description << endl; line prints the value of the favoriteTea and description 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.

#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;
}

Going through the code

  • The float teaPrice = 49.99; line declares a variable named teaPrice of type float and assigns the value 49.99 to it.
  • The int roundedTeaPrice = (int) teaPrice; line declares a variable named roundedTeaPrice of type int and assigns the value 49 to it.
  • The int teaQuantity = 2; line declares a variable named teaQuantity of type int and assigns the value 2 to it.
  • The int totalPrice = teaPrice * teaQuantity; line declares a variable named totalPrice of type int and assigns the value 98 to it.
  • The cout << totalPrice << endl; line prints the value of the totalPrice variable 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;
}

Going through the code

  • #include <string> line includes the string header file, which provides the string data type.
  • The string userTea; line declares a variable named userTea of type string and does not assign a value to it.
  • The int teaQuantity; line declares a variable named teaQuantity of type int 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 the userTea 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 the userTea 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 the teaQuantity variable.
  • The cout << teaQuantity; line prints the value of the teaQuantity variable to the console.
  • The cout << userTea; line prints the value of the userTea 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!