Section 1 - A Student Class

8B.1.1 Adding OOP to Arrays

We've had some interesting examples with arrays and also some fun with classes, but we haven't put them together.  This is going to be a very interesting module for you, because we will place these two important ingredients  into the same pot then light a match.

Let's start by expanding our student array example so that students have first and last names, and also total scores.  Here is an example of a program run.  Our job will be to use our OOP wisdom and our knowledge of arrays and sorting to make this happen.

console shot

Lets figure out what we can tell about this program and what it must be doing.

8B.1.2 The Student Class, Introduced

Let's take a stab at the Student class.  We begin, as always, by defining the class prototype and defining a constructor.

// class Student prototype -----------------------
class Student
{
private:
string lastName;
string firstName;
int totalPoints;

public:
Student(string lst, string fst, long pts);
// more method prototypes will go here ...

};  // end of class Student prototype --------------


// then further down ...


// constructor requires parameters - no default supplied
Student::Student( string lst, string fst, long pts)
{
lastName = firstName = "zz-error";
totalPoints = 0;
// at least require that it starts with a letter
if (lst.length() > 0 && isalpha(lst[0]))
lastName = lst;
if (fst.length() > 0 && isalpha(fst[0]))
firstName = fst;
if (pts >= 0 && pts <= 1000)
totalPoints = pts;
}
Warning - This is not how we will end up defining the constructors and data. It is just a rough design phase to help us get a feel for the class. We will be adding lots of symbolic statics for min values, max values and defaults, as well as some static validation methods.

It's pretty easy, right? We have an int, totalPoints, two strings, lastName and firstName, and some method prototypes with the constructor definition shown.   Now we can see where the zz-error comes from. 

So the client can create Student objects by supplying the initial data right in the instantiation, as in:

Student starPupil("smith", "frederick", 250);

Because we are going to be declaring an array of students, this is not, in fact, how we will instantiate the students, but we could have done this if we wanted an individual student identified by the reference starPupil.

Also, we are not going to supply a default constructor.  This is very important.  It means we cannot instantiate an object like this:

Student lateAdd;

Whenever we create a Student we must supply the data.  The omission of the default constructor is intentional, and one of its implications is that that the client must instantiate a Student by supplying arguments such as name and score.  Not providing a default (i.e., no parameter) constructor is often done if, for instance, we do not want to supply any other mutator method.  It guarantees that all objects of the Student class get data at the start through the constructor.

That's a good introduction to this class.  Let's peek further at our idea of a main program.