Coupon Accepted Successfully!



Write the difference between C. style structures an C++ style structure.

C Style Structure C++ Structure
struct <structure tag>


/* member elements declarations;

} ;

struct <structure tag>


[public:] [private] [protected]

/* data members declarations */

/*member functions declarations */


Define Structure.

A C style structure is a collection of variables referenced under one name.


Give an example for structure declaration.

The complete structure definition is as follows:

struct date { short day ;

                   short month ;

                   short year ;

                } ;

date joining-date ;


What in meant by referencing structure element?

Once a structure variable has been defined, its members can be acceded through the use of. The dot operator.


Explain the method for initializing a structure element.

The structure elements of a structure can be initialized either separately, using separate assignment statements or jointly, using the notation similar to array initialization. For instance, the members of structure senior_student as defined in example 13.1 can be initialized separately as shown below:

        senior_student.rollno = 01 ;

        senior_student.class = 12 ;

        senior_student.marks = 50.00 ;

        senior_student.grade = ‘A’ ;

Alternatively, the structure senior_student can be initialized by using the notation used for array initialization, as it is shown below:

        Stutype senior_student = { 02, 12, 50.00, ‘A’ } ;

Note that the second method of structure initialization defined above, can be used only when the structure variable is defined. It cannot be used before the structure variables have been defined. In that case the first method of separate initialization of elements is used.


Discuss nested structure.

A structure element may be either complex or simple. The simple elements are any of the fundamental data types of C++ i.e., int, char, float, double. However, a structure may consist of an element that itself is complex, i.e., it is made up of fundamental types, e.g., arrays, Structures etc. Thus, an element of a structure may even be an array or a structure in itself. A structure consisting of such complex elements is called a complex structure. This section discusses the nested structures i.e., structure within a structure.

A structure can be nested inside another structure. Following code fragment illustrates it:

struct addr

{         int houseno ;

          char area [26] ;

          char city [26] ;

          char state [26] ;

} ;

struct emp

{         int empno ;

          char name [26] ;

          char design [16] ;

          addr address ;

          float basic ;

} ;

emp worker ;

The structure emp has been defined having several elements including a structure address also. The element address (of structure emp) is itself a structure of type addr. While defining such structures, just make sure that inner structures are defined before outer structures.


How will you access the members of nested structure?

The members of structures are accessed using dot operator. To access the city member of address structure which is an element of another structure worker, we shall write


To initialize houseno member of address structure, element of worker structure, we can write as follows:

worker.address.houseno = 1694

The elements of each structure are referenced from outermost to innermost. Following example program reads values into such a nested structure:


Write the two methods for passing values from one function to another.

To pass the values to another function, then it can be achieved in two ways: (i) by passing individual structure elements, and (ii) by passing the entire structure.


Give an example for passing structure elements to a function.

struct date {

                     short day ;

                     short month ;

                     short year ;

                     } Bdate ;

Individual elements of this structure can be passed as follows:

Func1 (Bdate.day, Bdate.month, Bdate.year) ;


What is meant by call by value?

When a structure is used as an argument to a function, the entire structure is passed using the standard call-by-value method. This means that any changes made to the contends of the structure inside the function to which it is passed do not affect the structure used as an argument.


What is meant by call by reference?

Structures can be passed by reference just as other simple types. When a structure is passed by reference the called function declares a reference for the passed structure and refers to the original structure elements through its reference. Thus, the called function works with the original values.


Write a program to illustrate passing of a structure by reference.



struct distance

{      int feet ;

        int inches ;

} ;

int main( )

{      iclrscr( ) ;

        idistance length1, length2 ;

        void prnsum (distance & 11, diatance & 12) ;

        cout << "Enter length 1 :" << "\n" ;

        cout << "Feet :" ;

        cin >> length1. feet ;

        cout << "\n" << "Inches :" ;

        cin >> length1. inches ;

        cout << "\n\n Enter length2 :" << "\n" ;

        cout << "Feet :" ;

        cin >> length2. feet ;

        cout << "\n" << "Inches :" ;

        cin >> length2. inches ;

        prnsum (length1, length2) ;

        return 0 ;


void prnsum (distance & 11, distance 12)

{      distance 13 ;

        13.feet = 11.feet+12. feet+(11.inches+12.inches) /12 ;

        13.inches = (11.inches + 12. inches) % 12 ;

        cout << "\n \n Total Feet :" << 13.feet << "\n" ;

        cout << "Total Inches :" << 13.inches ;

        return ;



Give the syntax for type def data type.

The syntax of the typedef statement is

Typedef type name ;


Give any two processes performed during preprocessing phase.

Many things that can be done during preprocessing phase include:

Inclusion of other files through #include directive

Definition of symbolic constants and macros through #define directive


Write a program to illustrate the purpose of structure.



struct point

{       int x;

         int y;


int main( )


         clrscr( ) ;

         point p1, p2, p3;

         cout << "nEnter coordinates for p1 :" ;

         cin >> p1.x>> p1.y ;

         cout << "\n" << "Enter coordinates for p2 :" ;

         cin >> p2.x >> p2.y ;

         // calculate coordinates of p1+p2 are :"

         p3.x = p1.x + p2.x ;

         p3.y = p1.y + p2.y ;

         cout << "\n"<< "Coordinates of p1+p2 are :"

         << p3.x << "," << p3.y ;

         return 0 ;


Test Your Skills Now!
Take a Quiz now
Reviewer Name