/////////////////////////////////////////////////////////////// // exmp1e.cpp - structured data types in C and C++ // // // // Jim Fawcett, ELE591\791, Su'99 // // // // build: cl /GX exmp1e.cpp // /////////////////////////////////////////////////////////////// #include // C++ terminal I/O library #include // C++ stream manipulator library for setw() using namespace std; //----< define a structure of heterogenious types >------------ struct Stuff { double doubleStuff; int intStuff; char charStuff; }; //----< display the Stuff struct >----------------------------- void showStuff(const Stuff &s) { cout << "{ " << s.doubleStuff << ", " << s.intStuff << ", " << s.charStuff << " }\n"; } //----< display an array of doubles >-------------------------- void showDoubleArray(const double da[], int size) { int i; for(i=0; i----------------- void showStuffArray(const Stuff sa[], int size) { int i; for(i=0; i-------------------------- void main() { cout << "\n Testing Structured Data Types " << "\n =============================== \n\n"; //----< define and populate a structure >---- struct Stuff thisStuff; thisStuff.doubleStuff = 3.5; thisStuff.intStuff = -2; thisStuff.charStuff = 'z'; cout << " struct thisStuff = "; showStuff(thisStuff); int dataSize = sizeof(thisStuff); cout << " size of double = " << sizeof(double) << '\n' << " size of int = " << sizeof(int) << '\n' << " size of char = " << sizeof(char) << '\n'; cout << " size of thisStuff = " << dataSize << '\n'; //----< define and populate an array of doubles >---- const int size = 3; double doubleArray[size]; doubleArray[0] = 3.5; doubleArray[1] = 2.5; doubleArray[2] = 1.5; cout << "\n doubleArray = "; showDoubleArray(doubleArray,size); dataSize = sizeof(doubleArray); cout << " size of doubleArray = " << dataSize << '\n'; //----< define and populate an array of structures >---- Stuff StuffArray[size]; StuffArray[0] = thisStuff; StuffArray[1].doubleStuff = 0.5; StuffArray[1].intStuff = -1; StuffArray[1].charStuff = 'Q'; StuffArray[2] = StuffArray[1]; cout << "\n array of Stuff structures:\n"; showStuffArray(StuffArray,size); dataSize = sizeof(StuffArray); cout << " size of StuffArray = " << dataSize << "\n\n"; }