Introduction to C++ | User-Defined Data Types

User-Defined Data Types in C++

C++ allows you to define your own types, also known as user-defined types. These types enable you to model real-world entities or objects more effectively by grouping related data and functions together. The most common user-defined types are classes and structures, but C++ also allows defining other types such as unions and enumerations.

Why Use User-Defined Types?

Common User-Defined Types

Syntax of User-Defined Types

C++ provides syntax to create classes, structures, and other user-defined types. Here's an example of creating a class and a structure:

Class Syntax:

Syntax


        class ClassName {
            // Private data members
            int age;
            string name;
        
        public:
            // Constructor
            ClassName(int a, string n) : age(a), name(n) {}
        
            // Member function
            void display() {
                cout << "Name: " << name << ", Age: " << age << endl;
            }
        };
            

Structure Syntax:

Syntax


        struct StructureName {
            int age;
            string name;
        };
            

Example of User-Defined Type (Class)

Below is an example that defines a class to represent a person with attributes name and age, and a function to display them:

Code Example: Class in C++


        #include <iostream>
        #include <string>
        using namespace std;
        
        class Person {
            // Private data members
            string name;
            int age;
        
        public:
            // Constructor to initialize values
            Person(string n, int a) : name(n), age(a) {}
        
            // Member function to display person details
            void display() {
                cout << "Name: " << name << ", Age: " << age << endl;
            }
        };
        
        int main() {
            // Creating an object of Person class
            Person p1("John", 30);
            
            // Calling member function to display person details
            p1.display();
            
            return 0;
        }
                    

Output

Name: John, Age: 30

Example of User-Defined Type (Structure)

Below is an example that defines a structure to represent a person, with the same name and age attributes:

Code Example: Structure in C++


        #include <iostream>
        #include <string>
        using namespace std;
        
        struct Person {
            string name;
            int age;
        };
        
        int main() {
            // Creating an object of Person structure
            Person p1;
            
            // Initializing the structure members
            p1.name = "Jane";
            p1.age = 25;
            
            // Displaying person details
            cout << "Name: " << p1.name << ", Age: " << p1.age << endl;
            
            return 0;
        }
                    

Output

Name: Jane, Age: 25

Key Differences Between Classes and Structures

Advantages of Using User-Defined Types

Pro Tip:

💡 Pro Tip

For complex data types, it's recommended to use classes instead of structures, as classes allow better control over data access (through private/public members) and can include methods to manipulate data.