User Defined Data Types in C++

User Defined Data Types in C++
Techiio-author
Written by Sagar RabidasJanuary 27, 2022
11 min read
C++
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Techiio-author
Sagar Rabidas

Software Developer

In this blog, we will discuss User Defined Data Types in C++

User-Defined Data Types in C++

User-defined information type in c++ is a kind by which the facts can be represented. The form of data will tell the interpreter how the programmer will use the information. A data kind can be pre-described or user-described. Examples of pre-defined statistics kinds are char, int, drift, and so on. We can speak person-described statistics sorts in detail.

As the programming languages permit the consumer to create their facts kinds consistent with their wishes. As a result, the facts kinds which can be described by way of the user are referred to as person-described statistics sorts. For example; arrays, magnificence, shape, union, enumeration, pointer, and so forth. These records kinds maintain extra complexity than pre-defined records kinds.

Types of User-Defined Data in C++

Structure

A structure is defined as a collection of various types of related information under one name. The declaration of structure forms a template and the variables of structures are known as members. All the members of the structure are generally related. The keyword used for the structure is “struct”.

For example; a structure for student identity having ‘name’, ‘class’, ‘roll_number’, ‘address’ as a member can be created as follows:

struct stud_id
{
char name[20];
int class;
int roll_number;
char address[30];
};

This is called the declaration of the structure and it is terminated by a semicolon (;). The memory is not allocated while the structure declaration is delegated when specifying the same. The structure definition creates structure variables and allocates storage space for them. Structures variables can be defined as follows:

stud_id I1, I2;

Where I1, I2 are the two variables of stud_id. After defining the structure, its members can be accessed using the dot operator as follows:

I1.roll_number will access roll number of I1

I2.class will access class of I2

Example:

struct stud_id
{
int class, roll_number;
};
int main()
{
struct stud_id entries[10];   // Create an array of structures
entries[0].class = 4;           // Access array members
entries[0].roll_number = 20;
cout <<entries[0].class << ", " << entries[0].roll_number;
return 0;
}

Array

An Array is defined as a collection of homogeneous data. It should be defined before using it for the storage of information. The array can be defined as follows:

<datatype> <array_name><[size of array]>
int marks[10]

The above statement defined an integer type array named marks that can store marks of 10 students. After the array is created, one can access any element of an array by writing the name an array followed by its index. For example; to access 5th element from marks, the syntax is as follows:

marks[5]

It will give the marks stored at the 5th location of an array. An array can be one-dimensional, two-dimensional or multi-dimensional depending upon the specification of elements.

Example:

int main()
{
int marks[10];
marks[0] = 5;
marks[2] = -10;
cout<<marks[0], marks[2]);
return 0;
}

Union

Just like structures, the union also contain members of different data types. The main difference between the two is that union saves memory as members of a union share the same storage area whereas members of the structure are assigned their own unique storage area. Unions are declared with keyword “union” as follows:

union employee
{
int id;
double salary;
char name[20];
}

The variable of the union can be defined as:

union employee E;

To access the members of the union, the dot operator can be used as follows:

E.salary;

Class

A class is an important feature of object-oriented programming language just like C++. A class is defined as a group of objects with the same operations and attributes. It is declared using a keyword “class”. The syntax is as follows:

class <classname>
{
private:
Data_members;
Member_functions;
public:
Data_members;
Member_functions;
};

In this, the names of data members should be different from member functions. There are two access specifiers for classes that define the scope of the members of a class. These are private and public. The member specified as private can be only accessed by the member functions of that particular class only. However, the members defined as the public can be accessed from within and from outside the class also. The members with no specifier are private by default. The objects belonging to a class are called instances of the class. The syntax for creating an object of a class is as follows:

<classname> <objectname>

Example:

class kids
{
public:                //Access specifier
char name[10];   //Data members
int age;
void print()         //Member function
{
cout<<”name is:”<< name;
}
}
Int main
{
Kids k;                    //object of class kid is created as k
k.name=”Eash”;
k.print();
return 0;
}

Enumeration

Enumeration is specified by using a keyword “enum”. It is defined as a set of named integer constants that specify all the possible values a variable of that type can have. For example, enumeration of the week can have names of all the seven days of the week as shown below:

Example:

enum week_days{sun, mon, tues, wed, thur, fri, sat};
int main()
{
enum week_days d;
d = mon;
cout << d;
return 0;
}

Pointer

A Pointer is that kind of user-defined data type that creates variables for holding the memory address of other variables. If one variable carries the address of another variable, the first variable is said to be the pointer of another. The syntax for the same is:

type *ptr_name;

Here type is any data type of the pointer and ptr_name is the pointer’s name.

Example:

void main()
{
int a = 10;
int *p;   // pointer variable is declared
p = &a;  // data type of pointer ‘p’ and variable ‘a’ should be same
cout<<"Value at p = ",<<p); // the address of a variable is assigned to a pointer
cout<<"Value at variable a = “,<<a);
cout<<"Value at *p = ",<< *p);
}

Typedef

Using the keyword “typedef”, you can define new data type names to the existing ones. Its syntax is:

typedef <type> <newname>;
typedef float balance;

Where a new name is created for float i.e. using balance, we can declare any variable of float type.

The use of a typedef can make the code easy to read and also easy to port to a new machine.

Example:

typedef  int score;
int main()
{
score s1, s2;
s1 = 80;
cout << " " << b1;
return 0;
}

C
C plus plus
UserDefinedDataTypes
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance