Advanced data types in C++ - Informatic Point

Advanced data types in C++

In this tutorial, you will learn the advanced data types in C++ Language. All variables use data-type while declaration to modify the type of data to be saved. Therefore, we can tell that data types are used to represent the variables the type of data it can save. Whenever a variable is specified in C++, the compiler allocates some memory for that variable based on the data type with which it is named. However, Every data type requires a separate amount of memory.

Introduction to Advanced Data Types in C++.

C++ Data types specify the type of data that a variable can operate. It can be user-defined data types such as integer, float, double, char, or built-in data types like union, enum, struct, or can be derived data types like functions, pointers, arrays. A Data type is to let identify the variable, what type of element it is, and clearly going to define the memory allocation of that variable. Data types should be specified before the performance as it tells the compiler which type of data particular variables operates. The integer data type can keep only integer values, it cannot keep float values or string values. We are aware that per data type has a separate memory allocation. There are three different C++ data types particularly; Primitive, Derived and User Defined. Let’s walk forward and learn about them.

Here are three data types in c++ which are described below:

Primitive Data Types

Primitive data types are predefined or built-in data types and can be managed directly by the user to hold variables. example: int, char, float, bool, etc. Examples of Primitive data types available in C++ are:

?. Integer: Normally described by “int”. We can recognize the size of memory allocated and how the variable is declared as below. So, Let’s take a look at an example.

#include <iostream>
using namespace std;
int main()
{
int a;
cout<< " The Size of int is: " << sizeof(a);
}
Output
The Size of int is: 4

?. Character: Normally described by “char”. We can classify the size of memory allocated and how the variable is declared as below. So, Let’s take a glance at the Example.

#include <iostream>
using namespace std;
int main()
{
char a;
a='R';
cout<< "The Size of char is: " << sizeof(a)<<endl;
cout<< "The Value of a is: " << a;
}
Output
The Size of char is: 1 The Value of a is: R

?. Floating Point: Normally described by “float”. We can recognize the size of the memory assigned and how the variable is declared as below. So, Let’s see an example.

#include <iostream>
using namespace std;
int main()
{
float a;
a=5.85;
cout<< " The Size of float is: " << sizeof(a)<<endl;
cout<< " The Value of a is: " << a;
}
Output
The Size of float is: 4 The Value of a is: 5.85

?. Boolean: Normally described by “bool”. We can know the size of memory assigned and how the variable is declared as below. So, Let’s take a view at Example.

#include <iostream>
using namespace std;
int main()
{
bool a;
cout<< " The Size of bool is: " << sizeof(a)<<endl;
cout<< "The Value of a is: " << a;
}
Output
The Size of bool is: 1 The Value of a is: 0

String Data Type

?. String: Normally described by “String”. We can know the size of memory allocated and how the variable is declared as below.

#include <iostream>
using namespace std;
int main()
{
string a;
a="Happy";
cout<< " The Size of string is: " << sizeof(a)<<endl;
cout<< " The Value of a is: " << a;
}
Output
The Size of string is: 32 The Value of a is: Happy

Here, we further hold the concept of signed, unsigned, short, and long. So, what are these? These are named the Data type modifiers. These, in fact, select the exact length of any distinct data type.

Signed values provide us the values of both under and over zero, which is both positive and negative.

Whereas the unsigned conditions contain data that is only positive. And reaching to short and long, within the names itself we can clearly define that long data modifier has the ability to store huge amounts of values. And in fact, short is the data type needed and will exist a minimum of those numbers of values.

Derived Data Types

Derived data types that are derived from built-in or primitive data types are assigned to as Derived Data Types. These can be of two types particularly:

?. Array: C++ gives a data structure, the array, which collects a fixed-size, general collection of elements of the same derived data type. An array is used to save a collection of data, but it is usually more helpful to hold an array as a collection of variables of the same type. So, Let’s take a look at an example.

#include <iostream>
using namespace std;
int main()
{
int a[5]={1,6,10,15,56};
cout<< " Size of array is: " << sizeof(a)<<endl;
for(int i=0;i<6;i++)
{
cout<< " Value of a is: " << a[i] <<endl;
}}
Output
Size of array is: 20 Value of a is: 1 Value of a is: 6 Value of a is: 10 Value of a is: 15 Value of a is: 56 Value of a is: 32764

?. Pointer: This allows the request by reference functionality and these pointers perform a large position in holding or handling data in powerful data structures. For example in building Stacks, Queues, Linked lists we primarily use these pointers. So, Let’s take a look at an example.

#include <iostream>
using namespace std;
int main()
{
float a = 30;
float *h;
h= &a;
cout << " Value of pointer h "<< h << endl;
cout << " Value of variable a "<< a << endl;
cout << " h value "<< *h ;
}
Output
Value of pointer h 0x7fff09dcbb8c Value of variable a 30 h value 30

User-Defined Data Types

A data type is a collection of values collectively with a set of operations on the values. User-defined types are sets of data, which specify an object’s attributes and state. So, In C++ there are many examples of objects, including user-defined variables.

?.Structures: Collecting the sequence of both similar or different data types under consecutive memory locations. As we previously saw, in arrays we can save only items with similar data types. But structures can save different data types. So, Let’s see a small example below.

#include <iostream>
using namespace std;
struct First
{
int a = 58;
string r = "Happy";
float y = 58.5;
} ;
int main()
{
struct First f;
cout<< " Integer value is: "<< f.a <<endl;
cout<< " String value is: "<< f.r << endl;
cout<< " Float value is: "<< f.y;
}
Output
Integer value is: 58 String value is: Happy Float value is: 58.5

?.Class: It is described in object-oriented programming (OOP). This has functions, variables, and is obtained by building objects. So, let’s view a short example of the same.

#include <iostream>
using namespace std;
class First
{ public:
string name;
void show()
{cout << "Name is: " << name;}
};
int main()
{
First f;
f.name = "Rauf";
f.show();
return 0;
}
Output
Name is: Rauf

?. Enumeration: Described by the word “enum”. These are usually used when we already know a collection of values for a particular variable and take a single value from them. So, Let’s have a small example below.

#include <iostream>
using namespace std;
enum color {Yellow, Red, Green, Blue}col;
int main()
{
col = Green;
cout<<" The color chosen is in the place: "<<col;
return 0;
}
Output
The color chosen is in the place: 2

Well, You’re now able to write your code with Advanced data types C++ hope for the best Good Luck.

Leave a Reply

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