Program9_1 - 9.1 Advanced Structures Advanced structures...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
9.1 Advanced Structures Advanced structures are a C++ feature that allows one to put functions as field elements into a structure. Consider the datetype from before. Struct datetype { int month; int day; int year; }; struct datetype date; date.month = 7; date.day = 23; date.year = 2003; Now we want to add a function to this structure that will initialize the structure and another function to print the structure. We may then have: Struct datetype { char month[12]; int day; int year; void init(char m, int d, int y) { strcpy (month, m); day = d; year = y; } void print() { printf(“%10s/%2d/%2d”,month, day, year); } }; struct datetype date; Now to initialize the structure instead of doing: Strcpy(date.month,”July”); date.day = 23; date.year = 2003;
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
We now simply use: date.init(“July”, 23, 2003); And to print a the structure we use: date.print(); This reduces the effort on using the structure but more importantly the user of the structure does not need to worry about how to initialize or point the data in the structure since a function to do this is included in the structure.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

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