A Quick guide to C++ Data Types – Part 2

Variables in any programming language is used to define a memory location. A typical variable will be given a name that can be accessed during the time of program execution. In C++ we are required to specify what type of data we are going to store so the program compiler won’t get confused.

Data Types:

The data that is stored in memory can be of many types. For an example, a student’s age is stored as a number value and address is stored as an alphanumerical characters. Data types are used by the compiler to manage the operations possible on them and to determine the storage required for each of them.

A typical C++ compiler allow many forms of data types. Below are commonly used in Object Oriented C++ programs.

Built-in / Primitive data Types:

Built in data types includes data types at the lowers level. Typical use of built-in data types is to represent data in machine level.

Data TypeDescriptionEg.
charUsed for storing a single character.char c =’a’
intUsed for storing whole number values.int x = 250
floatUsed for single precision floating point values( decimal values)float f = 12.5
doubleUsed for double precision floating point values( decimal values)double d =12.55
boolUsed for storing Boolean values (true or false state)bool b = true
stringNot a built in type but commonly used data type used to store a sequence of characters.#include <string>
string test = “TEST”;
#include <iostream>

int main() 
{
    // intiger type : store a whole number.
    int dummyInt = 250;
    // char type : store a single character.
    char dummyChar = 'a';
    // float type : store a single precision floating point.
    float dummyFloat = 12.5;
    // double type : store a double precision floating point
    double dummyDouble = 123321123.12332112321;
    // boolean type : store a true or false.
    bool isValid = true;
    return 0;
}

Data Types and Size allocations:

In C++, all the data types are loaded to the heap memory when executing. Depending on your run-time environment (32 Bit Vs. 64 Bit, Linux Vs. Windows, etc) the space allocation for individual data type will be vary.

Use the below code to check heap memory allocation in your env.

// Use the sizeOf() to check the heap allocation for your variables.

#include <iostream>
using namespace std;

struct STUDENT
{ // Declare STUDENT struct type consists of 3 data types.
    int age;
    float marks;
    string name;
};
 
int main() {
   cout << "char = " << sizeof(char) << endl;
   cout << "int = " << sizeof(int) << endl;
   cout << "float = " << sizeof(float) << endl;
   cout << "long = " << sizeof(long int) << endl;
   cout << "double = " << sizeof(double) << endl;
   cout << "string = " << sizeof(string) << endl;
   cout << "struct STUDENT made of : (int + float + string) = " << sizeof(STUDENT) << endl;
   return 0;
}
Derived data types:

These are data types that are made of primitive/built-in data types. For an instance a typical function will have multiple primitive data types defined inside the body and function will be allocated to a memory location which can be retrieved by calling the memory location.

TypeDescriptioneg.
arrayCollection of elements of a single data type stored in adjacent memory location.int arr[8];
functionBlock of code separated to maintain the structure of the program logic.int getDummyInt(){}
pointerUsed to store the memory address of other variables. (Read more about C++ Pointers) int dummyInt = 250;
int *pointerToDummyInt = &dummyInt;
referenceUsed to store a reference to an existing variable.(Read more about C++ References) int dummyInt = 250;
int &refToDummyInt = dummyInt;
#include <iostream>

using namespace std;

// function type
int getDummyInt()
{
    int dummyInt = 250;
    // Ref. type
    int &refToDummyInt = dummyInt;
    // Pointer type
    int *pointerToDummyInt = &dummyInt;

    // Output the value
    cout << dummyInt << "\n";

    // Output the memory address using Reference
    cout << &refToDummyInt << "\n";

    // Output the memory address using Pointer
    cout << pointerToDummyInt << "\n";
}

int main()
{
    getDummyInt();
    return 0;
}
User-defined data types:

These are the data types or structures that are custom built by the users while they are programming.

TypeDescriptione.g.
structUsed to define a structure of a object.struct STUDENT {}
enumUsed to define set of constants that makes the program more readable.enum GRADE{PASS=’P’, FAILED=’F’, ABSENT=’A’};
unionUsed to reuse the regions of memory by allowing to storing different objects at different times. (memory sharing)union value { int x; int y; };
classUsed t defined a custom object structure including data members and functions.class Student{}
#include <iostream>
// Need to add the
#include <string>

using namespace std;

struct STUDENT
{ // Declare STUDENT struct type consists of 3 data types.
    int age;
    float marks;
    string name;
};

enum GRADE
{
    PASS = 'P',
    FAILED = 'F',
    ABSENT = 'A'
};

// The class : CalculateStudentGrades
class CalculateStudentGrades
{
public: // Access specifier
    STUDENT studentStruct;
    void calculate()
    {
        // impl. of the calculate functoin
    }

private:
    GRADE finalGrades;
    string studentID;
    float finalMarks;
};

int main()
{
    STUDENT newStudent;
    newStudent.age = 20;
    newStudent.marks = 80;
    newStudent.name = "Ann Fixma";

    cout << newStudent.name;
    return 0;
}
Download the Code.

That is a quick introduction to popular Data Types in C++. Next we will be discussing the Object Oriented Programming using C++.

NEXT >> C++ Object Oriented Programming Part 3

One Reply to “A Quick guide to C++ Data Types – Part 2”

Leave a Reply

Your email address will not be published. Required fields are marked *