![]() |
VOOZH | about |
User-defined data types in C++ allow programmers to create custom types beyond built-in ones, making code more structured and meaningful. They are widely used to model real-world entities efficiently.
User-defined data types are those data types that are defined by the user. In C++, these data types allow programmers to extend the basic data types provided by the language and create new types that are better suited to specific needs. C++ supports 5 user-defined data types:
A Class is a user-defined data type used in object-oriented programming to group data members and member functions together.
GeeksForGeeks
Explanation: The above program defines a class named GfG with a name attribute and a function printname() to print the name. In the main function, it creates an object named g, sets the geekname as "GeeksforGeeks", and calls the printname() function to display it.
A Structure is a user-defined data type used to group different types of variables under one name.
A: 65
Explanation: The above demonstrates program demonstrates the use of structures by defining a structure named A having i and c members. It then creates an instance if structure in the main function, sets the members' values, and prints them.
Structures in C++ are different from structures in C and resembles classes. Refer to this article to learn more - Difference Between C Structures and C++ Structures
Like structures , union is a special data type where all members share the same memory location.
a.i: 65 a.c: A
Explanation: The above program demonstrates the use of unions. Union named A with members i and c is defined that shares the same memory space. It is shown that when we only assign c some value, the i also stores the same value.
Enumeration (enum) is used to assign names to integral constants.
2
typedef or using are used to create alias names for existing data types.
Float Value: 3.14 Integer Value: 42