C++ Redux
Volume Number: | | 10
|
Issue Number: | | 1
|
Column Tag: | | Getting Started
|
Related Info: Color Quickdraw
C++ Redux
Rehashing the basics
By Dave Mark, MacTech Magazine Regular Contributing Author
Note: Source code files accompanying article are located on MacTech CD-ROM or source code disks.
Back in the October 93 Getting Started column, we took a look at the basics of object programming using C++. Since the column came out, Ive gotten tons of feedback, from both C++ novices and experts alike. After sorting through all the comments, I took a few days and did a complete rewrite of the October column and of the corresponding chapter (Chapter 5) in Learn C++ on the Macintosh. If youve read the first printing of Learn C++ on the Macintosh (the changes will appear in the second printing) or made your way through the first edition of this column, please take the time to read this new version. As a bonus, this months column includes a C++ program that demonstrates the techniques described throughout the text (the October column didnt include a program).
Objects
There is nothing mysterious about the concept of an object. In C++, an object is any instance of a data type. For example, this line of code:
intmyInt;
declares an int object. This column will teach you how to use C++ to create, destroy and manipulate objects in very powerful ways.
The first object well take a look at is the structure.
The Organizational Power of the Struct
One of the most valuable features shared by C and C++ is the structure. Without the structure, youd have no way to group data that belonged together. For example, suppose you wanted to implement an employee data base that tracked an employees name, employee ID, and salary. You might design a structure that looks like this:
const short kMaxNameSize = 20;
struct Employee
{
char name[ kMaxNameSize ];
long id;
float salary;
};
The great advantage of this structure is that it lets you bundle several pieces of information together under a single name. This concept is known as encapsulation.
For example, if you wrote a routine to print an employees data, you could write:
EmployeenewHire;
PrintEmployee( newHire.name, newHire.id, newHire.salary );
Did you notice anything unusual about the declaration of newHire in the preceding code sample? In C, this code would not have compiled. Instead, the declaration would have looked like this:
struct Employee newHire; /* The C version */
When the C++ compiler sees a structure declaration, it uses the structure name to create a new data type, making it available for future structure declarations.
On the other hand, it would be so much more convenient to pass the data in its encapsulated form:
PrintEmployee( &newHire );
Encapsulation allows you to represent complex information in a more natural, easily accessible form. In the C language, the struct is the most sophisticated encapsulation mechanism available. As you'll soon see, C++ takes encapsulation to a new level.
Encapsulating Data and Functions
While C structures are limited strictly to data, C++ supports structures composed of both data and functions.
Here's an example of a C++ structure declaration:
const short kMaxNameSize = 20;
struct Employee
{
// Data members...
char employeeName[ kMaxNameSize ];
long employeeID;
float employeeSalary;
// Member functions...
void PrintEmployee();
};
This example declares a new type named Employee. You can use the Employee type to declare individual Employee objects. Each Employee object is said to be a member of the Employee class.
The Employee class consists of three data fields as well as a function named PrintEmployee(). In C++, a classes data fields are known as data members and its functions are known as member functions.
Each Employee object you create gets its own copy of the Employee class data members. All Employee objects share a single set of Employee member functions.
Later in the column, youll see how to access an objects data members and member functions. For now, lets take a look at the mechanisms C++ provides to create and destroy objects.
Creating an Object
There are two ways to create a new object. The simplest method is to define the object directly, just as you would a local variable:
Employeeemployee1;
This definition creates an Employee object whose name is employee1. employee1 consists of a block of memory large enough to accomodate each of the three Employee data members.
When you create an object by defining it directly, as we did above, memory for the object is allocated when the definition moves into scope. That same memory is freed up when the object drops out of scope.
For example, you might define an object at the beginning of a function:
void CreateEmployee()
{
Employee employee1;
}
When the function is called, memory for the object is allocated, right along with the functions other local objects. When the function exits, the objects memory is deallocated.
If you want a little more control over when your object is destroyed, take advantage of C++s new operator.
First, define an object pointer, then use new to allocate the memory for your object. new returns a pointer to the newly created object. Heres some code that creates an Employee object:
Employee*employeePtr;
employeePtr = new Employee;
The first line of code defines a pointer designed to point to an Employee object. The second line uses new to create an Employee object. new returns a pointer to the newly created Employee.
Accessing an Objects Members
Once youve created an object, you can modify its data members and call its member functions. If youve defined the object directly, youll refer to its data members using the . operator:
Employeeemployee1;
employee1.employeeSalary = 200.0;
If youre referencing the object through a pointer, use the -> operator:
Employee*employeePtr;
employeePtr = new Employee;
employeePtr->employeeSalary = 200.0;
To call a member function, use the same technique. If the object was defined directly, youll use the . operator:
Employeeemployee1;
employee1.PrintEmployee();
If youre referencing the object through a pointer, youll use the -> operator:
Employee*employeePtr;
employeePtr = new Employee;
employeePtr->PrintEmployee();
The Current Object
In the previous examples, each reference to a data member or member function started with an object or object pointer. Inside a member function, however, the object or object pointer isnt necessary to refer to the object for which the member function is executing.
For example, inside the PrintEmployee() function, you can refer to the data member employeeSalary directly, without referring to an object or object pointer:
if ( employeeSalary <= 200 )
cout << "Give this person a raise!!!";
This code is kind of puzzling. What object does employeeSalary belong to? After all, youre used to saying:
myObject->employeeSalary
instead of just plain:
employeeSalary
The key to this puzzle lies in knowing which object spawned the call of PrintEmployee() in the first place. Although this may not be obvious, a call to a non-static member function must originate with a single object.
Suppose you called PrintEmployee() from a non-Employee function (such as main()). You must precede this call with a reference to an object:
employeePtr->PrintEmployee();
Whenever a member function is called, C++ keeps track of the object used to call the function. This object is known as the current object.
In the call of PrintEmployee() above, the object pointed to by employeePtr is the current object. Whenever this call of PrintEmployee() refers to an Employee data member or function without using an object reference, the current object (in this case, the object pointed to by employeePtr) is assumed.
Suppose PrintEmployee() then called another Employee function. The object pointed to by employeePtr is still considered the current object. A reference to employeeSalary would still refer to the current objects copy of employeeSalary.
The point to remember is, a non-static member function always starts up with a single object in mind.
The This Object Pointer
C++ provides a generic object pointer, available inside any member function, that points to the current object. The generic pointer has the name this. For example, inside every Employee function, the line:
this->employeeSalary = 400;
is equivalent to this line:
employeeSalary = 400;
this is useful when a member function wants to return a pointer to the current object, pass the address of the current object on to another function, or just store the address somewhere. This line of code:
return this;
returns the address of the current object.
Deleting an Object
When you create an object using new, youve got to take responsibility for destroying the object at the appropriate time. Just as a C programmer balances a call to malloc() with a call to free(), a C++ programmer balances each use of the new operator with an eventual use of the delete operator. Heres the syntax:
Employee*employeePtr;
employeePtr = new Employee;
delete employeePtr;
As youd expect, delete destroys the specified object, freeing up any memory allocated for the object. Note that this freed up memory only includes memory for the actual object and does not include any extra memory you may have allocated.
For example, suppose the object is a structure and one of its data members is a pointer to another structure. When you delete the first structure, the second structure is not deleted.
Writing Member Functions
Once your structure is declared, youre ready to write your member functions. Member functions behave in much the same way as ordinary functions, with a few small differences. One difference, pointed out earlier, is that a member function has access to the data members and member functions of the object used to call it.
Another difference lies in the function implementations title line. Heres a sample:
void Employee::PrintEmployee()
{
cout << "Employee Name: " << employeeName << "\n";
}
Notice that the function name is preceded by the class name and two colons. This notation is mandatory and tells the compiler that this function is a member of the specified class.
The Constructor Function
Typically, when you create an object, youll want to perform some sort of initialization on the object. For instance, you might want to provide initial values for your objects data members. The constructor function is C++s built-in initialization mechanism.
The constructor function (or just plain constructor) is a member function that has the same name as the objects class. For example, the constructor for the Employee class is named Employee(). When an object is created, the constructor for that class gets called.
Consider this code:
Employee*employeePtr;
employeePtr = new Employee;
In the second line, the new operator allocates a new Employee object, then immediately calls the objects constructor. Once the constructor returns, the address of the new object is assigned to employeePtr.
This same scenario holds true in this declaration:
Employeeemployee1;
As soon as the object is created, its constructor is called.
Heres our Employee struct declaration with the constructor declaration added in:
const short kMaxNameSize = 20;
struct Employee
{
// Data members...
char employeeName[ kMaxNameSize ];
long employeeID;
float employeeSalary;
// Member functions...
Employee();
void PrintEmployee();
};
Notice that the constructor is declared without a return value. Constructors never return a value.
Heres a sample constructor:
Employee::Employee()
{
employeeSalary = 200.0;
}
This is proper form.
Adding Parameters to Your Constructor
If you like, you can add parameters to your constructor. Constructor parameters are typically used to provide initial values for the objects data members. Heres a new version of the Employee() constructor:
Employee::Employee( char *name, long id, float salary )
{
strncpy( employeeName, name, kMaxNameSize );
employeeName[ kMaxNameSize - 1 ] = '\0';
employeeID = id;
employeeSalary = salary;
}
The constructor copies the three parameter values into the corresponding data members.
The object that was just created is always the constructors current object. In other words, when the constructor refers to an Employee data member, such as employeeName or employeeSalary, it is referring to the copy of that data member in the newly created object.
This line of code supplies the new operator with a set of parameters to pass on to the constructor:
employeePtr = new Employee( "Dave Mark", 1000, 200.0 );
This line of code does the same thing without using new:
Employeeemployee1( "Dave Mark", 1000, 200.0 );
As youd expect, this code creates an object named employee1 by calling the Employee constructor, passing it the three specified parameters.
Just for completeness, heres the class declaration again, showing the new constructor:
struct Employee
{
// Data members...
char employeeName[ kMaxNameSize ];
long employeeID;
float employeeSalary;
// Member functions...
Employee( char *name, long id, float salary );
void PrintEmployee();
};
The Destructor Function
The destructor function is called for you, just as the constructor is. Unlike the constructor, however, the destructor is called when an object in its class is deleted or goes out of scope. Use the destructor to clean up after your object before it goes away. For instance, you might use the destructor to deallocate any additional memory your object may have allocated.
The destructor function is named by a tilda character (~) followed by the class name. The destructor for the Employee class is named ~Employee(). The destructor has no return value and no parameters.
Heres a sample destructor:
Employee::~Employee( void )
{
cout << "Deleting employee #" << employeeID << "\n";
}
If you created your object using new, the destructor is called when you use delete:
Employee*employeePtr;
employeePtr = new Employee;
delete employeePtr;
If your object was defined directly, the destructor is called just before the object is destroyed. For example, if the object was declared at the beginning of a function, the destructor is called when the function exits.
Heres an updated Employee class declaration showing the constructor and destructor:
struct Employee
{
// Data members...
char employeeName[ kMaxNameSize ];
long employeeID;
float employeeSalary;
// Member functions...
Employee( char *name, long id, float salary );
~Employee();
void PrintEmployee();
};
Access Priveleges
When you declare a class, you need to decide which data members and functions youd like to make available to the rest of your program. C++ gives you the power to hide a classes functions and data from all the other functions in your program, or allow access to a select few.
For example, consider the Employee class weve been working with throughout the column. In the current model, an Employees name is stored in a single array of chars. Suppose you wrote some code that created a new Employee, specifying the name, id, and salary, then later in your program you decided to modify the Employees name, perhaps adding a middle name provided while your program was running.
With the current design, you could access and modify the Employees employeeName data member from anywhere in your program. As time passes and your program becomes more complex, you might find yourself accessing employeeName from several places in your code.
Now imagine what happens when you decide to change the implementation of employeeName. For example, you might decide to break the single employeeName into three separate data members, one each for the first, middle and last names. Imagine the hassle of having to pore through your code finding and modifying every single reference to employeeName, making sure you adhere to the brand new model.
C++ allows you to hide the implementation details of a class (the specific type of each data member, for example), funneling all access to the implementation through a specific set of interface routines. By hiding the implementation details, the rest of your program is forced to go through the interface routines your class provides. That way, when you change the implementation, all you have to do is make whatever changes are necessary to the classes interface, without having to modify the rest of your program.
The mechanism C++ provides to control access to your classes implementation is called the access specifier.
Access Specifiers
C++ allows you to assign an access specifier to any of a classes data members and member functions. The access specifier defines which of your programs functions have access to the specified data member or function. The access specifier must be one of public, private, or protected.
If a data member or function is marked as private, access to it is limited to member functions of the same class (or, as youll see later in the chapter, to classes or member functions marked as a friend of the class).
On the flip side, the public specifier gives complete access to the member function or data member, limited only by scope.
By default, the data members and member functions of a class declared using the struct keyword are all public. By adding the private keyword to our class declaration, we can limit access to the Employee data members, forcing the outside world to go through the provided member functions:
struct Employee
{
// Data members...
private:
char employeeName[ kMaxNameSize ];
long employeeID;
float employeeSalary;
// Member functions...
public:
Employee( char *name, long id, float salary );
~Employee();
void PrintEmployee();
};
Once the compiler encounters an access specifier, all data members and functions that follow are marked with that code, at least until another code is encountered. In this example, the three data members are marked as private and the three member functions are marked as public.
The class Keyword
So far, all of our classes have been created using the struct keyword. You can also create classes, using the exact same syntax, substituting the keyword class for struct. The only difference is, the members of a struct are all public by default and the members of a class are all private by default.
Why use class instead of struct? If you start with a struct, you give the world complete access to your class members unless you intentionally limit access using the appropriate access specifiers. If you start with a class, access to your class members is limited right from the start. You have to intentionally allow access by using the appropriate access specifiers.
For the remainder of this book, well use the class keyword to declare our classes. Heres the new version of the Employee class:
class Employee
{
// Data members...
private:
char employeeName[ kMaxNameSize ];
long employeeID;
float employeeSalary;
// Member functions...
public:
Employee( char *name, long id, float salary );
~Employee();
void PrintEmployee();
};
Notice that the private access specifier is still in place. Since the members of a class-based class are private by default, the private access specifier is not needed here, but it does make the code a little easier to read. The public access specifier is necessary, however, to give the rest of the program access to the Employee member functions.
With all that weve covered so far, were about ready for our next sample program. Employee.cp brings these concepts together.
An Object Programming Example
Create a new folder named Employee in your development folder. Then, launch Symantec C++ and create a new project, named Employee.Π, in the Employee folder. Next, select Add Files... from the Project menu and navigate into the Symantec C++ for Macintosh folder and then into the Standard Libraries folder. Youll be adding three libraries to this project. Add the ANSI++, CPlusLib, and IOStreams libraries to the project. When the libraries appear in the project window, drag CPlusLib and IOStreams to a new segment (just click on them, one at a time, and drag them towards the bottom of the project window. The Project Manager will create the new segment for you automatically).
Next, create a new source code file and save it as Employee.cp inside the Employee folder. Add the file to the project. If it doesnt get added to the same segment as CPlusLib and IOStreams, drag it into that segment. ANSI++ should be in one segment and the three other files should be in a different segment.
Heres the source code for Employee.cp:
/* 1 */
#include <iostream.h>
#include <string.h>
const short kMaxNameSize = 20;
class Employee
{
// Data members...
private:
char employeeName[ kMaxNameSize ];
long employeeID;
float employeeSalary;
// Member functions...
public:
Employee( char *name, long id, float salary );
~Employee();
void PrintEmployee();
};
Employee::Employee( char *name, long id, float salary )
{
strncpy( employeeName, name, kMaxNameSize );
employeeName[ kMaxNameSize - 1 ] = '\0';
employeeID = id;
employeeSalary = salary;
cout << "Creating employee #" << employeeID << "\n";
}
Employee::~Employee()
{
cout << "Destroying employee #" << employeeID << "\n";
}
void Employee::PrintEmployee()
{
cout << "-----\n";
cout << "Name: " << employeeName << "\n";
cout << "ID: " << employeeID << "\n";
cout << "Salary: " << employeeSalary << "\n";
cout << "-----\n";
}
intmain()
{
Employee employee1( "Dave Mark", 1, 200.0 );
Employee *employee2;
employee2 = new Employee( "Steve Baker", 2, 300.0 );
employee1.PrintEmployee();
employee2->PrintEmployee();
delete employee2;
return 0;
}
Save your source code, and select Run from the Project menu. Symantec C++ will compile and run your program. Heres what the output should look like:
/* 2 */
Creating employee #1
Creating employee #2
-----
Name: Dave Mark
ID: 1
Salary: 200
-----
-----
Name: Steve Baker
ID: 2
Salary: 300
-----
Destroying employee #2
Destroying employee #1
Lets take a look at the source code.
The employee Source Code
As you look through employee.cp, you should see some familiar sights. This program takes the Employee class described throughout this column through its paces.
The first thing youll notice is the two include files <iostream.h> which is like the C++ version of <stdio.h> (well talk about the iostream library in a later column) and <string.h>, which is needed for the call to strncpy() later in the program:
#include <iostream.h>
#include <string.h>
The const kMaxNameSize and the Employee class declaration are identical to those presented earlier in the column. Notice that the data members are all marked as private (unnecessary, but it does make the code easier to read) while the member functions are marked as public.
const short kMaxNameSize = 20;
class Employee
{
// Data members...
private:
char employeeName[ kMaxNameSize ];
long employeeID;
float employeeSalary;
// Member functions...
public:
Employee( char *name, long id, float salary );
~Employee();
void PrintEmployee();
};
The Employee class has three member functions: a constructor, a destructor, and a utility routine named PrintEmployee(). The constructor, Employee(), uses its three parameters to initialize each of the Employee data members.
Employee::Employee( char *name, long id, float salary )
{
To avoid a possible non-terminated string in the name parameter, well use strncpy() to copy all the bytes from name into employeeName. strncpy() copies kMaxNameSize characters from name to employeeName. If the name string is less than kMaxNameSize characters long, strncpy() will also copy over the null-terminator.
strncpy( employeeName, name, kMaxNameSize );
If name is not null-terminated or is kMaxNameSize bytes long or longer, well stick a null-terminator at the very end of employeeName to ensure that one exists.
employeeName[ kMaxNameSize - 1 ] = '\0';
Finally, well copy the remaining two parameters into their respective data members.
employeeID = id;
employeeSalary = salary;
Once the data members are initialized, the constructor sends a message to the console, telling us which Employee object was just created.
cout << "Creating employee #" << employeeID << "\n";
}
Since no extra memory was allocated, theres not a whole lot for the destructor to do. Just like the constructor, the destructor sends a message to the console, telling us which Employee object will be deleted.
Employee::~Employee()
{
cout << "Deleting employee #" << employeeID << "\n";
}
PrintEmployee() displays the contents of the three data members of the current object:
void Employee::PrintEmployee()
{
cout << "-----\n";
cout << "Name: " << employeeName << "\n";
cout << "ID: " << employeeID << "\n";
cout << "Salary: " << employeeSalary << "\n";
cout << "-----\n";
}
main() is the control center, where all the action is. First, we define an Employee object, passing three parameters to the constructor:
intmain()
{
Employee employee1( "Dave Mark", 1, 200.0 );
As the Employee constructor is called, it displays the following line on the console:
Creating employee #1
Next, an Employee object pointer is defined:
Employee *employee2;
This time, new is used to create a second Employee object:
employee2 = new Employee( "Steve Baker", 2, 300.0 );
Once again, the Employee constructor is called, sending another line to the console:
Creating employee #2
Now, both objects are used to call the PrintEmployee() member function. employee1 is an object and uses the . operator to access its member function. Since employee2 is a pointer and uses the -> operator to access the PrintEmployee() function:
employee1.PrintEmployee();
employee2->PrintEmployee();
These two calls result in the following output:
-----
Name: Dave Mark
ID: 1
Salary: 200
-----
-----
Name: Steve Baker
ID: 2
Salary: 300
-----
Next, the object pointed to by employee2 is deleted:
delete employee2;
}
This causes employee2s destructor to be called, resulting in this line of output:
Destroying employee #2
Finally, main() exits and all of main()s local variables (including employee1) are deallocated. As soon as employee1 was deallocated, its destructor was called, resulting in a final line of output being sent to the console:
Destroying employee #1
Notice that employee1s destructor wasnt called till main() had exited.
Take another look at your programs output. If you like, go run the program again. Notice that every single line of output was produced by an objects member function. Although you did call PrintEmployee() directly, the constructor and destructor functions were called for you when you created and deleted an object.
Consider the line of code used to delete an Employee object:
delete employee1;
This line of code does not contain a function call. It does not contain code that prints information to the console. Even so, a function call was made (the destructor function, called for you). A line of output was sent to the console.
The point here is that theres action going on behind the scenes. Stuff happens automatically. You delete an object, the destructor gets called for you. This might seem like a minor point, but this is your first peek at the power of object programming.
Till Next Month
Interested in more C++ coverage? Let me know. You can write to me c/o MacTech magazine at the addresses listed on page 2 of the magazine (Under the heading How to communicate with Xplain Corporation). In the meantime, Ill go back to the Mac Toolbox and more Color QuickDraw in next months column. See you then...