C++ Data Types

In C++, data types are used to define the type of data a variable can hold. There are several built-in data types in C++ which can be classified into four categories:

  1. Basic Data Types
  2. Enumerated Data Types
  3. Derived Data Types
  4. User-Defined Data Types

Basic Data Types

Basic data types in C++ are the most fundamental data types that the language provides. They include:

  1. Integers:
    • int (4 bytes)
    • short (2 bytes)
    • long (8 bytes)
    • long long (8 bytes)
    • unsigned int (4 bytes)
    • unsigned short (2 bytes)
    • unsigned long (8 bytes)
    • unsigned long long (8 bytes)
    • signed (the same as int)
  1. Floating-point numbers:
    • float (4 bytes)
    • double (8 bytes)
    • long double (12 or 16 bytes)
  1. Boolean:
    • bool (1 byte)
  1. Characters:
    • char (1 byte)
    • signed char (1 byte)
    • unsigned char (1 byte)
    • wchar_t (2 or 4 bytes)
    • char16_t (2 bytes)
    • char32_t (4 bytes)

Here’s an example of how to declare and initialize variables of basic data types in C++:

#include <iostream>
using namespace std;

int main() {
  int num1 = 10; // integer
  float num2 = 3.14; // floating-point number
  double num3 = 3.14159265358979323846; // double precision floating-point number
  bool flag = true; // boolean
  char letter = 'A'; // character
  
  // output the values
  cout << "num1 = " << num1 << endl;
  cout << "num2 = " << num2 << endl;
  cout << "num3 = " << num3 << endl;
  cout << "flag = " << flag << endl;
  cout << "letter = " << letter << endl;

  return 0;
}

Output:

num1 = 10
num2 = 3.14
num3 = 3.14159
flag = 1
letter = A

In the example above, we declared variables of different basic data types: num1 of type int, num2 of type float, num3 of type double, flag of type bool, and letter of type char. We initialized these variables with values and outputted their values using cout. Note that bool variables are outputted as 1 for true and 0 for false.


Enumerated Data Types

Enumerated data types, also known as enums, are a user-defined data type in C++ that allows you to define a set of named constants. Enums are typically used to represent a limited range of values, such as days of the week or colors.

To define an enum in C++, you use the enum keyword followed by the name of the enum and a set of named constants enclosed in curly braces.

For example:

enum DaysOfWeek {
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday,
    Sunday
};

In this example, the enum named DaysOfWeek is defined to include seven named constants representing the days of the week.

Enums are treated as integer values in C++. By default, the first named constant is assigned a value of 0, and each subsequent constant is assigned a value one greater than the previous constant. However, you can assign specific values to each constant by explicitly setting the value.

For example:

enum Colors {
    Red = 1,
    Green = 2,
    Blue = 4,
    Yellow = 8
};

In this example, the enum named Colors is defined with four named constants, each assigned a specific value. The values assigned to the named constants can be any integral type, including negative values.


Derived Data Types

In C++, derived data types are data types that are derived from basic data types. They include arrays, pointers, and references.

Arrays:

An array is a collection of similar data types that is stored in contiguous memory locations. Each element of an array is accessed using an index. In C++, arrays are declared by specifying the data type of the elements and the number of elements in the array. For example, the following code declares an array of integers with five elements:

int myArray[5];

Pointers:

A pointer is a variable that stores the memory address of another variable. In C++, pointers are declared by specifying the data type of the variable being pointed to, followed by an asterisk (). The address of a variable can be obtained using the address-of operator (&), while the value stored at a particular memory location can be accessed using the indirection operator (). For example, the following code declares a pointer to an integer variable:

int myVariable = 10;
int* myPointer = &myVariable;

References:

A reference is an alias for a variable. In C++, references are declared by using the ampersand (&) symbol. Once a reference is initialized, it can be used in place of the original variable. This can be useful for passing arguments to functions, as it allows the original variable to be modified directly. For example, the following code declares a reference to an integer variable:

int myVariable = 10;
int& myReference = myVariable;

User-Defined Data Types

User-defined data types in C++ are types that are defined by the programmer rather than being built-in to the language.

There are three main types of user-defined data types in C++:

  1. Structures
  2. Classes, and
  3. Unions.

Structures:

Structures are collections of different data types grouped together under a single name. Each item in a structure is called a member, and can be of any data type including other structures. A structure can be declared using the keyword “struct”, followed by the name of the structure and the list of member variables enclosed in braces.

For example:

struct Student {
    int id;
    char name[20];
    float gpa;
};

This creates a structure called “Student” with three members: an integer called “id”, a character array called “name”, and a float called “gpa”.

Classes:

Classes are similar to structures but also contain member functions. Member functions are functions that operate on an instance of the class, and can access and modify the data members of that instance. Classes can also have access modifiers, such as public, private, and protected, which control the visibility and accessibility of member variables and functions. A class is declared using the keyword “class”, followed by the name of the class and the list of member variables and functions enclosed in braces.

For example:

class Rectangle {
    private:
        float length;
        float width;
    public:
        float area();
        void set_values(float, float);
};

This creates a class called “Rectangle” with two private members: two floats called “length” and “width”, and two public member functions: a function called “area” that calculates the area of the rectangle, and a function called “set_values” that sets the values of the length and width.

Unions:

Unions are structures that store different data types in the same memory location. Only one member of the union can be accessed at a time. Unions are declared using the keyword “union”, followed by the name of the union and the list of member variables enclosed in braces.

For example:

union Number {
    int i;
    float f;
};

This creates a union called “Number” with two members: an integer called “i” and a float called “f”. Since both “i” and “f” share the same memory location, only one of them can be accessed at a time.

Wordpress Social Share Plugin powered by Ultimatelysocial
Wordpress Social Share Plugin powered by Ultimatelysocial