Introduction to User Defined Data Types in C++
User Defined Data type in c++ is a type by which the data can be represented. The type of data will inform the interpreter how the programmer will use the data. A data type can be pre-defined or user-defined. Examples of pre-defined data types are char, int, float, etc. We will discuss user-defined data types in detail.
As the programming languages allow the user to create their own data types according to their needs. Hence, the data types that are defined by the user are known as user-defined data types. For example; arrays, class, structure, union, Enumeration, pointer, etc. These data types hold more complexity than pre-defined data types.
Types of User-Defined Data in C++
1. Array
An Array is defined as a collection of homogeneous or similar data. It should be defined before using it for the storage of information. The array can be defined as follows:
<datatype> <array_name><[size of array]>
int marks[10]
Types of Arrays in C++
There are 3 types of
an array in C++ :
·
One-dimensional array
·
Two-dimensional array
·
Multidimensional array
One-Dimensional Array:
In this type of array,
it stores elements in a single dimension (row or Column ). And, In this array, a single
specification is required to describe elements of the array.
The diagram above shows that it arranged all the elements row-wise in a single dimension, one after the other.
Now, take an example
of a one-dimensional array.
In this example, you are printing the elements 1,7,9,4,5 using for loop,
Two-Dimensional Array:
In this type of array,
two indexes describe each element, the first index represents a row, and the
second index represents a column. its also known as array of array.
Fig: Two-dimensional array
Example of a two-dimensional array.
Multidimensional Array:
The simplest example
of a multidimensional array is a 2-d array; a two-dimensional array also falls
under the category of a multidimensional array. This array can have any number
of dimensions.
The syntax for
declaring a multidimensional array is:
Syntax:
Datatype
array_name [size 1][size 2] . . . . . [size n];
2. Structure
A structure is defined as a collection of various types of related information under one name. The declaration of structure forms a template and the variables of structures are known as members. All the members of the structure are generally related. The keyword used for the structure is “struct”.
For example; a structure for student identity having ‘name’, ‘class’, ‘roll_number’, ‘address’ as a member can be created as follows:
struct stud_id
{
char name[20];
int class;
int roll_number;
char address[30];
};
This is called the declaration of the structure and it is terminated by a semicolon (;). The memory is not allocated while the structure declaration is delegated when specifying the same. The structure definition creates structure variables and allocates storage space for them. Structures variables can be defined as follows:
stud_id I1, I2;
Where I1, I2 are the two variables of stud_id. After defining the structure, its members can be accessed using the dot operator as follows:
I1.roll_number will access roll number of I1
I2.class will access class of I2
Example:
- #include <iostream>
- using namespace std;
- struct Rectangle
- {
- int width, height;
- };
- int main(void) {
- struct Rectangle rec;
- rec.width=8;
- rec.height=5;
- cout<<"Area of Rectangle is: "<<(rec.width * rec.height)<<endl;
- return 0;
- }
3. Union
Just like structures, the union also contain members of different data types. The main difference between the two is that union saves memory as members of a union share the same storage area whereas members of the structure are assigned their own unique storage area. Unions are declared with keyword “union” as follows:
union employee
{
int id;
double salary;
char name[20];
}
The variable of the union can be defined as:
union employee E;
To access the members of the union, the dot operator can be used as follows:
E.salary;
4. Class
A class is an important feature of object-oriented programming language just like C++. A class is defined as a group of objects with the same operations and attributes. It is declared using a keyword “class”. The syntax is as follows:
class <classname>
{
private:
Data_members;
Member_functions;
public:
Data_members;
Member_functions;
};
In this, the names of data members should be different from member functions. There are two access specifiers for classes that define the scope of the members of a class. These are private and public. The member specified as private can be only accessed by the member functions of that particular class only. However, the members defined as the public can be accessed from within and from outside the class also. The members with no specifier are private by default. The objects belonging to a class are called instances of the class. The syntax for creating an object of a class is as follows:
<classname> <objectname>
Example:
class kids
{
public: //Access specifier
char name[10]; //Data members
int age;
void print() //Member function
{
cout<<”name is:”<< name;
}
};
Int main
{
Kids k; //object of class kid is created as k
k.name=”Eash”;
k.print();
return 0;
}
Access Specifiers
Access specifiers define
how the members (attributes and methods) of a class can be accessed. However,
what if we want members to be private and hidden from the outside world?
In C++, there are three
access specifiers:
- public -
members are accessible from outside the class
- private -
members cannot be accessed (or viewed) from outside the class
- protected -
members cannot be accessed from outside the class, however, they can be
accessed in inherited classes.
In the following
example, we demonstrate the differences between public and private members:
Example
#include <iostream>
using namespace std;
class Ram {
public: // Public access specifier
int x; // Public attribute
private: // Private access
specifier
int y; // Private attribute
};
int main() {
Ram Obj;
Obj.x = 25; // Allowed (public)
Obj.y = 50; // Not allowed (private)
return 0;
}
Accessing private member
#include
<iostream>
using
namespace std;
class rj {
// private elements
private:
int age;
// public elements
public:
void displayAge(int a) {
age = a;
cout << "Age = "
<< age << endl;
}
};
int main()
{
int ageInput;
// declare an object
rj obj1;
cout << "Enter your age: ";
cin >> ageInput;
// call
function and pass ageInput as argument
obj1.displayAge(ageInput);
return 0;
}
5. Enumeration
Enumeration is specified by using a keyword “enum”. It is defined as a set of named integer constants that specify all the possible values a variable of that type can have. For example, enumeration of the week can have names of all the seven days of the week as shown below:
Example:
enum week_days{sun, mon, tues, wed, thur, fri, sat};
int main()
{
enum week_days d;
d = mon;
cout << d;
return 0;
}
6. Pointer
A Pointer is that kind of user-defined data type that creates variables for holding the memory address of other variables. If one variable carries the address of another variable, the first variable is said to be the pointer of another. The syntax for the same is:
type *ptr_name;
Here type is any data type of the pointer and ptr_name is the pointer’s name.
Example:
void main()
{
int a = 10;
int *p; // pointer variable is declared
p = &a; // data type of pointer ‘p’ and variable ‘a’ should be same
cout<<"Value at p = ",<<p); // the address of a variable is assigned to a pointer
cout<<"Value at variable a = “,<<a);
cout<<"Value at *p = ",<< *p);
}
7. Typedef
Using the keyword “typedef”, you can define new data type names to the existing ones. Its syntax is:
typedef <type> <newname>;
typedef float balance;
Where a new name is created for float i.e. using balance, we can declare any variable of float type.
The use of a typedef can make the code easy to read and also easy to port to a new machine.
Example:
typedef int score;
int main()
{
score s1, s2;
s1 = 80;
cout << " " << b1;
return 0;
}
0 Comments