Nested Structures and Array of Structures.pdf - Nested Structures and Array of Structures Suppose we wanted to store data on an employee For each

Nested Structures and Array of Structures.pdf - Nested...

This preview shows page 1 - 3 out of 6 pages.

Nested Structures and Array of Structures Suppose we wanted to store data on an employee. For each employee we need to store an integer employee number, a job type (‘M’: Manager, ‘C’: clerical, ‘S’: salesperson) and a monthly salary. We can create a user defined data type, Employee , which will allow us to combine the different types of data needed to be stored and manipulate them as if they were one unit. The definition of the Employee structure is: struct Employee { int empNum; char jobType; double salary; }; We can declare an Employee structure, e , and assign it values as follows: Enployee e = {1000, 'M', 10000.00}; To print the data of the Employee structure, cout statements can be written to print each field of the structure. Alternatively, we can write a function to print all the data of an employee structure which is passed as a parameter. The given Employee structure would be passed to the function when it is called. The function prints the values of each field of the structure using the dot notation. void printEmployee (Employee e) { cout << "Employee Number : " << e.empNum << endl; cout << "Job Type : " << e.jobType << endl; cout << "Employee Salary : $" << e.salary << endl; } To call the function from the main function, we use the following statement: printEmployee(e); Question 1 Define a structure Date to store the date. Each date has an integer value for the day. an integer value for the month and an integer value for the year. Solution struct Date { int day; int month; int year; };
Image of page 1
Nested Structures and Array of Structures Question 2 Write a function printDate which accepts a Date structure as a parameter and prints the date in the format dd/mm/yyyy. Solution void printDate (Date d) { cout << << "/" << d.month << "/" << d.year << endl; } Suppose we would like to include a field in the Employee structure to store the date that the corresponding employee was hired. We can do this in one of two ways: 1. We can keep use three additional fields to store each component of the date: hireDay , hireM
Image of page 2
Image of page 3

You've reached the end of your free preview.

Want to read all 6 pages?

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes