C++ Data Types

Data type of c++ programming language are used to store information. this information are various type, Normally number are used to mathematically operation and string or text are store the user information.C++ are providing to several data type. help of this data type we are solve complex problem and build a real time application.

Concept of C++ Data Types

So how to use data type in c++ program? . when we are declared variable then compiler are allocated memory of those variable at compile time. Because every variable are store the information according to data type. so every data type are has reserved memory. We are utilize those data according to on requirements. cpp is allowed to use fundamental data type. and programmer can make on data type using of inbuilt data type.

Data Types

c++ data type are categorized into following.

Fundamental Data Type

Fundamental data type also known as primitive data type of inbuilt data type. This are divided into seven parts.

keyword Name default values
int Integer local variable :(garbage) global variable :(0) zero
bool Boolean local and global variables : 0 (false)
char Character local and global variable :( ) (empty)
float Floating point local variable :(garbage) global variable :(0) zero
double double floating point local variable :(garbage) global variable :(0) zero
void double floating point pointer local variable :(garbage) global variable :(0) zero or NULL
wchar_t Wide character local variable :(garbage) global variable :(0) zero

User Defined Data Type

C++ are allowing to create user defined data types. there is following types.

structure (struct) are used to combine multiple data into single unit. That is reliable to create user defined data type. c++ are allowing to use data members and member function within the structures. there are declared using of this syntax.

struct tag_name{
  //data_members;
  //functions(){}
}variable_list;

Learn more about structures in Structures section.

Enumeration (enum) are used to define the constants integers. Or we can say there are combined group of integers. enumeration are simplest user defined data type. There are defined by following syntax.

//declaration of enumeration
enum tag_name{
  //enum body
  //constant_variables
}variable_list;

Learn more about Enumeration in Enumeration section.

union is keyword of c++ programming language. there are use to defined the user data type. we can use multiple data member within union. all union member is share the same memory location. In this reason only one member can be utilize those memory at a time. union provide an efficient way to use same memory location in multiple purpose.

union tag_name{
  data_member;
  data_member;
  //:
  //:
  //another data_members
}variable_list;
Key Point overview
union keyword
tag_name Name of union
data_member list of data type and its variables
variable_list union variable

Using of this example look at understand the behavior of union.

//union example in C++
#include<iostream>
using namespace std;
union Sim4G{
  //data ,member
  int airtel;
  float jio;
  double docomo;
};
int main(){

  Sim4G u1,u2,u3;
  //assign values
  u1.airtel=10;
  u2.jio=30;
  u3.docomo=5;
  //display values
  cout<<" u1 airtel :"<<u1.airtel<<endl;
  cout<<" u2 jio :"<<u2.jio<<endl;
  cout<<" u3 docomo :"<<u3.docomo<<endl;
  return 0; 
}
Output
u1 airtel :10
 u2 jio :30
 u3 docomo :5

Now assume that there are following sim are you in our mobile phone. but there are possible to use one 4G SIM in our mobile. we cannot access two 4g network at a time.

similarly union are capable to define various variable. but memory are allocated shareable format. every data member are sharing this memory. If access is data member value in single union variable then it will print garbage or undefined values. see examples.

#include<iostream>
using namespace std;
union Sim4G{
  //data ,member
  int airtel;
  float jio;
  double docomo;
};
int main(){
  //union variable
  Sim4G u1;
  //assign values
  u1.jio=31;
  u1.airtel=10;
  u1.docomo=5;
  //display values
  cout<<" u1 airtel :"<<u1.airtel<<endl;
  cout<<" u1 jio :"<<u1.jio<<endl;
  cout<<" u1 docomo :"<<u1.docomo<<endl;
  return 0; 
}
Output
 u1 airtel :0
 u1 jio :0
 u1 docomo :5

View more example of Union in Union section.

Class is also an user defined data type this are contain data member and member function.

class example
{
  //Access specifiers
  //Data member
  //Member function
};

There is an important concept in object oriented programming. learn about that in Class & Object section.

Derived Data Type

C++ derived data type are supported on array and pointers

Array are useful storage they are provide a capability to combine similar type of data. Most of cases they are reliable to implements similar type of variables. array is an data structure of programming language. In c++ program create an array of using this syntax.

data_type tag_name[size_of_array];

Learn more about array in Array section.

Pointer is variable which are store the address of another variable. pointer is very interesting topic because they are utilize the address of memory location. Using of & (address of operator) compiler are providing the memory location of variable which are occupied in RAM section. and help of * (Dereferencing operator) utilize the memory of those location like changing the value and get the value so on.

data_type * variable_name;

Learn more about pointer in pointer section.

Data Type Modifier

Type modifiers are used to providing a special meaning (behaviors) of inbuilt data type variables. There are few keywords are provide in c++ programming to do this task. those keywords are prepend before the inbuilt data type and change its properties and storage capacity (increase and decrease size). there are following types. (signed,unsigned,long,short).

Learn more about type modifiers in Type modifiers.

© 2019, regularcodes.com, All rights reserved