Data Types In C++ .

In this session i have discussed the data types in C++ .


A data type is used to specify the type of data that a variable can store such as integer, floating, character, double etc.


Types
Data Types
Primary data types int , float , double , char , void
Derived data types array , function , pointer , reference
User Defined data types structure , union , class , enumeration
.


1-Primary data types

We will discuss the primary data types as below .

1.1-Integer data type

Integer data type allows the variable to hold the numeric value (whole numbers) . int keyword is used to refer the integer data type .

1.2-Floating data types

It is consist of two types

1-float

2-double

1-float

float data type allows the variable to hold the decimal numbers . float keyword is used to refer float data type .

2-double

double data type also same as float data type but it store the value up to 10 digit after decimal . double keyword is used to refer double data type .

1.3-Character data type

character data type is used to store the one character in variable . char keyword is used to refer the character data type .


2-Derived data types

2.1-Array

Array is the collection of values and thier data type is same . The values stored in array is called the elements .

    #include <iostream>
    using namespace std;
    int main()
    {
        int arr[4]; //declared the array
        arr[0]=10; //stored the value at 0 index
        arr[1]=20; //stored the value at 1 index
        arr[2]=30; //stored the value at 2 index
        arr[3]=40; //stored the value at 3 index
        cout<<"\nvalue1 : "<<arr[0];
        cout<<"\nvalue2 : "<<arr[1];
        cout<<"\nvalue3 : "<<arr[2];
        cout<<"\nvalue4 : "<<arr[3];
        return 0;
    }
    OUTPUT : value1 : 10
             value2 : 20
             value3 : 30
             value4 : 40
    

The above code declared the array of size 4 that means you can store the 4 elements . Array always start with 0 index .

We will discuss the array in details in array topic .

2.2-Pointer

A pointer is a data type that store the address of a variable with particular type . They are declared as follows .

    #include <iostream>
    using namespace std;
    int main()
    {
        int x;
        int* ptr; // ptr pointer is declared
        x=10;
        ptr=&x; // ptr store the address of x 
        cout<<"value : "<<*ptr;
        return 0;
    }
    
    value : 10
    

We will discuss the pointer in details in pointer topic .

2.3-Function

A C++ function is collection of code that perform a particular task .

    #include<iostream>
    using namespace std;
    void show();
    int main()
    {
        show();
        return 0;
    }
    void show()
    {
        cout<<"Hello World";
    }
    

2.4-Reference

A C++ introduced a new kind that is called reference type . If you make a variable reference type means you can use interchangeble .

Syntax

Data type & identifier=variable-name
    #include<iostream>
    using namespace std;
    int main()
    {
        int sum=200;
        int&total=sum;
        cout<<"\nTotal : "<<total;
        cout<<"\nSum : "<<sum;
        return 0;
    }
    
    OUTPUT : Total : 200
             Sum : 200
    

3-User Defined Data Type

3.1-Structure

Structure store the multiple data items with different data types in one memory block referenced by the single variable.

    #include <iostream>
    #include <string.h>
    using namespace std;
    struct Employee
    {
        int empid;
        string empname;
        int empsal;
    };
    int main()
    {
        struct Employee emp;
        emp.empid=1;//store the value in empid
        emp.empname="Amit";//store the value in empid
        emp.empsal=10000;//store the value in empid
        cout<<"\nEmpID : "<<emp.empid;
        cout<<"\nEmpName : "<<emp.empname;
        cout<<"\nEmpSal : "<<emp.empsal;
        return 0;
    }
    
    OUTPUT : EmpID : 1
             EmpName : Amit
             EmpSal  : 10000
    

We will discuss the structure in details in structure topic .

3.2-Union

A union type is a special construct that permits access to the same memory block by using a choice of differing type descriptions.

    #include<iostream>
    #include<string.h>
    using namespace std;
    union Data
    {
        int i;
        float f;
        char name[30];

    };
    int main()
    {
        union Data data;
        data.i = 10;
        data.f = 220.5;
        strcpy(data.name,"C Language");
        cout<<"\ndata.i : "<<data.i;
        cout<<"\ndata.f : "<<data.f;
        cout<<"\ndata.name : "<<data.name;
        return 0;
     }
   OUTPUT : data.i : 1917853763
            data.f : 4122360580327794860452759994368.000000 
            data.name : C Language
    

The i , f value is got currupted because the final value stored in name variable .

We will discuss the union in details in union topic .


3.3-Enumeration data type

Enumeration data type is used to assign the names to number . enum keyword refer the enumeration data type . Bydefault it start from 0 .

    #include<iostream>
    using namespace std;
    enum week{Mon, Tue, Wed, Thur, Fri, Sat, Sun}; 
    int main() 
    { 
        enum week day; 
        day = Tue; 
        cout<<day; 
        return 0; 
    }  
    
    OUTPUT : 1
    

We will discuss the enum in details in enum topic .

3.4-Class

Class is encapsulate the data members and member function .

    #include<iostream>
    using namespace std;
    class ABC
    {
        public :
          void show()
          {
                cout<<"Hello World";
          }
    }; 
    int main() 
    { 
       ABC obj;
       obj.show();
       return 0; 
    }  
    
    OUTPUT : Hello World
    

Welcome to AndroidATPoint.

back image                     next image