Open In App

User Defined Data Types in C++

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Data types are means to identify the type of data and associated operations of handling it. improve. In C++ datatypes are used to declare the variable. There are three types of data types:

  1. Pre-defined DataTypes
  2. Derived Data Types
  3. User-defined DataTypes
user-defined-data-types-in-cpp

In this article, the User-Defined DataType is explained:

User-Defined DataTypes

The data types that are defined by the user are called the derived datatype or user-defined derived data type. These types include:

Below is a detailed description of the following types:

1. Class

A Class is the building block of C++ that leads to Object-Oriented programming is a Class. It is a user-defined data type, which holds its own data members and member functions, which can be accessed and used by creating an instance of that class. A class is like a blueprint for an object.

Syntax

classes-and-objects-in-cExample:

The below example demonstrates the use of class in C++.

CPP




// C++ program to demonstrate
// Class
  
#include <bits/stdc++.h>
using namespace std;
  
class Geeks {
    // Access specifier
public:
    // Data Members
    string geekname;
  
    // Member Functions()
    void printname()
    {
        cout << "Geekname is: " << geekname;
    }
};
  
int main()
{
  
    // Declare an object of class geeks
    Geeks obj1;
  
    // accessing data member
    obj1.geekname = "GeeksForGeeks";
  
    // accessing member function
    obj1.printname();
  
    return 0;
}


Output

Geekname is: GeeksForGeeks

Explanation: The above program defines a class named “Geeks” with a geekname attribute and a function printname() to print the geek’s name. In the main function, it creates an object named obj1, sets the geekname as “GeeksforGeeks”, and calls the printname() function to display it.

2. Structure

A Structure is a user-defined data type in C/C++. A structure creates a data type that can be used to group items of possibly different types into a single type.

Syntax

struct structName{     
char varName[size];
int varName;
};

Example:

The below example demonstrates the use of structures in C++.

CPP




// C++ program to demonstrate
// Structures in C++
  
#include <iostream>
using namespace std;
  
// declaring structure
struct Point {
    int x, y;
};
  
int main()
{
    // Create an array of structures
    struct Point arr[10];
  
    // Access array members
    arr[0].x = 10;
    arr[0].y = 20;
  
    cout << arr[0].x << ", " << arr[0].y;
  
    return 0;
}


Output

10, 20

Explanation: The above demonstrates program demonstrates the use of structures by defining a structure named “Points” having x and y coordinates. It creates an array of these structures in the main function, sets their values, and prints them.

3. Union

Like Structures , Union a user-defined data type. In union, all members share the same memory location. For example in the following C program, both x and y share the same location. If we change x, we can see the changes being reflected in y.

Syntax

Union_Name 
{
// Declaration of data members
}; union_variables;

Example:

The below example demonstrates the use of union in C++.

CPP




#include <iostream>
using namespace std;
  
// Declaration of union is same as the structures
union test {
    int x, y;
};
  
int main()
{
    // A union variable t
    union test t;
  
    // t.y also gets value 2
    t.x = 2;
  
    cout << "After making x = 2:" << endl
         << "x = " << t.x << ", y = " << t.y << endl;
  
    // t.x is also updated to 10
    t.y = 10;
  
    cout << "After making Y = 10:" << endl
         << "x = " << t.x << ", y = " << t.y << endl;
  
    return 0;
}


Output

After making x = 2:
x = 2, y = 2
After making Y = 10:
x = 10, y = 10

Explanation: The above program demonstrates the use of unions. Union named “test” with integer members x and y is defined, here x and y shares the same memory space. In the main function value of x is set to 2 and then printed. Later, it updates y to 10 and the value of x is also updated to 10, this shows the shared memory characteristic of unions.

4. Enumeration

Enumeration (or enum) is a user-defined data type in C. It is mainly used to assign names to integral constants, the names make a program easy to read and maintain.

Syntax

enum  nameOfEnum {
varName1 = 1, varName2 = 0
};

Example:

The below example demonstrates the use of enumeration (enum) in C++.

C++




// Program to demonstrate working
// of enum in C++
  
#include <iostream>
using namespace std;
  
enum week { Mon, Tue, Wed, Thur, Fri, Sat, Sun };
  
int main()
{
    enum week day;
  
    day = Wed;
  
    cout << day;
  
    return 0;
}


Output

2

5. Typedef

C++ allows you to define explicitly new data type names by using the keyword typedef. Using typedef does not create a new data class, rather it defines a name for an existing type. This can increase the portability(the ability of a program to be used across different types of machines; i.e., mini, mainframe, micro, etc; without many changes to the code)of a program as only the typedef statements would have to be changed. Using typedef one can also aid in self-documenting code by allowing descriptive names for the standard data types.

Syntax

typedef typeName;

where typeName is any C++ data type and name is the new name for this data type. This defines another name for the standard type of C++.

Example:

The below program demonstrates the type def in C++.

CPP




// C++ program to demonstrate typedef
#include <iostream>
using namespace std;
  
// After this line BYTE can be used
// in place of unsigned char
typedef unsigned char BYTE;
  
int main()
{
    BYTE b1, b2;
    b1 = 'c';
    cout << " " << b1;
    return 0;
}


Output

 c

Conclusion

In conclusion, C++ has user-defined data types, like classes, structures, unions, enumerations, and typedefs. These user-defined datatypes allow the programmers to easily organize and customize their code, and write modular, efficient, and readable code. Hence, User-defined data types improve the flexibility and structure of C++ programs for better code organization.



Last Updated : 11 Jan, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads