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?
- Provides flexibility to represent complex data structures.
- Allows better organization of data and related functions.
- Improves code readability and reusability by organizing related data into a single unit.
- Can be used to model real-world objects, such as cars, students, etc., with attributes and behaviors.
Common User-Defined Types
- Class: Used to define complex data structures with attributes (variables) and behaviors (functions).
- Structure: Similar to classes, but by default, all members are public.
- Union: A data structure where all members share the same memory location, but only one member can be accessed at a time.
- Enumeration: A user-defined type that consists of a set of named integer constants.
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
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
Key Differences Between Classes and Structures
- Access Control: By default, all members of a structure are public, whereas all members of a class are private unless specified otherwise.
- Purpose: Structures are typically used for simple data grouping, while classes are used for creating more complex objects with encapsulation and methods.
Advantages of Using User-Defined Types
- Better Code Organization: You can group related data and functions together, making the code more readable.
- Code Reusability: Once a class or structure is defined, it can be reused to create multiple instances of the type.
- Data Encapsulation: Classes allow you to hide data from outside access (encapsulation), ensuring data integrity.
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.