User-defined data types in C++ with examples.

Q2. Explain user-defined data types in C++ with appropriate examples.

Ans. There are some derived data types that are defined by the user. These are: Class, Structure, Union and Enumeration.

  1. Class

A class represents a group of similar objects. To represent classes in C++, it offers a user-defined data type called class. Once a class has been defined in C++, objects belonging to that class can easily be created. A class bears the same relationship to an object that a type does to a variable.

To define a class you describe what sort of information it can represent and what sort of actions you can perform with that data. For instance, if you want to create a class department. It can represent information like name of the department, number of its employee, name of its head and actions that can be performed with this information are : addition, deletion, modification of employee data, printing of department report etc.. now this class will be declared in C++ as follows:


class department


char name[20];

int num_emp;

char h_o_d[20];







department sales, purchase, import, accounts;

The above declaration declares a class department and the last line declares objects (sales, purchase, import and account) of class type department.

  1. Structure

A structure is a collection of variables (of different data types) referenced under one name, providing a convenient means of keeping related information together. For example, a student record is collection of rollno, name, class, marks, grade etc. a structure definition forms a template that may be used to create structure variables. The variables that make up thee structure are called structure elements. The keyword struct is used to construct a structure. The following example creates a structure for student record:

struct sturec


int rollno;

char name[20];

int class;

float marks:

char grade;


sturec newstu;


The above declaration declares a structure named sturec with structure elements rollno, name, class, marks and grade. The last line creates (that is, reserves memory) a structure object named newstu of structure type sturec.

The elements of structure are referred to as follows:

newstu.rollno = 10

newstu.class = 12

that is, name-of the-structure-object. Element-name.


  1. Union

A union is a memory location that is shared by two or more different variables, generally of different types at different times. Defining a union is similar to defining a structure. Following declaration declares a union share having two variables (integer and character type) and creates a union object cnvt of union type share:

union share


int i;

char ch;


union share cnvt;


the keyword union is used for declaring and creating a union cnvt, both integer I and character ch share the same memory location. (of course, i (being integer) occupies 2 bytes and ch (being character) uses only 1 byte.

At any point, you can refer to the data stored in a cnvt as either an integer or a character. To assign the integer 20 to element i of cnvt, write

cnvt.i = 20;

To print the value of element ch of cnvt, write



  1. Enumeration

An alternative method for naming integer constants is often more convenient than const. this can be achieved by creating enumeration using keyword enum. For example,


defines three integer constants, called enumerators, and assigns values to them. Enumerator values are by default assigned increasing from 0, therefore, the above declarations is equivalent to writing

const int START = 0;

const int PAUSE = 1;

const int GOO = 2;

An enumeration can be named also. For instance:

Enum status {START,PAUSE,GOO};

The name of the enumeration becomes a distinct type.

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: