Classes
- Arrays are collections of homogeneous variables
- all of the variables in an array are of the same type
- If you need a variable which is a collection of variables
of heterogeneous types, we need to use classes
- Situations where we need a heterogeneous collection
- student record
- Name
- Address
- Phone number
- Number of classes she has taken
- Grade point average
- First example from the book is Figure 6.1
- We are defining a new type:
- So far we only used built in types from C++
- The concept of class/struct allows you to create your own
types, user defined types
- In figure 6.1, we have a userdefined type called Time
- When you create your own type, it can only be collection of
previously defined types
- The type Time is a collection of three variables all of them
are of the type int. First variable is called hour, second
is called minute and the third is called second.
- These variables are also called members of the type
- General format of a user defined type is
struct <type-name>{
<members>
};
or
class <type-name>{
<members>
};
struct Time { // structure definition
int hour; // 0-23
int minute; // 0-59
int second; // 0-59
};
- A variable of the type can be declared just like any other
type
Time dinnerTime;
- A variable called dinnerTime of the type Time
- We can access the members of a user-defined type using . notation
- name of the variable is followed by a period followed by the
member name
dinnerTime.hour = 18;
dinnerTime.minute = 30;
dinnerTime.second = 0;
- Any variable of the type Time is going to have three members
- dinnerTime has three slots
hour 18
minute 30
second 0
- The assignment statement dinnerTime.hour puts the value 18
in the hour slot, and so on as shown above.
- Another method of passing parameters (digression)
- The function printMilitary accepts a paramter by constant
reference (const Time &)
- When we pass it by reference we don't make a copy which can
be time consuming for large struct's
- But at the same time const protects the variable from being
changed
- We have the efficiency of passing by reference but protection
of passing by value
- Another digression - conditional expression
- <boolean-expression> ? <value-1> : <value-2>
- if <boolean-expression> is true then the value of the
expression is <value-1> otherwise the value of the expression
is <value-2>
- if (a > b ) max = a; else max = b;
- max = a > b ? a : b;
- printMilitary function accepts a variable of the type Time
and prints it out using Military conventions
- This approach is procedural programming
- A user-defined type is just a collection of data members
- If we want to manipulate those data members we pass these
variables to a function
- The function is not part of the userdefined type
- Object oriented programming changes this view
- Most of the user-defined types cannot be completely defined
without specifying typical operations
- printMilitary should really be the part of the definition
or part of the package. It is not a separate function but integral
part of the type Time
- The first principle of Object Oriented programming is called
encapsulation
- An object is defined in terms of data and function members
- In fact, data abstraction principle says that an object should
be defined only in terms of operations. Data members are irrelevant.
That's the topic for COSC2006.
-