Section 4 - Classes Redefined

6A.4.1 Another Perspective

Since this is all new, I'm going to say the same thing again differently and with a new example.  Hopefully, if you were hazy on any of what I just presented, this will help solidify things for you.

We have seen and used classes before.  Remember this excerpt from a couple weeks ago:

string thxMom;
thxMom = "my Mother, Ethel, and wife Kitty.";

This is an example of our using a pre-defined class, string, in main().  We declared an instance of string object, and called it thxMom.  We also stored the literal "my Mother, Ethel, and wife Kitty." in the object.

The only difference between this string class and the Pet class of the last example is that we (you and I) defined the Pet class, whereas some genius named Bjarne Stroustrup at Bell Labs created the string classes.  We don't see the definitions of this or other pre-defined classes, we just use them.  With Pet, we see the definition because we created it.

Compare the above to our use of the Pet class in main():

Pet mikesDuck;
mikesDuck.petsName = "Daffy";

Do you see?  Same deal (almost).

The word class refers to the blueprint, or data type.

The word object refers to a particular instance of the class.

In our second example, we'll again define a class that contains only data, no methods, and make it clear that the data has some unifying purpose.

Our class will be all basic data that is needed to define an Employee.

6A.4.2 An Employee Class

people walking on street

Class Employee (capital E) will reflect certain aspects of a real employee (lowercase e). Consider, for the sake of our example, that an Employee consisted of three numbers, bundled together. Each number represented something about the employee. The first number is the social security number. The second is the hourly wage. The third is the employee's age. Now we construct a new data type as follows:

class Employee
{
public:
// member data for the class
long socSec;
double wage;
short age;
};

This describes our new data type. You can see that this definition begins with the keywords class. That means that we are defining a new type - not an object yet. The class is the new data type.

The three data inside the new data type are called the data members or members or variables or fields of the class.  As we hinted at in earlier lectures, because the keyword static is missing from their definition, they are called instance variables. If the word static appeared before them they become static class variables.

I'm going to repeat this important concept from the last section:

Instance Variable vs. Static Class Variable

Every time an Employee object is instantiated in main() or some other client, that instance gets its own copy of all these fields:  socSec, wage and age.  That's why they are called instance variables - a new set is created every time a new instance of Employee is created. 

If the keyword static were placed in front of any of the fields, then that field would be a static class member.  The result would be that new Employee instances would not get their own copy of those static members.  All the objects of the class Employee would share one copy of the static class variables.  We don't have any examples in class Employee of static class variables since all the data members are instance variables.

This class demonstrates the creation of a user-defined data type made up of exactly the kinds of primitive data that we need. The idea that we can bundle different data into a user-defined type is called encapsulation.

Now that we have described a new type, we can declare an object of this type. We do this inside a method, just like a local variable, or even above the main() as a global (if we dare).

Employee birkoff, walter;

This defines two Employee variables.  

Here is the general plan:

  1. We define the class.
  2. Then we use the class name to instantiate an object of the class.

Understand, please, that before we declare specific objects like birkoff and walter there are no objects of the type Employee. We only had the notion (blueprint) of what this type is.

Terminology

Declaring an object of a given class is often termed instantiating the class because we are creating a particular instance of the data type described by the class. Some authors use the term instancing or creating an instance.

Finally we want to assign data to the objects, birkoff and walter. In the case of a primitive data type, we would do this:

k = 4;

But in the Employee case, there is more than one elementary data type that comprises it. So we have to explicitly say which one of the three we are interested in. We do this with the dot operator:

birkoff.age = 26;
walter.socSec = 777443333;

or

cout << birkoff.age << " "
<< walter.socSec << endl

We will complete this example in the next section.