C++ Data Types

C++ Data Types


Define :
C++ Data Types
Data Type is type of the data that is used to declare the variable.
All variables use data-type during declaration the type of data to be stored.

                       

Data types in C++ are categorised in three groups: Built-in (Primitive), User-defined and Derived.

Built-in (Primitive) Data Types

C++ offers many data types, in the very broadest sense there are only two: numeric and character. Numeric data types are broken into two additional categories: integer and floating point. Integers are whole numbers like 12, l57, -34, and 2. Floating-point numbers have a decimal point, like 23.7, 189.0231, and 0.987.

  1. Integer Data Type
  2. Floating points Data Type
  3. Character points Data Type
  4. Boolean points Data Type

i. Integer Data Type

The int keyword is used to indicate integers.

For Example:

ii. Floating points Data Type

float and double are used to store floating-point numbers (decimals and exponentials).

The size of float is 4 bytes and the size of double is 8 bytes. Hence, double has two times the precision of float. To learn more, visit C++ float and double.

For Example:

As mentioned above, these two data types are also used for exponentials. For example,

iii. Character points Data Type

     Keyword char is used for characters. Its size is 1 byte.

     Characters in C++ are enclosed inside single quotes ' '.

     For example,

iv. Boolean points Data Type 
Booleans are used in conditional statements and loops(which we will learn in later chapters).
The bool data type has one of two possible values: true or false.
 For example,

2.User defined Data Types:

The data types that are defined by the user are called the derived data type or user-defined derived data type.
These types include:

  1. Class
  2. Structure
  3. Enumeration
  1. Class:

A class in C++ is a user-defined type or data structure declared with keyword class.

When you define a class, you define a blueprint for a data type. This doesn't actually define any data, but it does define what the class name means, that is, what an object of the class will consist of and what operations can be performed on such an object.

A class definition starts with the keyword class followed by the class name; and the class body, enclosed by a pair of curly braces. A class definition must be followed either by a semicolon or a list of declarations. For example, we defined the Box data type using the keyword class as follows –

Structure Data Types:

Structure is a collection of variables of different data types under a single name. It is similar to a class in that, both holds a collection of data of different data types.

The struct keyword defines a structure type followed by an identifier (name of the structure).

Then inside the curly braces, you can declare one or more members (declare variables inside curly braces) of that structure. For example:

iii. Enumeration Data Type

An enumeration is a user-defined data type that consists of integral constants. To define an enumeration, keyword enum is used.

For Example:

3. Derived Data Types

  1. Function
  2. Array
  3. Pointer