Team LiB
Previous Section Next Section

Objects As Attributes

When we first discussed the attributes and methods associated with the Student class, we stated that some of the attributes could be represented by predefined types provided by the C# language, whereas the types of a few others (advisor, courseLoad, and transcript) were left undefined. Let's now put what we've learned about user-defined types to good use.

Rather than declaring the Student class's advisor attribute as simply a string representing the advisor's name, we'll declare it to be of user-defined type—namely, type Professor, another class that we've invented (see Table 3-2).

Table 3-2: Student Class Attributes, Revisited

Attribute

Type

name

string

studentID

string

birthdate

DateTime

address

string

major

string

gpa

double

advisor

Professor

courseLoad

???

transcript

???

By having declared the advisor attribute to be of type Professor—i.e., by making the advisor attribute a reference variable—we've just enabled a Student object to maintain a handle on the actual Professor object that is advising the student. We'll still leave the courseLoad and transcript types unspecified for the time being; we'll see how to handle these a bit later.

The Professor class, in turn, might be defined to have attributes as listed in Table 3-3.

Table 3-3: Student Class Attributes

Attribute

Type

name

string

employeeID

string

birthdate

DateTime

address

string

worksFor

string (or Department)

studentAdvisee

Student

teachingAssignments

???

Again, by having declared the studentAdvisee attribute of Professor to be of type Student—i.e., by making the studentAdvisee attribute a reference variable— we've just given a Professor object a way to hold onto/refer to the actual Student object that the professor is advising. We'll leave the type of teachingAssignments undefined for the time being.

The methods of the Professor class might be as follows:

A few noteworthy points about the Professor class:


Team LiB
Previous Section Next Section