User defined
User-defined Variables
User-defined data types are data types that are defined by the programmer. These data types are created by combining or extending existing primitive data types or other user-defined data types.
User-defined data types allow programmers to define their own custom data structures that suit the requirements of their programs.
These user-defined data types provide a way to encapsulate related data and operations into a single entity, making the code more organised and reusable.
In C++, there are several collective data types available for organising and storing multiple values.
Collective data type:
- Arrays
- Structures (struct)
- Unions
- Enumerations (enum)
- class
Arrays
Arrays are used to store multiple elements of the same data type in contiguous memory locations. The size of an array is fixed during compilation.
int numbers[5] = {1, 2, 3, 4, 5};
char characters[3] = {'a', 'b', 'c'};
Structures (struct)
A structure is a user-defined data type that allows you to group different types of variables under a single name. It can contain variables of different data types, including primitive types, other structures, or even arrays.
struct in c/c++ for combining different data types in one place, struct in C is different than C++, in C++ struct is a class, but the default member is public.
// in C
struct person {
char name[50];
int age;
};
struct person amr = {"Amr", 30};
// Or
typedef struct person{
char name[50];
int age;
} person;
person amr = {"Amr", 30};
//In C++
struct person {
char name[50];
int age
};
struct person amr = {"Amr", 30};
// Or
person amr = {"Amr", 30};
// this allowed without typedef
// because it is class
// and you have made an instance from it.
Unions
A union is a user-defined data type that allows you to store different types of data in a single memory location. It enables you to allocate memory that can be interpreted in multiple ways, depending on the type of data being stored.
union Data {
int intValue;
float floatValue;
char charValue;
};
Data myData;
myData.intValue = 42;
int myValue = myData.intValue;
In this example, the union Data is defined with three members of different types. However, only one member can be active at a time. The size of the union is determined by the largest member, and accessing one member of the union implicitly overrides the others.
Enumerations (enum)
An enumeration, or enum, is a user-defined data type that allows you to define a set of named values. It provides a way to associate constant integral values with symbolic names, making code more readable and maintainable.
enum Color {
RED,
GREEN,
BLUE
};
Color myColor = GREEN;
In this example, the enum type Color is defined with three possible values: RED, GREEN, and BLUE. You can create variables of type Color and assign one of the enum values to them.
Classes (class)
A class is a user-defined data type that provides a blueprint for creating objects. It encapsulates data (member variables) and functions (member functions) that operate on that data. Classes support the concept of data abstraction and encapsulation.
class Circle {
private:
double radius;
public:
Circle(double r) : radius(r) {}
double calculateArea() {
return 3.14159 * radius * radius;
}
};
Circle circle1(5.0); // Creating an instance of the Circle class
double area = circle1.calculateArea();
By defining their own data types using structures and classes, programmers can create complex data structures and encapsulate functionality to represent real-world entities or abstract concepts within their programs. These user-defined data types enhance code organization, reusability, and maintainability.