public value struct CHouse { private: __wchar_t TypeOfHome; int Bedrooms; public: double Bathrooms; private: Byte Stories; int YearBuilt; double Value; private: };
Notice that, so far, we didn't signal
any difference between a structure and a class, but there is one. In a
structure, if you don't specify the private and public members, all members considered
public. In a class, if you don't specify the private and public members, all members considered
private. Based on this, to show the level of access of the members of your class
(whether struct or class), precede each with the desired level: private:
or public:.
Notice that you can
create as many public: sections or as many private: sections as you want. When creating a class with different public and private
sections, all of the declared variables under an access level keyword abide by
the rules of that access level. The fact that you use different public sections
does not give different kinds of public levels to the variables. A variable
declared as public in one public section has the same public level of access as
any other variable that is declared in another public section. There is no rule
as to which section should come first. Simply remember that, for a class, if the
top members are not given a level of access, then they are private.
When a member is declared private, you cannot access it
outside of its class. Based on this rule, the following program will not
compile:
using namespace System; public value class CHouse { __wchar_t TypeOfHome; int Bedrooms; double Bathrooms; Byte Stories; int YearBuilt; double Value; }; int main() { CHouse townhouse; townhouse.YearBuilt = 1986; townhouse.Bathrooms = 1.5; townhouse.Stories = 3; townhouse.Value = 348255; townhouse.Bedrooms = 3; townhouse.TypeOfHome = L'T'; return 0; }
Remember that when an access level is not set for a class,
all of its members are private by default. The above program would produce the
following errors:
error C2248: 'CHouse::YearBuilt' : cannot access private member declared in class 'CHouse' see declaration of 'CHouse::YearBuilt' see declaration of 'CHouse' error C2248: 'CHouse::Bathrooms' : cannot access private member declared in class 'CHouse' see declaration of 'CHouse::Bathrooms' see declaration of 'CHouse' error C2248: 'CHouse::Stories' : cannot access private member declared in class 'CHouse' .see declaration of 'CHouse::Stories' see declaration of 'CHouse' error C2248: 'CHouse::Value' : cannot access private member declared in class 'CHouse' see declaration of 'CHouse::Value' see declaration of 'CHouse' error C2248: 'CHouse::Bedrooms' : cannot access private member declared in class 'CHouse' see declaration of 'CHouse::Bedrooms' see declaration of 'CHouse' error C2248: 'CHouse::TypeOfHome' : cannot access private member declared in class 'CHouse' see declaration of 'CHouse::TypeOfHome' see declaration of 'CHouse' error C2248: 'CHouse::TypeOfHome' : cannot access private member declared in class 'CHouse' see declaration of 'CHouse::TypeOfHome' see declaration of 'CHouse' error C2248: 'CHouse::Bedrooms' : cannot access private member declared in class 'CHouse' see declaration of 'CHouse::Bedrooms' see declaration of 'CHouse' error C2248: 'CHouse::Bathrooms' : cannot access private member declared in class 'CHouse' see declaration of 'CHouse::Bathrooms' see declaration of 'CHouse' error C2248: 'CHouse::Stories' : cannot access private member declared in class 'CHouse' see declaration of 'CHouse::Stories' see declaration of 'CHouse' error C2248: 'CHouse::YearBuilt' : cannot access private member declared in class 'CHouse' see declaration of 'CHouse::YearBuilt' see declaration of 'CHouse' error C2248: 'CHouse::Value' : cannot access private member declared in class 'CHouse' see declaration of 'CHouse::Value' see declaration of 'CHouse'
The following program will not compile either:
using namespace System; public value struct CHouse { private: __wchar_t TypeOfHome; int Bedrooms; public: double Bathrooms; private: Byte Stories; int YearBuilt; double Value; private: }; int main() { CHouse townhouse; townhouse.YearBuilt = 1986; townhouse.Bathrooms = 1.5; townhouse.Stories = 3; townhouse.Value = 348255; townhouse.Bedrooms = 3; townhouse.TypeOfHome = L'T'; Console::Write("TypeOfHome: "); Console::WriteLine(townhouse.TypeOfHome); Console::Write("Number of Bedrooms: "); Console::WriteLine(townhouse.Bedrooms); Console::Write("Number of Bathrooms: "); Console::WriteLine(townhouse.Bathrooms); Console::Write("Number of Stories: "); Console::WriteLine(townhouse.Stories); Console::Write("Year Built: "); Console::WriteLine(townhouse.YearBuilt); Console::Write("Monetary Value: "); Console::WriteLine(townhouse.Value); return 0; }
This time, although this is a structure, the members that
have been marked as private are no longer accessible outside of the class.
You can also type-define a class outside of any
function to make the name global.
Just as you can create a constant of a built-in data
type, you can also create a constant of a class. Always
remember that the constant refers to a value that doesn't change and the
constant value must be initialized with a known or already defined value.
Here is an example:
using namespace System; public value class CHouse { public: __wchar_t TypeOfHome; int Bedrooms; double Bathrooms; Byte Stories; int YearBuilt; double Value; }; int main() { const CHouse singleFamily = { L'S', 5, 2.5, 3, 1962, 524885 }; Console::Write("Type of Home: "); Console::WriteLine(singleFamily.TypeOfHome); Console::Write("Number of Bedrooms: "); Console::WriteLine(singleFamily.Bedrooms); Console::Write("Number of Bathrooms: "); Console::WriteLine(singleFamily.Bathrooms); Console::Write("Number of Stories: "); Console::WriteLine(singleFamily.Stories); Console::Write("Year Built: "); Console::WriteLine(singleFamily.YearBuilt); Console::Write("Monetary Value: "); Console::WriteLine(singleFamily.Value); return 0; }
Once the union is defined, you can declare it and use its
members:
using namespace System; union CHouseAge { int YearBuilt; double Age; }; int main() { CHouseAge home; home.YearBuilt = 1972; Console::Write("The house was built in = "); Console::WriteLine(home.YearBuilt); return 0; }
This would produce:
The house was built in = 1972 Press any key to continue . . .
When creating a class of struct or class
type, each variable, treated as its own
entity, occupies its assigned amount of space in memory. This is
different with
unions. All of the members of a union share the same memory space. After
declaring a union object, instead of allocating memory for each member
variable,
the compiler assigns an amount of memory equal to the largest variable.
If you define a union that would represent a
CHouseAge object as the above, the compiler would assign the same memory
space to
all members; and the compiler would find out which member needs the
largest
space. In our example, the Age member variable uses more memory than the
YearBuilt. Therefore, the compiler will allocated the amount of memory
required
by a double, then all member variables would share that memory.
Consequently, a union can store the value of only one of its members at a
time: you cannot simultaneously access the values of all members, at the
same
time. If you initialize all of the members of a union at the same time,
the
result is unreliable; since all members are using the same memory space,
that
space cannot accommodate all member values at the same time.
Because only one member of a union can be considered for an
instance of the object, use a union you are in an "either of one"
scenario. For example, when processing our CHouseAge object, we want Either the
year the house was built OR its age. We don't need both values. In fact this
would create redundancy in the program. Notice that, if we know the year the
house was built, we can deduct its age. On the other hand, if we know the age of
the house, we can calculate the year it was built.
|
Introduction to Classes
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment