USER DEFINED DATATYPES IN C/C++
There are some derived data types that are defined by the
user. These are class, structure, union and enumeration. This article briefly
talks about these user-defined derived data types.
USER
DEFINED DATATYPES IN C/C++
Class
A class represents a group of similar objects. To represent
classes in C++, it offers a user defined 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 as a data type bears to a
variable.
To define a class, we 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 class – department, it can represent the
information like name of the department, number of its employees, name of its
head and the 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 :
class department
{
char name[20];
int num_emp ;
char h_o_d [20];
public:
void add();
void Delete();
void modify();
void print();
};
department sales;
The above code defines a class department and the last statement creates an object named sales of
the department class.
Structure
A structure is a collection of variables (of different data
types) referenced under one name, providing of 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 the structure are called structure elements. The keyword
struct is used to construct a structure.
The following code creates a structure for student records :
struct student
{
int rollno;
char name[20];
int Class;
float marks;
char grade;
} ;
student s;
The above declaration declares a structure named student with
structure elements rollno, name, Class,
marks and
grade. The last line creates (i.e.
reserves memory) a structure variable named s of type student.
As it might seem, both class and structure are similar
aspects. You are right. In C++ they only differ depending on their default
behaviour. Any member of a structure is by default public, while any member of
a class is by default private. In fact, a structure is basically a class
defined with struct keyword.
The members of structure or class can be referenced using . (dot
operator) as follows :
object.member;
Example,
s.rollno = 12;
sales.add();
Here, s.rollno accesses rollno member of the variable s (of type student) and
sets its value to 12.
Similarly, sales.add() accesses add() function of the object sales (of class
department).
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 type share :
union share
{
int i;
char ch;
} ;
union share cnvt;
The keyword union is used for declaring and creating a
union. In union cnvt, both integer i & character
ch share
the same memory location. Of course, i being integer takes 4 bytes while ch takes 1
byte. The following figure illustrates the concept :
A union is used when the same value is needed in different
types. For example, if we need 65 at one point of time and ‘A’ at other we may
do it in 2 ways:
First, using the data type casting.
Second, using union.
Using of union is very rare in C++ programs but at times they
do serve the purpose.
Let’s see an example:
cnvt.i = 65;
Once cnvt member is assigned a value, this can be used in
all the data types included in the union. For instance,
cout<< cnvt.i<<endl;
cout<< cnvt.ch;
Output:
65
A
Enumeration
An alternative method for naming integer constants is often
more convenient that const. This can be achieved by creating enumeration using
the keyword enum. For example,
enum{START, PAUSE, GO};
The above code defines 3 integer constants, called enumerators,
and assigns values to them. Enumerator values are by default assigned
increasing from 0, therefore, the above declaration is equivalent to writing
const int START = 0;
const int PAUSE = 1;
const int GO = 2;
An enumeration can be named also
enum status {START, PAUSE, GO};
status i = START;
Here, status acts as a distinct type (const int) and i is initialized with START (i.e. 0).
The enumerator values can be explicitly set by the programmer
also.
enum{START=2, PAUSE, GO};
Here, START is initialized with 2 and all the other
enumerators are initialized increasingly from 2 (i.e. PAUSE = 3
& GO =
4). The rule is that all enumerators are automatically assigned increasing
values till an enumerator is again explicitly initialized.
Which topics you want an article on? Comment down on this
post.
Do not stop here. Read all articles related to C++ here.
If you want to contribute any article for the website, feel
free to share it on coding.nkcoder@gmail.com. We will
publish it on the website with your name.
Download the pdf form of this article here.
Post a Comment