Features & Data types in C++

Features & Data types in C++
Features & Data types in C++

Introduction

This article will give you a brief about the data types in C++. Just like every course has some categories, every variable in programming has a special category to which it is assigned and it is called the data type of that variable.

By defining a data type we restrict a variable to that data type in C++. Compiler allocates some memory to a variable. This memory is depended on the data type being used. So, let us learn about the data types and their features.

        PRIMARY       DERIVED   USER DEFINED
IntegerFunctionClass
CharacterArrayStructure
BooleanPointerUnion
Floating pointReferenceTypedef
Double floating point Enum
Void and wide  
  1. Primitive data types: These data types in c++ are predefined in the system. Users can use them directly to declare the variables. Below is the explanation of primitive data types. Examples are int, char, float, double, etc.
  • Integer: This data types in c++ stores the values that are integer types. The keyword used for this category is an int that requires 4 bytes in memory.
  • Character: It is used to store all kinds of characters. The keyword used for the character data type is char that requires 1 byte of memory space.
  • Boolean: The keyword for a Boolean type is bool. It is used to the values that are Boolean type i.e, true or false.
  • Floating Point: This data types in c++ uses single-precision or single-precision values. The keyword used is float. It also takes 4 bytes to store in memory. 
  • Double Floating Point: It is used to store double-precision floating-point numbers. The keyword used is double. This data types in c++ takes 8 bytes in the computer memory.
  • Void: It means nothing. It is used for a function that returns nothing or pointers that are not defined to any type.
  • Wide Character: It is similar to char but with a greater size than the normal 8-bit datatype. The keyword used is wchar_t and it is generally 2 or 4 bytes long. 

Data type modifiers: The data type modifiers are used to modify the length of data.


Have a look at these modifiers below:

SignedUnsignedLongShort
IntegerIntegerIntegerInteger
CharCharDouble 
Long-prefixShort-prefix  

A signed modifier means it can represent both negative and positive values. An unsigned modifier can only represent non-negative integers i.e, zero or positive numbers. Long and Short modifier can be appended to int and double to increase their size.

B) Derived data types:

  • Functions: A function is a block of statements that executes to produce the desired output. A user writes functions to save time.
Syntax – function_type function_name ( parameter list)
Example -
// Derived data type
include <iostream>
using namespace std;
// max function returns max of 2 nos.
int max(int x, int y)
{
if (x > y)
return x;
else
return y;
}
// main is the default function
int main()
{
int a = 10;
int b = 20;
// Calling above function to 
int m = max(a, b); 
// print m which is max of a and b
cout << "m is " << m;
return 0;
}
  • Array: An array is a linear data structure that holds multiple values in a single variable.
Eg: int arr[10] // It stores 10 integers in consecutive manner.

Example:

#include<iostream>
Using namespace std;
Int main()
{
int arr[5]={1, 2, 3, 4, 5}; // declaration and //initialisation of array
// printing of an array
for(int I =0;i<5; i++)
cout << arr[i] << “ “ ;
return 0;
}
  • Pointer: Pointers are a symbolic representation of addresses. They enable programs to simulate call-by-reference as well as to create and manipulate dynamic data structures. Also, understand more about the size of the pointers in C++

    Its general declaration in C/C++ has the format:

Syntax:

datatype  *var_name;
int  *ptr; // ptr can point to an address which holds int data

Example:

#include <bits/stdc++.h>
using namespace std;
void function()
{
int var = 20;
int *ptr; //declare pointer variable
//note that data type of ptr and var must be same
ptr = &var;
// assign the address of a variable to a pointer
cout << "Value at ptr = " << ptr << "\n";
cout << "Value at var = " << var << "\n";
cout << "Value at *ptr = " << *ptr << "\n";
}
int main()
{
function();
}
  • References: A reference is like an alias to a variable. When we pass something as a reference to a function it does not make its copy but makes changes to the actual parameter passed by reference.

References are generally used to avoid memory overflow.

blog banner 1
Syntax: datatype &varname;

Example:

#include<iostream>
using namespace std;
int main()
{
int x = 10;
// ref is a reference to x.
int& ref = x;
// Value of x is now changed to 20
ref = 20;
cout << "x = " << x << endl ;
// Value of x is now changed to 30
x = 30;
cout << "ref = " << ref << endl ;
return 0;
}

C) User-defined data types:

  • Class: In C++, an object is a group of similar objects. It is a template from which objects are created. It can have fields, methods, constructors, etc. Let’s see an example of a C++ class that has three fields only.
class Student
{
public:
int id; // field or data member
float salary; // field or data member
String name; // field or data member
}
  • Structure: C/C++ arrays allow you to define variables that combine several data items of the same kind, but the structure is another user-defined data type that allows you to combine data items of different kinds. Structures are used to represent a record, suppose you want to keep track of your books in a library. You might want to track the following attributes about each book:
  • Title
  • Author
  • Subject
  • Book ID

Defining a Structure: To define a structure, you must use the struct statement. The struct statement defines a new data type, with more than one member, for your program. The format of the struct statement is this:

struct [structure tag] {
member definition;
member definition;
member definition;
} [one or more structure variables];

  • Union: A union is a user-defined datatype. All the members of the union share the same memory location. The size of the union is decided by the size of the largest member of the union. If you want to use the same memory location for two or more members, the union is the best for that. Unions are similar to structures. Union variables are created in the same manner as structure variables. The keyword “union” is used to define unions in C language.
Syntax: 
Union union_name {
Member definition;
} union_variables;

Example:

include <stdio.h>
union test {
int x, y;
};
int main()
{
// A union variable t
union test t;
t.x = 2; // t.y also gets value 2
printf("After making x = 2:\n x = %d, y = %d\n\n",
t.x, t.y);
t.y = 10; // t.x is also updated to 10 
printf("After making y = 10:\n x = %d, y = %d\n\n", 
    t.x, t.y); 
return 0; 

}

  • Typedef: The C++ programming language provides a keyword called typedef, which you can use to give a type a new name. Following is an example to define the term BYTE for one-byte numbers:
typedef unsigned char BYTE;

After this type definition, the identifier BYTE can be used as an abbreviation for the type unsigned char, for example:

BYTE  b1, b2;

By convention, uppercase letters are used for these definitions to remind the user that the type name is really a symbolic abbreviation, but you can use lowercase, as follows −

typedef unsigned char byte;

You can use typedef to give a name to your user-defined data types as well.

  • Enum: Enums in c++ is a user-defined data type that can be assigned some limited values. These values are defined by the programmer at the time of declaring the enumerated type.

When we assign a float value in a character value then the compiler generates an error in the same way if we try to assign any other value to the enumerated data types the compiler generates an error. Enumerator types of values are also known as enumerators. It is also assigned by zero the same as the array. It can also be used with switch statements.

For example: If a gender variable is created with value male or female. If any other value is assigned other than male or female then it is not appropriate. In this situation, one can declare the enumerated type in which only male and female values are assigned.

Syntax:
enum enumerated-type-name{value1, value2, value3…..valueN};

the enum keyword is used to declare enumerated types after that enumerated type name was written then under curly brackets possible values are defined. After defining Enumerated type variables are created. It can be created in two types:-

  • It can be declared during declaring enumerated types, just add the name of the variable before the semicolon. or,
  • Besides this, we can create enumerated type variables as same as the normal variables.
enumerated-type-name variable-name = value;

We hope that you enjoyed reading this article. To explore more articles on C++, click here.

By Mansi Agarwal