#include "OrderProcessing.h"
COrderProcessing::COrderProcessing(void)
{
_flr = gcnew CFlower;
}
COrderProcessing::~COrderProcessing()
{
delete _flr;
}
double COrderProcessing::GetTotalPrice()
{
return Quantity * _flr->UnitPrice;
}
To create one more source file, on the main menu, click Project -> Add New Item...
In the Templates list, make sure C++ File (.cpp) is selected.
Set the Name to Exercise and click Add
Complete the file as follows:
#include "Flower.h"
#include "OrderProcessing.h"
using namespace System;
COrderProcessing ^ CreateFlowerOrder()
{
COrderProcessing ^ order = gcnew COrderProcessing;
int type, color, qty;
__wchar_t arrangement;
double price;
Console::WriteLine(L"=======================");
Console::WriteLine(L"==-=-=Flower Shop=-=-==");
Console::WriteLine(L"-----------------------");
Console::WriteLine(L"Enter the Type of Flower Order");
Console::WriteLine(L"1. Roses");
Console::WriteLine(L"2. Lilies");
Console::WriteLine(L"3. Daisies");
Console::WriteLine(L"4. Carnations");
Console::WriteLine(L"5. Live Plant");
Console::WriteLine(L"6. Mixed");
Console::Write(L"Your Choice: ");
type = int::Parse(Console::ReadLine());
Console::WriteLine(L"Enter the Color");
Console::WriteLine(L"1. Red");
Console::WriteLine(L"2. White");
Console::WriteLine(L"3. Yellow");
Console::WriteLine(L"4. Pink");
Console::WriteLine(L"5. Orange");
Console::WriteLine(L"6. Blue");
Console::WriteLine(L"7. Lavender");
Console::WriteLine(L"8. Mixed");
Console::Write(L"Your Choice: ");
color = int::Parse(Console::ReadLine());
Console::WriteLine(L"Enter the Type of Arrangement");
Console::WriteLine(L"U. Bouquet");
Console::WriteLine(L"V. Vase");
Console::WriteLine(L"B. Basket");
Console::WriteLine(L"M. Mixed");
Console::Write(L"Your Choice: ");
arrangement = __wchar_t::Parse(Console::ReadLine());
Console::Write(L"Enter the Unit Price: ");
price = double::Parse(Console::ReadLine());
Console::Write(L"Enter Quantity: ");
qty = int::Parse(Console::ReadLine());
CFlower ^ flr = gcnew CFlower(type, color, arrangement, price);
order->Flower = flr;
order->Quantity = qty;
return order;
}
void ShowFlowerOrder(COrderProcessing ^ order)
{
Console::WriteLine(L"=======================");
Console::WriteLine(L"==-=-=Flower Shop=-=-==");
Console::WriteLine(L"-----------------------");
Console::WriteLine(L"Flower Type: {0}", order->Flower->Type);
Console::WriteLine(L"Flower Color: {0}", order->Flower->Color);
Console::WriteLine(L"Arrangement: {0}", order->Flower->Arrangement);
Console::WriteLine(L"Price: {0:C}", order->Flower->UnitPrice);
Console::WriteLine(L"Quantity: {0}", order->Quantity);
Console::WriteLine(L"Total Price: {0:C}", order->GetTotalPrice());
Console::WriteLine(L"=======================");
}
int main()
{
COrderProcessing ^ flower = CreateFlowerOrder();
ShowFlowerOrder(flower);
Console::WriteLine();
return 0;
}
Execute the application and test it. Here is an example:
=======================
==-=-=Flower Shop=-=-==
-----------------------
Enter the Type of Flower Order
1. Roses
2. Lilies
3. Daisies
4. Carnations
5. Live Plant
6. Mixed
Your Choice: 2
Enter the Color
1. Red
2. White
3. Yellow
4. Pink
5. Orange
6. Blue
7. Lavender
8. Mixed
Your Choice: 4
Enter the Type of Arrangement
U. Bouquet
V. Vase
B. Basket
M. Mixed
Your Choice: B
Enter the Unit Price: 65.85
Enter Quantity: 3
=======================
==-=-=Flower Shop=-=-==
-----------------------
Flower Type: 2
Flower Color: 4
Arrangement: B
Price: $65.85
Quantity: 3
Total Price: $197.55
=======================
Press any key to continue . . .
Close the DOS window
Declaring a Boolean Variable
|
|
A variable is referred to as Boolean if it can hold a value
that is either true or false. To declare a Boolean variable, you can use the bool
keyword. Here is an example:
using namespace System;
int main()
{
bool drinkingUnderAge;
return 0;
}
Alternatively, you can declare a Boolean
variable using the Boolean data type. The Boolean data type is part of the
System namespace. Here is an example:
using namespace System;
int main()
{
bool drinkingUnderAge;
Boolean TheFloorIsCoveredWithCarpet;
return 0;
}
To display the value of a Boolean variable on the
console, you can
type its name in the parentheses of the Write() or the WriteLine()
methods of the Console class.
After the variable has been declared, the compiler initializes it with a
false value. This can be demonstrated in the following program:
using namespace System;
int main()
{
bool drinkingUnderAge;
Console::WriteLine(L"Drinking Under Age: ");
Console::WriteLine(drinkingUnderAge);
return 0;
}
This would produce:
Drinking Under Age: False
Press any key to continue . . .
As you can see, by default, a Boolean variable is
initialized with false when it is declared. You can initialize the variable
with true or
false to make sure you know its initial value. Here
is an example:
using namespace System;
int main()
{
bool drinkingUnderAge = true;
Console::WriteLine(L"Drinking Under Age: ");
Console::WriteLine(drinkingUnderAge);
return 0;
}
At any time and when you judge it necessary, you can
change the value of the Boolean variable by assigning it a true or false
value. Here is an example:
using namespace System;
int main()
{
bool drinkingUnderAge = true;
Console::WriteLine(L"Drinking Under Age: ");
Console::WriteLine(drinkingUnderAge);
drinkingUnderAge = false;
Console::WriteLine(L"Drinking Under Age: ");
Console::WriteLine(drinkingUnderAge);
return 0;
}
This would produce:
Drinking Under Age: True
Drinking Under Age: False
Press any key to continue . . .
Retrieving the Value of a Boolean Variable
|
|
As reviewed for the other data types in Lesson
8, you can request the value of a Boolean variable from the user. In this
case, the user must type either True (or true) or False (or false)
and you can retrieve it using the Read() or the ReadLine() methods
of the Console class. Here is an example:
|
|
using namespace System;
int main()
{
bool drivingUnderAge = false;
Console::WriteLine(L"Were you driving under age?");
Console::Write(L"If Yes, enter True. Otherwise enter False: ");
drivingUnderAge = bool::Parse(Console::ReadLine());
Console::WriteLine(L"\nWas Driving Under Age: {0}\n", drivingUnderAge);
return 0;
}
Here is an example of running the program:
Were you driving under age?
If Yes, enter True. Otherwise enter False: true
Was Driving Under Age: True
Press any key to continue . . .
In Windows programming, to expect the user to enter a
specify a value of being true or false, the operating system provides a control
named CheckBox.
Creating a Boolean Member Variable
|
|
Like the other types of variables we used in previous
lessons, a Boolean variable can be a member of a class. You declare it like any
other variable, using the bool keyword or the Boolean data type.
Here is an example:
public ref class CHouse
{
public:
__wchar_t TypeOfHome;
int Bedrooms;
float Bathrooms;
Byte Stories;
bool HasCarGarage;
int YearBuilt;
double Value;
};
When initializing an object that has a Boolean variable as a
member, simply assign true or false to the variable. In the same way, you can
retrieve or check the value that a Boolean member variable is holding by simply
accessing it. Here are examples:
using namespace System;
public ref class CHouse
{
public:
__wchar_t TypeOfHome;
int Bedrooms;
float Bathrooms;
Byte Stories;
bool HasCarGarage;
int YearBuilt;
double Value;
};
int main()
{
CHouse ^ condominium = gcnew CHouse;
condominium->HasCarGarage = false;
condominium->YearBuilt = 2002;
condominium->Bathrooms = 1.5F;
condominium->Stories = 18;
condominium->Value = 155825;
condominium->Bedrooms = 2;
condominium->TypeOfHome = L'C';
Console::WriteLine(L"Type of Home: ");
Console::WriteLine(condominium->TypeOfHome);
Console::WriteLine(L"Number of Bedrooms: ");
Console::WriteLine(condominium->Bedrooms);
Console::WriteLine(L"Number of Bathrooms: ");
Console::WriteLine(condominium->Bathrooms);
Console::WriteLine(L"Number of Stories: ");
Console::WriteLine(condominium->Stories);
Console::WriteLine(L"Year Built: ");
Console::WriteLine(condominium->YearBuilt);
Console::WriteLine(L"Has Car Garage: ");
Console::WriteLine(condominium->HasCarGarage);
Console::WriteLine(L"Monetary Value: ");
Console::WriteLine(condominium->Value);
Console::WriteLine();
return 0;
}
This would produce:
Type of Home: C
Number of Bedrooms: 2
Number of Bathrooms: 1
Number of Stories: 18
Year Built: 2002
Has Car Garage: False
Monetary Value: 155825
Press any key to continue . . .
Like parameters of the other types, you can pass an argument
of type bool or Boolean to a function. Such an argument would be
treating as holding a true or false value.
When creating a Windows control, after using the control, it must be destroyed. In C++, this is
traditionally done using the delete operator in the destructor of the
parent or host of the control, which could be a form. Another detail of Windows
controls is that they use or consume computer resources during their lifetime.
When the controls are not used anymore, such as when their application closes,
these resources should be freed and given back to the operating system to make
them available to other applications. This task can be performed using the Dispose()
method to the Control class, which can then be overridden by its child
controls. The syntax of the Control.Dispose() method is:
protected: void Dispose(bool disposing);
This method takes one argument, disposing, that
indicates how the resources would be released. If this argument is passed with a
false value, only the unmanaged resources would be released. If it is passed as
true, then both managed and unmanaged resources would be released.
During design, we saw that, if you have added a control to
your application but you don't want that control anymore, you could delete it.
In some rare cases, you may also want to allow the user to delete a control
(instead of hiding it). When a control is deleted, the Control::ControlRemoved
event is fired. The syntax of this event is:
public: event ControlEventHandler^ ControlRemoved;
This event is carried by a ControlEventHandler
delegate through the ControlEventArgs class.
A property can be created as a Boolean type. To do this,
first specify its data type as bool. When treating the property, make
sure its get() accessory returns a Boolean type. If its a write property, make
sure you pass a Boolean argument to its set() member.
Practical
Learning: Creating a Boolean Property |
|
- To create a Boolean property, change the Flower.h header file as follows:
#pragma once
public ref class CFlower
{
private:
int _tp;
int _clr;
int _arg;
bool _mx;
double _price;
public:
property int Type
{
int get() { return _tp; }
void set(int tp) { _tp = tp; }
}
property int Color
{
int get() { return _clr; }
void set(int clr) { _clr = clr; }
}
property int Arrangement
{
int get() { return _arg; }
void set(int arg) { _arg = arg; }
}
property bool Mixed
{
bool get() { return _mx; }
void set(bool mx) { _mx = mx; }
}
property double UnitPrice
{
double get() { return _price; }
void set(double price) { _price = price; }
}
public:
CFlower(void);
CFlower(int type, int color,
int argn, bool mx, double price);
~CFlower();
};
- Access the Flower.cpp source file and change it as follows:
#include "Flower.h"
CFlower::CFlower(void)
: _tp(0), _clr(0),
_arg(0), _mx(false),
_price(45.95)
{
}
CFlower::CFlower(int type, int color,
int argn, bool mx,
double price)
: _tp(type),
_clr(color),
_arg(argn),
_mx(mx),
_price(price)
{
}
CFlower::~CFlower()
{
}
- Save all
Consider that, when creating a program for a real
estate company that sells houses, you want the program to ask a customer
the type of house that he or she wants to purchase and/or the type of
garage that the desired house should have. Here is an example:
using namespace System;
int main()
{
int TypeOfHouse = 0;
int TypeOfGarage = 0;
Console::WriteLine(L"Enter the type of house you want to purchase");
Console::WriteLine(L"1 - Single Family");
Console::WriteLine(L"2 - Townhouse");
Console::WriteLine(L"3 - Condominium");
Console::Write(L"Your Choice: ");
TypeOfHouse = int::Parse(Console::ReadLine());
Console::WriteLine(L"Enter the type of garage you want");
Console::WriteLine(L"0 - Doesn't matter");
Console::WriteLine(L"1 - Interior");
Console::WriteLine(L"2 - Exterior");
Console::Write(L"Your Choice: ");
TypeOfHouse = int::Parse(Console::ReadLine());
Console::WriteLine(L"\nYou selected: {0}", TypeOfHouse);
Console::WriteLine(L"Type of Garage: {0}", TypeOfGarage);
return 0;
}
Here is an example of running the program:
Enter the type of house you want to purchase
1 - Single Family
2 - Townhouse
3 - Condominium
Your Choice: 1
Enter the type of garage you want
0 - Doesn't matter
1 - Interior
2 - Exterior
Your Choice: 2
You selected: 2
Type of Garage: 0
Press any key to continue . . .
|
|
For such a program, the numbers can be vague. 1 can
be considered a general number but, in our program, it can represent a
Single Family house or an Interior type of garage. At the same time, our
program uses the constant 1 in particular meaningful ways. To make it
possible to give more meaning to a constant number, when the number can
be made part of a series, C++ allows you to create a type of list.
An enumerator is a series of constant integers that each
has a specific position in the list and can be recognized by a
meaningful name. Based on this, instead of just remembering that the constant
1 represents Single Family, you can create a list that has that type of
house. In another list, instead of using 1 again, you can give
it a name. Consequently, in each list, although the constant 1 would
still be considered, at least it would mean something precise.
To create an enumerator, you use the enum
keyword, followed by the name of the enumerator, followed by a name for
each item of the list. The name of the enumerator and the name of each
item of the list follows the rules
we reviewed for names in C++. The formula of creating an enumeration is:
enum Series_Name {Item1, Item2, Item_n};
Here is an example:
using namespace System;
int main()
{
enum HouseType { Unknown, SingleFamily, TownHouse, Condominium };
return 0;
}
After creating an enumerator, each item in the list is
referred to as a member of the enumerator.
Indexing the Members of an Enumeration
|
|
Each member is assigned a constant
number. The members are counted starting at 0, then 1, etc. By default,
the first member in the enumerator is assigned the number 0, the second is 1,
etc. This is referred to as the index. In fact, you can use Write()
or WriteLine() to show the index of a member. This is illustrated
in the following program:
using namespace System;
int main()
{
enum HouseType { Unknown, SingleFamily, TownHouse, Condominium };
Console::Write(L"Member Index: ");
Console::WriteLine(Unknown);
Console::Write(L"Member Index: ");
Console::WriteLine(SingleFamily);
Console::Write(L"Member Index: ");
Console::WriteLine(TownHouse);
Console::Write(L"Member Index: ");
Console::WriteLine(Condominium);
return 0;
}
This would produce:
Member Index: 0
Member Index: 1
Member Index: 2
Member Index: 3
Press any key to continue . . .
In this HouseType enumeration, the
Unknown member has an index 0. The SingleFamily member has an index of 1. The
TownHouse member has an index of 2.
You can specify or change the numbers to your liking when you
create the enumeration but once the enumeration has been created, whether
you specified these numbers or not, they cannot be changed.
To make
the list start at a specific number, assign the starting value to the
first item in the list. Here is an example:
using namespace System;
int main()
{
enum HouseType { Unknown = 5, SingleFamily, TownHouse, Condominium };
Console::Write(L"Member Index: ");
Console::WriteLine(Unknown);
Console::Write(L"Member Index: ");
Console::WriteLine(SingleFamily);
Console::Write(L"Member Index: ");
Console::WriteLine(TownHouse);
Console::Write(L"Member Index: ");
Console::WriteLine(Condominium);
return 0;
}
This would produce:
Member Index: 5
Member Index: 6
Member Index: 7
Member Index: 8
Press any key to continue . . .
This time, the Unknown member has a value
of 5, the SingleFamily member has a value of 6, etc. When
creating the enumeration, you can assign any value of your choice to any
member of the enumeration, or you can set different ranges of values to various items.
You can create a list like this:
using namespace System;
int main()
{
enum HouseType { Unknown, SingleFamily, TownHouse, Condominium = 6 };
Console::Write(L"Member Index: ");
Console::WriteLine(Unknown);
Console::Write(L"Member Index: ");
Console::WriteLine(SingleFamily);
Console::Write(L"Member Index: ");
Console::WriteLine(TownHouse);
Console::Write(L"Member Index: ");
Console::WriteLine(Condominium);
return 0;
}
This would produce:
Member Index: 0
Member Index: 1
Member Index: 2
Member Index: 6
Press any key to continue . . .
As mentioned already, you can assign any value to
any member of the enumeration. You can even assign the same number to
different members. Here are examples:
using namespace System;
int main()
{
enum HouseType { Unknown = 4, SingleFamily, TownHouse = 12, Condominium };
Console::Write(L"Member Index: ");
Console::WriteLine(Unknown);
Console::Write(L"Member Index: ");
Console::WriteLine(SingleFamily);
Console::Write(L"Member Index: ");
Console::WriteLine(TownHouse);
Console::Write(L"Member Index: ");
Console::WriteLine(Condominium);
return 0;
}
This would produce:
Member Index: 4
Member Index: 5
Member Index: 12
Member Index: 13
Press any key to continue . . .
We mentioned that an enumeration must be an integer type.
This meant that the members can represent Byte, short, int,
long or even char types. As we have demonstrated so far, by
default, the members of an enumeration are of type int. When creating the
elements of an enumeration, you can initialize them to specify the type of
values that they would have. For example, if you want the members to be compared
to character types, initialize them with single-quoted symbols. Here is an
example:
public enum class FlowerArrangement
{
Basket = L'A',
Bouquet = L'U',
Vase = L'V',
Bundle = L'D',
Any
};
Notice you don't have to initialize all members, only those
for whom you want to specify a specific value.
Declaring an Enumeration Variable
|
|
After creating an enumeration, it can be used as a data type
and you can declare a variable from it. One of the formulas you can use
is:
Series_Name VariableName;
Here is an example:
using namespace System;
int main()
{
enum HouseType { Unknown, SingleFamily, TownHouse, Condominium };
HouseType hType;
return 0;
}
When you declare a variable using an enumerator, the
variable is initialized to the value of the first member of the
enumeration. To initialize a variable declared from an enumeration,
assign the name of the desired member
to the variable. Here is an example:
using namespace System;
int main()
{
enum HouseType { Unknown, SingleFamily, TownHouse, Condominium };
HouseType hType = SingleFamily;
Console::Write(L"Enumerator: ");
Console::WriteLine(hType);
return 0;
}
This would produce:
Enumerator: 1
Press any key to continue . . .
As done with the other variables, you can declare
more than one variable of an enumerator type. Here is an example:
using namespace System;
int main()
{
enum HouseType { Unknown, SingleFamily, TownHouse, Condominium };
HouseType hHouse = SingleFamily;
HouseType apart = Condominium;
return 0;
}
Instead of declaring the variable(s) after the list has been
created, you can declare the variables of the enumeration type on the
right side of the list but before the closing semi-colon. Here is an
example:
using namespace System;
int main()
{
enum HouseType
{
Unknown,
SingleFamily,
TownHouse,
Condominium
} hHouse;
hHouse = SingleFamily;
HouseType apart = Condominium;
Console::Write(L"Enumerator: ");
Console::WriteLine(hHouse);
return 0;
}
You can also initialize he variable(s) when
declaring it(them). Here is an example:
using namespace System;
int main()
{
enum HouseType
{
Unknown,
SingleFamily,
TownHouse,
Condominium
} hHouse = SingleFamily, apart = Condominium;
Console::Write(L"Enumerator: ");
Console::WriteLine(hHouse);
return 0;
}
An enumeration is referred to as global when it is created
outside of any function or class. For example, you can create an enumeration
above main() to make it global:
using namespace System;
enum HouseType { Unknown, SingleFamily, TownHouse, Condominium };
int main()
{
return 0;
}
After creating a global enumeration, you can use it as you
see fit. For example, you can use it directly in your source code or you can
declare its variable where you see fit. Here is an example:
using namespace System;
enum HouseType { Unknown, SingleFamily, TownHouse, Condominium };
int main()
{
HouseType hType;
return 0;
}
When we study functions, we will see that a global
enumeration can be accessed from any function of the same source file.
Enumerations and Assemblies
|
|
By default, if you create an enumeration the way we have
proceeded so far, it would be available only in the assembly it belongs to. Just
we reviewed for a class, you can control an enumeration's accessibility outside
of its assembly. This means that you can hide or make it visible outside of its
assembly. To do this, you can precede it with the private or the public keyword.
Here is an example:
using namespace System;
public enum HouseType { Unknown, SingleFamily, TownHouse, Condominium };
int main()
{
HouseType hType;
return 0;
}
An Enumeration as a Member Variable
|
|
If you have created an enumeration, it has characteristics
that resemble those of an integer type. This allows you to use it as a data type
to declare a variable. To create a member variable that is of an enumeration
type, follow the same rules as done for the primitive types: the name of the
enumeration, followed by the name of the variable, and followed by a semi-colon.
Here is an example:
public enum HouseType
{
Unknown,
SingleFamily,
TownHouse,
Condominium
};
public value class CHouse
{
public:
String ^ PropertyNumber;
String ^ Address;
String ^ City;
String ^ State;
String ^ ZIPCode;
HouseType TypeOfHome;
int Bedrooms;
float Bathrooms;
Byte Stories;
bool HasCarGarage;
int YearBuilt;
int Condition;
__wchar_t Style;
double Value;
};
In the same way, you can declare as many enumeration
variables as you want. After declaring the variable, to initialize it, assign it
the desired member of the enumeration. Here is an example:
int main()
{
CHouse ^ home = gcnew CHouse;
home->TypeOfHome = SingleFamily;
. . .
Console::WriteLine();
return 0;
}
Once the member variable has been initialized, you can use
it as you see fit as we will learn and practice in future lessons. At a minimum,
you can pass it to Write() or WriteLine() to display its value. Here is an
example:
using namespace System;
public enum HouseType
{
Unknown,
SingleFamily,
TownHouse,
Condominium
};
public value class CHouse
{
public:
String ^ PropertyNumber;
String ^ Address;
String ^ City;
String ^ State;
String ^ ZIPCode;
HouseType TypeOfHome;
int Bedrooms;
float Bathrooms;
Byte Stories;
bool HasCarGarage;
int YearBuilt;
int Condition;
__wchar_t Style;
double Value;
};
int main()
{
CHouse ^ home = gcnew CHouse;
home->PropertyNumber = "288635";
home->Address = "6808 Lilas Drive";
home->City = "Silver Spring";
home->State = "MD";
home->ZIPCode = "20904";
home->TypeOfHome = SingleFamily;
home->Bedrooms = 5;
home->Bathrooms = 1;
home->Stories = 3;
home->HasCarGarage = true;
home->YearBuilt = 1992;
home->Condition = 2; // Good, may need minor repair
home->Style = L'M'; // High-rise
home->Value = 555825;
Console::WriteLine(L"Property #: {0}", home->PropertyNumber);
Console::WriteLine(L"Address: {0}", home->Address);
Console::WriteLine(L" {0}", home->City);
Console::WriteLine(L", ");
Console::Write(home->State);
Console::WriteLine(L" ");
Console::WriteLine(home->ZIPCode);
Console::WriteLine(L"Type of Home: ");
Console::WriteLine(home->TypeOfHome);
Console::WriteLine(L"Number of Bedrooms: {0}", home->Bedrooms);
Console::WriteLine(L"Number of Bathrooms: {0}", home->Bathrooms);
Console::WriteLine(L"Number of Stories: {0}", home->Stories);
Console::WriteLine(L"Has Car Garage: {0}", home->HasCarGarage);
Console::WriteLine(L"Year Built: {0}", home->YearBuilt);
Console::WriteLine(L"Condition: {0}", home->Condition);
Console::WriteLine(L"Style: {0}", home->Style);
Console::WriteLine(L"Property Value: {0}", home->Value);
Console::WriteLine();
return 0;
}
|
You cannot use the data formatting
features of the curly brackets such as {0} to display the value of an
enumeration. |
This would produce:
Property #: 288635
Address: 6808 Lilas Drive
Silver Spring, MD 20904
Type of Home: SingleFamily
Number of Bedrooms: 5
Number of Bathrooms: 1
Number of Stories: 3
Has Car Garage: True
Year Built: 1992
Condition: 2
Style: M
Property Value: 555825
Press any key to continue . . .
An Enumeration as a Class
|
|
By default, if you declare a variable of an enumeration type
in main(), it would get stored in the stack memory and the compiler would be
in charge of "cleaning up" after it when the program exits. If you
want, you can store the variable in the managed heap so the garbage collector
would be in charge of it. To do this, when creating the enumeration, precede its
name with struct or class. Here is an example:
public enum class HouseType { Unknown, SingleFamily, TownHouse, Condominium };
After creating the enumeration like this, you can use it as
a data type and declare a variable of it.
Here is an example:
using namespace System;
public enum class HouseType { Unknown, SingleFamily, TownHouse, Condominium };
int main()
{
HouseType hType;
return 0;
}
You can also use it to create a member variable of a class
as we saw in the previous section. This time, you use a different technique to
access the members of the enumeration. In reality, and as we will learn when we
study static member variables, the members of
the enumeration become static. To access a member of the enumeration, you must
qualify it. To do this, type the name of the enumeration, followed by the ::
operator, followed by the desired member of the enumeration. Here is an example:
using namespace System;
public enum class HouseType
{
Unknown,
SingleFamily,
TownHouse,
Condominium
};
public value class CHouse
{
public:
String ^ PropertyNumber;
String ^ Address;
String ^ City;
String ^ State;
String ^ ZIPCode;
HouseType TypeOfHome;
int Bedrooms;
float Bathrooms;
Byte Stories;
bool HasCarGarage;
int YearBuilt;
int Condition;
__wchar_t Style;
double Value;
};
int main()
{
CHouse ^ home = gcnew CHouse;
home->PropertyNumber = L"288635";
home->Address = L"6808 Lilas Drive";
home->City = L"Silver Spring";
home->State = L"MD";
home->ZIPCode = L"20904";
home->TypeOfHome = HouseType::SingleFamily;
home->Bedrooms = 5;
home->Bathrooms = 1;
home->Stories = 3;
home->HasCarGarage = true;
home->YearBuilt = 1992;
home->Condition = 2; // Good, may need minor repair
home->Style = L'M'; // High-rise
home->Value = 555825;
Console::WriteLine(L"Property #: {0}", home->PropertyNumber);
Console::WriteLine(L"Address: {0}", home->Address);
Console::Write(L" {0}", home->City);
Console::Write(L", ");
Console::Write(home->State);
Console::Write(L" ");
Console::WriteLine(home->ZIPCode);
Console::Write(L"Type of Home: ");
Console::WriteLine(home->TypeOfHome);
Console::WriteLine(L"Number of Bedrooms: {0}", home->Bedrooms);
Console::WriteLine(L"Number of Bathrooms: {0}", home->Bathrooms);
Console::WriteLine(L"Number of Stories: {0}", home->Stories);
Console::WriteLine(L"Has Car Garage: {0}", home->HasCarGarage);
Console::WriteLine(L"Year Built: {0}", home->YearBuilt);
Console::WriteLine(L"Condition: {0}", home->Condition);
Console::WriteLine(L"Style: {0}", home->Style);
Console::WriteLine(L"Property Value: {0}", home->Value);
Console::WriteLine();
return 0;
}
Practical
Learning: Creating Enumerations |
|
- To create a few enumerations, access the Flower.h header file and change
it with the following:
#pragma once
public enum class FlowerType
{
Roses = 1,
Lilies,
Daisies,
Carnations,
LivePlant,
Mixed
};
public enum class FlowerColor
{
Red = 1,
White,
Yellow,
Pink,
Orange,
Blue,
Lavender,
Mixed
};
public enum class FlowerArrangement
{
Bouquet = 1,
Vase,
Basket,
Mixed
};
public ref class CFlower
{
. . .
};
- Save all
|