Program9_1 - 9.1 Advanced Structures Advanced structures...

Info iconThis 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;
Background image of page 1

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

View Full DocumentRight Arrow Icon
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
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/16/2011 for the course EGN 3210 taught by Professor Fernando during the Spring '05 term at University of Central Florida.

Page1 / 5

Program9_1 - 9.1 Advanced Structures Advanced structures...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online