Anonymous Objects

Please support our C++ advertiser: Intel Parallel Studio Home
meabed meabed is offline Offline Sep 20th, 2004, 8:35 am |
0
hi all .. this is intorduction to an anonymous classes,
An anonymous class is one that doesn't have a name. In the following example, both structures inside the TRectangle union are anonymous classes:

Here is an example of running the program:
You want to identify your rectangle by its location or its dimensions?
1 - Location
2 - Dimension
Enter your choice: 1

Enter the location of the rectangle on a Cartesian coordinate system
First point
X coordinate: -4
Y coordinate: 2
Second point
X coordinate: 5
Y coordinate: 1

The rectangle spans from A(-4, 2) to B(5, 1).

Press any key to continue...
Quick reply to this message  
C++ Syntax
  1. //---------------------------------------------------------------------------
  2. #include <iostream>
  3. #include <conio>
  4. using namespace std;
  5. #pragma hdrstop
  6.  
  7. //---------------------------------------------------------------------------
  8. #pragma argsused
  9. // This rectangle can be identified by EITHER its location OR its dimensions
  10. union TRectangle
  11. {
  12. struct // Location
  13. {
  14. int x1;
  15. int y1;
  16. int x2;
  17. int y2;
  18. };
  19. struct // Dimensions
  20. {
  21. double Length;
  22. double Height;
  23. };
  24. };
  25. //---------------------------------------------------------------------------
  26. int main(int argc, char* argv[])
  27. {
  28. TRectangle Recto;
  29. char Choice;
  30.  
  31. cout << "You want to identify your rectangle by "
  32. << "its location or its dimensions?";
  33. cout << "\n1 - Location";
  34. cout << "\n2 - Dimension";
  35. cout << "\nEnter your choice: ";
  36. cin >> Choice;
  37.  
  38. if( Choice == '1' )
  39. {
  40. cout << "\nEnter the location of the rectangle on a "
  41. << "Cartesian coordinate system\n";
  42. cout << "First point\n";
  43. cout << "X coordinate: "; cin >> Recto.x1;
  44. cout << "Y coordinate: "; cin >> Recto.y1;
  45. cout << "Second point\n";
  46. cout << "X coordinate: "; cin >> Recto.x2;
  47. cout << "Y coordinate: "; cin >> Recto.y2;
  48. cout << "\nThe rectangle spans from A("
  49. << Recto.x1 << ", " << Recto.y1 << ") to B("
  50. << Recto.x2 << ", " << Recto.y2 << ").\n";
  51. }
  52. else if( Choice == '2' )
  53. {
  54. cout << "Enter the dimensions of the rectangle\n";
  55. cout << "Length: "; cin >> Recto.Length;
  56. cout << "Height: "; cin >> Recto.Height;
  57. cout << "\nDimensions of the rectangle";
  58. cout << "\nLength: " << Recto.Length;
  59. cout << "\nHeight: " << Recto.Height << endl;
  60. }
  61. else
  62. cout << "\nInvalid Choice";
  63.  
  64. return 0;
  65. }
  66. //---------------------------------------------------------------------------
0
crestaldin crestaldin is offline Offline | Jul 4th, 2005
hmm this is interesting and educating..do you have any reference where I can learn more on using "Unions"
Thank you
 
 

Message:


Similar Threads
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC