Classes Combinations and Inheritance |
|
Like a regular data type, you can use a class to create a member variable of another class. The primary rules to follow are the same as those of a primitive type. Here is an example: public value class Classification { public: long PropertyNumber; int Condition; __wchar_t Style; }; public value class CHouse { public: Classification Class; __wchar_t TypeOfHome; int Bedrooms; double Bathrooms; Byte Stories; int YearBuilt; double Value; }; After declaring the variable, to access its members, if you had declared it like a regular type, you can access a member using the period operator. This allows you either retrieve the value of the member or to modify it. Here are examples: using namespace System; public value class Classification { public: long PropertyNumber; int Condition; __wchar_t Style; }; public value class CHouse { public: Classification Class; __wchar_t TypeOfHome; int Bedrooms; double Bathrooms; Byte Stories; int YearBuilt; double Value; }; int main() { CHouse ^ condo = gcnew CHouse; condo->Class.PropertyNumber = 697234; condo->Class.Condition = 2; condo->Class.Style = L'H'; condo->YearBuilt = 2002; condo->Bathrooms = 1; condo->Stories = 18; condo->Value = 155825; condo->Bedrooms = 2; condo->TypeOfHome = L'C'; Console::Write("Property #: "); Console::WriteLine(condo->Class.PropertyNumber); Console::Write("Condition: "); Console::WriteLine(condo->Class.Condition); Console::Write("Style: "); Console::WriteLine(condo->Class.Style); Console::Write("Type of Home: "); Console::WriteLine(condo->TypeOfHome); Console::Write("Number of Bedrooms: "); Console::WriteLine(condo->Bedrooms); Console::Write("Number of Bathrooms: "); Console::WriteLine(condo->Bathrooms); Console::Write("Number of Stories: "); Console::WriteLine(condo->Stories); Console::Write("Year Built: "); Console::WriteLine(condo->YearBuilt); Console::Write("Monetary Value: "); Console::WriteLine(condo->Value); Console::WriteLine(); return 0; } This would produce: Property #: 697234 Condition: 2 Style: H Type of Home: C Number of Bedrooms: 2 Number of Bathrooms: 1 Number of Stories: 18 Year Built: 2002 Monetary Value: 155825 Press any key to continue . . . Most of the time, you will declare the member variable as a handle type. To do this, type the ^ operator between the class type and the name of the member variable. Here is an example: public value class CHouse { public: Classification ^ Class; __wchar_t TypeOfHome; int Bedrooms; double Bathrooms; Byte Stories; int YearBuilt; double Value; }; This time, to access a member of that class, you can use the -> operator. Because the member variable is a handle, you must appropriately initialize it on the managed heap. Otherwise the program will not compile. There are various ways you can initialize the variable. From what we have learned so far, you can first create an object of that type, allocated it on the managed heap, and initialize it as you see fit. Here is an example: int main() { CHouse ^condo = gcnew CHouse; Classification ^ type = gcnew Classification; type->PropertyNumber = 697234; // Unique Number type->Condition = 2; // Good, may need minor repair type->Style = L'H'; // High-rise return 0; } Once the object is ready, you can assign it to the member variable that needs the values: int main() { CHouse ^condo = gcnew CHouse; Classification ^ type = gcnew Classification; type->PropertyNumber = 697234; // Unique Number type->Condition = 2; // Good, may need minor repair type->Style = L'H'; // High-rise condo->Class = type; return 0; } When accessing the member variables of that object, remember to use the -> opera tor. Here are examples: using namespace System; public value class Classification { public: long PropertyNumber; int Condition; __wchar_t Style; }; public value class CHouse { public: Classification ^ Class; __wchar_t TypeOfHome; int Bedrooms; double Bathrooms; Byte Stories; int YearBuilt; double Value; }; int main() { CHouse ^condo = gcnew CHouse; Classification ^ type = gcnew Classification; type->PropertyNumber = 697234; // Unique Number type->Condition = 2; // Good, may need minor repair type->Style = L'H'; // Highrise condo->Class = type; condo->YearBuilt = 2002; condo->Bathrooms = 1; condo->Stories = 18; condo->Value = 155825; condo->Bedrooms = 2; condo->TypeOfHome = L'C'; Console::Write("Property #: "); Console::WriteLine(condo->Class->PropertyNumber); Console::Write("Condition: "); Console::WriteLine(condo->Class->Condition); Console::Write("Style: "); Console::WriteLine(condo->Class->Style); Console::Write("Type of Home: "); Console::WriteLine(condo->TypeOfHome); Console::Write("Number of Bedrooms: "); Console::WriteLine(condo->Bedrooms); Console::Write("Number of Bathrooms: "); Console::WriteLine(condo->Bathrooms); Console::Write("Number of Stories: "); Console::WriteLine(condo->Stories); Console::Write("Year Built: "); Console::WriteLine(condo->YearBuilt); Console::Write("Monetary Value: "); Console::WriteLine(condo->Value); Console::WriteLine(); return 0; } When we study methods and constructor, we will see that there are other ways to initi alize a member variable declared as a handle.
The data types we introduced in Lesson 2 each supports a precise type of symbols to make up its value. For example, an integer must contain only digits or characters that can be evaluated to a number. A decimal number can combine only digits and a period for its value. A character can be only one symbol. We also saw that there were precise ways to initialize variables of these types. A string is one or more characters or symbols considered as an entity. The symbols can be digits only. It can be a combination of digits and letters. It can also combine digits, letters, and non-readable characters. This type of definition doesn't fit any of the data types we have seen so far, not even the Object. To be able to consider such a combination as a (whole) value, there are some techniques you must use. Overall, such a combination is considered a string. In the C++/CLI and the .NET Framework, a string is created using the String class. You can declare it as a handle. Here is an example: using namespace System; int main() { String ^ Whatever; return 0; } After declaring the variable, you can initialize it. Because a string can be used to represent any type significant or insignificant value, there are techniques you must use to initialize it. The fundamental technique of initializing a string is to include its value in double-quotes. Here is an example: using namespace System; int main() { String ^ Whatever = "KhjK526Hh6"; return 0; } To display the value of a String object, you can pass it to Write() or WriteLine(). Here is an example: using namespace System; int main() { String ^ Whatever = "KhjK526Hh6"; Console::WriteLine(Whatever); return 0; } This would produce: KhjK526Hh6 Press any key to continue . . . You can also include one or more escape sequences in a String object. Here is an example: using namespace System; int main() { String ^ WhiteHouse = "The White House\n"; Console::WriteLine(WhiteHouse); return 0; } A string can be of almost any length, at least you are allowed to create long strings. When initializing a long string, you can span its value on various lines. Each line must have its pair of quotes. Here is an example: using namespace System; int main() { String ^ WhiteHouse = "The White House" "1600 Pennsylvania Avenue" "Washington, DC"; return 0; } As mentioned already, you can put escape sequences anywhere inside of the string. The compiler would find and interpret them appropriately. Here are examples: using namespace System; int main() { String ^ WhiteHouse = "The White House\n" "1600 Pennsylvania Avenue\n" "Washington, DC"; Console::WriteLine(WhiteHouse); return 0; } This would produce: The White House 1600 Pennsylvania Avenue Washington, DC Press any key to continue . . . Another technique you can use to span a string on various lines with one pair of double-quotes is to end each line with a backslash. Here is an example: int main() { String ^ str = "A Soho, \ dans Londres"; Console::WriteLine(str); return 0; } A string as those we have used so far is considered a regular string. Notice that our strings use only regular characters of a Latin-based language. The C++ language also supports wide strings of the Unicode. To specify that your String value supports Unicode, you can precede its initialization with L. Here is an example: using namespace System; int main() { String ^ Whatever = L"The White House"; Console::WriteLine(WhiteHouse); return 0; } If your initialization spans many lines, start each with its own L. Here are examples: using namespace System; int main() { String ^ Whatever = L"The White House" L"1600 Pennsylvania Avenue" L"Washington, DC"; Console::WriteLine(WhiteHouse); return 0; } The strings displayed in Write() or WriteLine() also support all aspects of the String class. For this reason, you can also start a string of Write() or WriteLine() with L. Just as you can declare a String variable in a function such as main(), you can also create a member of class as a string. To do this, simple declare the String variable in the body of the class.
A string is one of the most sought members of a class. It allows the member variable to carry any type of value, like any combination of characters or symbols included in double-quotes. As stated already, in C++/CLI, a string is mostly identified with the String class. Because the String class is a managed type, it must be declared on the managed heap as a handle. This is done using the ^ operator. Here are examples: public value class CHouse { public: String ^ PropertyNumber; String ^ Address; String ^ City; String ^ State; String ^ ZIPCode; __wchar_t TypeOfHome; int Bedrooms; double Bathrooms; Byte Stories; int YearBuilt; int Condition; __wchar_t Style; double Value; }; To access a String member variable, use the -> operator and you don't have to allocate its memory. Here are example: using namespace System; public value class CHouse { public: String ^ PropertyNumber; String ^ Address; String ^ City; String ^ State; String ^ ZIPCode; __wchar_t TypeOfHome; int Bedrooms; double Bathrooms; Byte Stories; 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 = L'S'; home->Bedrooms = 5; home->Bathrooms = 1; home->Stories = 3; home->YearBuilt = 1992; home->Condition = 2; // Good, may need minor repair home->Style = L'M'; // Highrise home->Value = 555825; Console::Write("Property #: "); Console::WriteLine(home->PropertyNumber); Console::Write("Address: "); Console::WriteLine(home->Address); Console::Write(" "); Console::Write(home->City); Console::Write(", "); Console::Write(home->State); Console::Write(" "); Console::WriteLine(home->ZIPCode); Console::Write("Type of Home: "); Console::WriteLine(home->TypeOfHome); Console::Write("Number of Bedrooms: "); Console::WriteLine(home->Bedrooms); Console::Write("Number of Bathrooms: "); Console::WriteLine(home->Bathrooms); Console::Write("Number of Stories: "); Console::WriteLine(home->Stories); Console::Write("Year Built: "); Console::WriteLine(home->YearBuilt); Console::Write("Condition: "); Console::WriteLine(home->Condition); Console::Write("Style: "); Console::WriteLine(home->Style); Console::Write("Property Value: "); Console::WriteLine(home->Value); Console::WriteLine(); return 0; } This would produce: Property #: 288635 Address: 6808 Lilas Drive Silver Spring, MD 20904 Type of Home: S Number of Bedrooms: 5 Number of Bathrooms: 1 Number of Stories: 3 Year Built: 1992 Condition: 2 Style: M Property Value: 555825 Press any key to continue . . .
|
|
||
Previous | Copyright © 2006-2016, FunctionX, Inc. | Next |
|