User Menu

Login



Share with

User Defined Data Types PDF Print E-mail

User Defined Data Types

The C++ language allows you to create and use data types other than the fundamental data types. These types are called user-defined data types. There are two ways by which a user-defined data type can be created. The first is by using the keyword struct, and the second is by using the keyword class. Data types defined using the keyword struct are called structures.

Structures

In C++, a structure is a collection of variables that are referenced by a single name. The variable can be of different data types. They provide a convenient means of keeping related information together. Structure definition forms a template which is then used to create structure variables. The variables that make up the structure are called structure members.

The following code segment shows how a structure that defines an roll_no and name can be created. The keyword struct tells the compiler that a structure is being declared.
struct student

{
int roll_no;

char name[20];

};
Notice that the structure declaration is terminated by a semi-colon. Also, the structure name student identifies this particular data structure. In this structure no structure variable has been declared.

To declare a variable, you need to use the following statement:

student stu;

This defines a structure's variable of type 'student', called stu. When we declare a structure, we are, in essence, defining a data type. Memory is not allocated for a structure until a variable of that type is created.

The compiler automatically allocates sufficient memory to accommodate all the elements that make up the structure. We can declare structure's variables at the time of declaring a structure.

For example:

struct student

{
int roll_no;

char name[20];

}stu;

will declare a structure type called student and declare the variable stu.

The general form of a structure declaration is:
struct <structure_name>

{
<type> <variable1>;

<type> <variable2>;

...
...
<type> <variableN>;

}<structure vars>;

where <structure_name> is the name of the structure. The structure name is then used to declare structure variables. <structure vars> are the names of structure variables.

The .(dot) Operator

Individual structure elements can be referenced by combining the .(dot) operator and the name of the structure variable. For example, the following statement assigns a value of 10 to the element roll_no of the structure variable stu declared earlier.

stu.roll_no=10;

The general form to access a structure element is: <structure variable>.<element name>

The following program illustrates input and output operations using structures:

//structure example

#include<iostream.h>
struct student

{
int roll_no;

char name[20];

}stu;

int main()

{
cout<<"Enter Roll No:";

cin>>stu.roll_no;
cout<<"Enter Name:";

cin>>stu.name;
cout<<endl<<"Roll NO = "<< stu.roll_no<<endl<<"Name = "

<<stu.name;

return 0;

}

Enter Roll No: 11

Enter Name : Sample

Roll No = 11

Name = Sample

Passing structure variable to a function

(call by-value)
#include <iostream.h>
struct temp

{
  int a, b;
  
} ;

void StructPass(temp var);

int main(void)
{
  temp arg;
  arg.a = 123;

  arg.b = 321;

  StructPass(arg); // Passing structure variable
  return 0;
}
void StructPass(temp var)
{
  cout<<var.a;

  cout<<var.b;

}

Passing structure variable to function using
(call-by-reference)

#include <iostream.h>

struct temp

 {
  int a, b;
 } ;

void StructPass(temp *var);

int main(void)
{
  temp arg;

    // Here the name of  ".(dot)" is Direct Member Selector Operator
  arg.a = 123;

  arg.b = 321;

  StructPass(&arg); // Passing structure variable address

     cout<<"\nInside Main "; 

  cout<<"\n arg.a = "<<arg.a;

  cout<<"\n arg.b = "<<arg.b;

  return 0;
}
void StructPass(temp *var) // Pointer to structure temp
{

/* The -> (Indirect Member Selector) operator is used when a structure element has to be accessed through pointer. */

  var->a=888;

  var->b=999;

  cout<<"\nInside StrutPass "; 

  cout<<"\nvar->a = "<<var->a;

  cout<<"\nvar->b = "<<var->b;

}

Output

Inside StrutPass 

var->a =888

var->b =999

Inside Main 

arg.a =888

arg.b =999;

The -> Operator

The -> (Indirect Member Selector) operator is used when a structure element has to be accessed through a pointer.

Arrays of Structure

#include <iostream.h>
struct student
{
     int age;
     int roll_no;
     char name[20];
};
int main()
{
   student data[3];// Array of 3 Structure variables
   int i = 0;
   cout<<endl<<"Enter age,roll no and name of three students:";
   for(i = 0; i<3 ;i++)
   {
   cin>>data[i].age;
   cin>>data[i].roll_no;
   cin>>data[i].name;
   }
   cout<<endl<<"Your have Entered:";
   for(i = 0; i<3 ;i++)
   {
   cout<<data[i].age<<"\t";
   cout<<data[i].roll_no<<"\t";
   cout<<data[i].name<<endl;
   }
return 0;
}

Passing Array of Structure to a Function

#include <iostream.h>
struct student
{
     int age;
     int roll_no;
     char name[20];
};
int main()
{
   void PassArrayStruct(student data[]);

   student data[3];// Array of 3 Structure variables
   int i = 0;
   cout<<endl<<"Enter age,roll no and name of three students:";
   for(i = 0; i<3 ;i++)
   {
   cin>>data[i].age;
   cin>>data[i].roll_no;
   cin>>data[i].name;
   }

       PassArrayStruct(data);  // Passing Array of Structure variables

       return 0;
}
void PassArrayStruct(student data[])

{  int i;

   cout<<endl<<"Accessing Values Inside PassArrayStruct Function:";
   for(i = 0; i<3 ;i++)
   {
   cout<<data[i].age<<"\t";
   cout<<data[i].roll_no<<"\t";
   cout<<data[i].name<<endl;

   }

}
typedef keyword in C++

In C++, we can declare a variable using one of the built-in data types. In the same way, we can declare an array or a pointer. If we want to use the same data type for many declarations of variables, we can customize its name. The typedef keyword allows us to create an alias for a data type. The syntax to use typedef is:

typedef DataType AliasName;

The typedef keyword is required. The typedef keyword can be followed by any C++ built-in data type, like int, char, double etc. On the right side of the data type, type the name(alias) that will be used to represent the data type. Here is the example:

#include <iostream.h>

typedef short SmallInt;

typedef unsigned long Lint;

int main()

{   SmallInt tdef = 123;

    Lint LI = 321; 

cout << endl <<"Use of typedef short SmallInt" <<tdef;

 cout<<endl<<"Use of typedef unsigned long Lint" <<LI;

 return 0;

}

Output:

Use of typedef short SmallInt  123

Use of typedef unsigned long Lint 321

With typedef short SmallInt and unsigned long Lint can now be used as a data type to declare variables of type small integer and unsigned long integer.

Define preprocessor macros .

Syntax:
#define identifier replacement

When the preprocessor encounters this directive, it replaces any occurrence of identifier in the rest of the code by replacement. This replacement can be an expression, a statement or a block. Preprocessor simply replaces any occurrence of identifier by replacement.
#define SIZE 100

int Number[SIZE];

int Count[SIZE]; 

After the preprocessor has replaced SIZE, the code becomes equivalent to:

int Number[100];

int Count[100];

#define can also work with parameters to define function macros:

#define getmax(a,b) a>b?a:b


This would replace any occurrence of getmax followed by two arguments by the replacement expression.

// #Define Macro Example

#include <iostream.h>

#define getmax(a,b) ((a)>(b)?(a):(b))

int main()


{
  int x=5, y;

  y= getmax(x,2);

  cout << y << endl;

  cout << getmax(7,x) << endl;

  return 0;

}

Output

7

Because preprocessor replacements happen before any C++ syntax check, macro definitions can be a tricky feature so be careful !!!.

scroll back to top