Anonymous Objects

maoxiongyi 1 years ago 411 readed 0Fav, Cancel Fav

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...

//---------------------------------------------------------------------------
#include <iostream>
#include <conio>
using namespace std;
#pragma hdrstop
 
//---------------------------------------------------------------------------
#pragma argsused
// This rectangle can be identified by EITHER its location OR its dimensions
union TRectangle
{
struct // Location
{
int x1;
int y1;
int x2;
int y2;
};
struct // Dimensions
{
double Length;
double Height;
};
};
//---------------------------------------------------------------------------
int main(int argc, char* argv[])
{
TRectangle Recto;
char Choice;
 
cout << "You want to identify your rectangle by "
<< "its location or its dimensions?";
cout << "\n1 - Location";
cout << "\n2 - Dimension";
cout << "\nEnter your choice: ";
cin >> Choice;
 
if( Choice == '1' )
{
cout << "\nEnter the location of the rectangle on a "
<< "Cartesian coordinate system\n";
cout << "First point\n";
cout << "X coordinate: "; cin >> Recto.x1;
cout << "Y coordinate: "; cin >> Recto.y1;
cout << "Second point\n";
cout << "X coordinate: "; cin >> Recto.x2;
cout << "Y coordinate: "; cin >> Recto.y2;
cout << "\nThe rectangle spans from A("
<< Recto.x1 << ", " << Recto.y1 << ") to B("
<< Recto.x2 << ", " << Recto.y2 << ").\n";
}
else if( Choice == '2' )
{
cout << "Enter the dimensions of the rectangle\n";
cout << "Length: "; cin >> Recto.Length;
cout << "Height: "; cin >> Recto.Height;
cout << "\nDimensions of the rectangle";
cout << "\nLength: " << Recto.Length;
cout << "\nHeight: " << Recto.Height << endl;
}
else
cout << "\nInvalid Choice";
 
return 0;
}
//---------------------------------------------------------------------------

Tags: