"Student = (name, address, phone_number, major)" Student John, Mary; // Define 2 students John.address // John's address Mary.address // John's address |
struct StructureName { type1 element1; type2 element2; type3 element3; ... }; |
struct myStruct { int i; float f; }; |
After defining the structure, you can define variables of this structure as follows:
struct StructureName variableName; |
struct myStruct x, A[3]; |
class ClassName { [private:|public:|protected:] type1 element1; type2 element2; type3 element3; ... }; |
class myClass { public: int i; float f; }; |
After defining the class, you can define variables of this class as follows:
ClassName variableName; NOTE: no keyword "class" !!! |
A class is a complete type that behave exactly like built-in data types
A class also provides secure data access to special "privileged" functions (called member functions of a class)
NOTE: in the mean time, ALWAYS use public: - it will be explained later...
How C++ define class/object variables: ClassName variableName; Example: ListElem x; |
How Java define class/object variables: ClassName variableName; variableName = new ClassName(); Example: ListElem x; x = new ListElem(); |
C++ allows the programmer to create storage for the object variable in place.
Java always allocate (reserve) space for an object variable using the new operator which returns the location of the variable. The access to the object variable in Java is always indirect
extern struct StructureName variableName; |
extern ClassName variableName; NOTE: no keyword "class" !!! |
x.i |
The component variable "i" in "x" |
&(x.i) |
Address of the component variable "i" in "x" |
x |
The entire object "x" (all components) |
&x |
The address of "x". It is also equal to the address of the first component in "x" |
void Print1(struct myStruct h) { cout << "Structure values = (" << h.i << ", " << h.f << ")\n"; h.i = h.i + 1000; h.f = h.f + 1000; } |
void Print2(myClass h) { cout << "Structure values = (" << h.i << ", " << h.f << ")\n"; h.i = h.i + 1000; h.f = h.f + 1000; } |
void Print1(struct myStruct & h) { cout << "Structure values = (" << h.i << ", " << h.f << ")\n"; h.i = h.i + 1000; h.f = h.f + 1000; } |
void Print2(myClass & h) { cout << "Structure values = (" << h.i << ", " << h.f << ")\n"; h.i = h.i + 1000; h.f = h.f + 1000; } |
The prime example where the de-reference operator * is used with structured variables is maintaining (= accessing, creating and destroying) a "linked list" structure
The "linked list" structure will be discussed as part of the course description; and it is very useful in storing SPARSE matrices
Class variable definition:
class myClass { public: int i; float f; }; myClass x; // x is a // class variable // x has an int and // a float myclass *p; // p is a // reference variable // p holds an // address value |
Using class reference variables
p = &x; // p points to x (*p).i // equivalent to x.i (*p).f // equivalent to x.f p->i // equivalent to (*p).i p->f // equivalent to (*p).f |
From now on, the expression *p is equivalent to x