Dani 4,084 The Queen of DaniWeb Administrator Featured Poster Premium Member

Instances of a Class
An instance of the class is an object which is created based on the class definition. A programmer using a pre-defined (previously written by another programmer) class has the opportunity to use certain functions which have been decided by the programmer of the class.

Perhaps the only simple way to truly understand classes is to actually write many of them. By doing so, you will get a feel for which members to place in the public section and which to place in the private section.

Logic Behind Data Members
In general, variables are placed in the private section when the programmer only wants them to be seen by other functions in the class. Such variables cannot be edited directly from functions outside of the class. In fact, they can't even be viewed outside the class! For this reason, special functions must be written whose sole purpose is to print out or return the data contained in private variables and not to change anything.

Of course, given the particular circumstances, there must also be public functions to make changes to the private data. Last but not least, a constructor function (whose name is, interestingly enough, the same as the name of the class and contains no return type) is used to set the initial values for the private variables when a new instance of the class is created. Constructor functions may or may not have parameters being passed in from outside of the class.

To help you get acquainted with when, exactly, to use the private and public sections of a class, the following chart has been established. It lays out some general ideas of which functions and variables to put where.

Scenerio: a class is written to hold account information for students in a university
Private: variables holding student's name, age, gpa, etc.
Public: functions to return the name, age, or gpa and tamper-proof functions containing error-checks to securely manipulate student's data

Scenerio: a class is written to handle the dvd collection available at a video rental store
Private: variables holding the name of the dvd and a boolean value saying whether or not it is in stock
Public: a function to return the name of the dvd, to return the status of the dvd, to set the InStock variable to false when rented out, and to set the InStock variable to true when returned

Scenerio: a class is written to store the geometrical dimensions of various squares
Private: variables holding the height and width of each square
Public: functions to return the area, perimeter, height, and width of each square

Working With Functions
Functions within a class are known as member functions. One cannot call member functions the same way as one would call other non-member functions. For example, suppose a class is used to keep track of computers in a warehouse. One cannot simply call a member function to, suppose, raise the price. Rather, one would need to specify which instance of the class, which computer, this price raise is affecting.

The following code represents a segment of a program consisting of a class keeping track of a student's record. It begins by outlining a class named student. The private data members contain a given student's age and phone number. Of course, these values cannot be edited directly from outside of the class. They are private and can only be seen from other functions within the class.

class student
{

private:

int age;
int phone;

public:

student(); // constructor fcn
student(int, int); // constructor fcn 2
void set_age(int); // sets age to value
void birthday(); // inc. age by one
void move(int); // change phone number
int get_age(); // returns age
int get_phone(); // returns phone number

};

int main() {

student John;
student Mike(19, 5678);
John.birthday();
Mike.move(9876);
return 0;

}

void student::birthday() {

age++;

}

void student::move(int new_phone) {

phone = new_phone;

}


The public section consists of seven member functions. The first listed is the constructor function. Its sole purpose is to create a new instance of the class. (In other words, to make a new student). In main, John is created using this constructor function. Note that the student John is created, but he consists of empty data. For example, no age or phone number is set. At least this is until the set_age and move functions are called.)

There is also an alternate constructor function in this class. All classes must have at least one constructor function, but they may have more. The difference between each constructor function is that a different number of parameters is being passed in. For example, this alternate constructor function allows for the initial pasisng in of two integers. Note that Mike was created in main using this function. It allows an instance of the class to be created and at the same time sets initial values to the private members of the class.

While the first constructor function contains, in this case, an empty pair of brackets, a constructor function where data is being passed in would set the age and phone variables to their appropriate values (the values being passed into the function.) Of course, initial values may be set when no values are being passed into the constructor function, as well. In such a case, predefined values must be set in the member function. For example, one could write such a function where if neither an age nor phone are passed in, the program defaults to an age of 21 and a phone number of #0000.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.