Click Here!
home account info subscribe login search FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
[an error occurred while processing this directive]
Previous Table of Contents Next


2.  Defining the class member functions
The class definition is located in a separate file, phbook.cpp. The constructors simply initialize the private data members, and the destructor is empty.
// PhBook.cpp
// PhBook class definition

#include <iostream>
#include <string>
using namespace std;

#include “phbook.h”

PhBook::PhBook()
{
    LastName= “”;
    FirstName= “”;
    City= “”;
    AreaCode= “”;
    Phone= “”;

}

PhBook::PhBook(string InLastName, string InFirstName,
          string InCity, string InAreaCode, string InPhone)
{
    LastName= InLastName;
    FirstName= InFirstName;
    City= InCity;
    AreaCode= InAreaCode;
    Phone= InPhone;

}

PhBook::~PhBook()
{
}

bool PhBook::operator==(const PhBook InPhBook) const
{
if (LastName != InPhBook.LastName) return false;
if (FirstName != InPhBook.FirstName) return false;

return true;

}

bool PhBook::operator>(const PhBook InPhBook) const
{
    if (LastName > InPhBook.LastName) return true;
    if (LastName < InPhBook.LastName) return false;
    if (LastName == InPhBook.LastName)
    {
        if (FirstName > InPhBook.FirstName) return true;
        else return false;
    }

return true;
}

bool PhBook::operator<(const PhBook InPhBook) const
{
    if (LastName < InPhBook.LastName) return true;
    if (LastName > InPhBook.LastName) return false;
    if (LastName == InPhBook.LastName)
    {
        if (FirstName < InPhBook.FirstName) return true;
        else return false;
    }
return false;
}

bool PhBook::Equal(const PhBook InPhBook) const
{
if (LastName != InPhBook.LastName) return false;
if (FirstName != InPhBook.FirstName) return false;
if (City != InPhBook.City) return false;
if (AreaCode != InPhBook.AreaCode) return false;
if (Phone != InPhBook.Phone) return false;

return true;
}

bool PhBook::GreaterThan(const PhBook InPhBook) const
{
    if (LastName > InPhBook.LastName) return true;
    if (LastName < InPhBook.LastName) return false;
    if (LastName == InPhBook.LastName)
    {
       if (FirstName > InPhBook.FirstName) return true;
       if (FirstName < InPhBook.FirstName) return false;
       if (FirstName == InPhBook.FirstName)
       {
         if (City > InPhBook.City) return true;
         if (City < InPhBook.City) return false;
         if (City == InPhBook.City)
         {
           if (AreaCode > InPhBook.AreaCode) return true;
           if (AreaCode < InPhBook.AreaCode) return false;
           if (AreaCode == InPhBook.AreaCode)
           {
               if (Phone > InPhBook.Phone) return true;
               if (Phone < InPhBook.Phone) return false;
               if (Phone == InPhBook.Phone) return true;
               else return false;
           }
         }
     }
    }
return false;
}
bool PhBook::LessThan(const PhBook InPhBook) const
{
    if (LastName < InPhBook.LastName) return true;
    if (LastName > InPhBook.LastName) return false;
    if (LastName == InPhBook.LastName)
    {
       if (FirstName < InPhBook.FirstName) return true;
       if (FirstName > InPhBook.FirstName) return false;
       if (FirstName == InPhBook.FirstName)
       {
           if (City < InPhBook.City) return true;
           if (City > InPhBook.City) return false;
           if (City == InPhBook.City)
           {
               if (AreaCode < InPhBook.AreaCode) return true;
               if (AreaCode > InPhBook.AreaCode) return false;
               if (AreaCode == InPhBook.AreaCode)
               {
                   if (Phone < InPhBook.Phone) return true;
                   if (Phone > InPhBook.Phone) return false;
                   if (Phone == InPhBook.Phone) return true;
                   else return false;
              }
         }
     }
   }
return false;
}
void PhBook::DisplayPhone() const
{
    string FullName, FullPhone;
    FullName = LastName + “, “ + FirstName;
    FullPhone = “(“ + AreaCode + “)” + Phone.substr(0,3)
           ⇒ + “-” + Phone.substr(3,4);
    cout << FullName << “: “ << FullPhone << endl;
}
void PhBook::DisplayCity() const
{
    string FullName;
    FullName = LastName + “, “ + FirstName;
    cout << FullName << “: “ << City << endl;
}

This example uses two sets of comparison functions. The first set is presented by overloaded equality and relational operators (==, >, and <). These functions compare the last name and the first name fields in the phone book entries; this set of functions is used most often. The second set is presented by member functions Equal, GreaterThan, and LessThan. The functions compare all fields in the phone book record and are intended to be used rarely. Therefore, less convenient names are designated for these functions.
To initialize the objects of the PhBook class, use the constructor
PhBook::PhBook(string InLastName, string InFirstName,
           string InCity, string InAreaCode, string InPhone)

To display the objects on the computer screen, use the DisplayPhone and DisplayCity functions.
3.  Preparing simple sorting
For sorting, the last and first names of a person are considered as one entity. The phone book is not usually sorted by last names only, and I can barely imagine that somebody wants to sort the phone book by first names. In the first example, you want to sort phone book entries by last name and then by first name. You want to arrange the entries in alphabetical order, and are not interested in the fields other than the name fields.
In other words, it is sufficient to use the overloaded operator < to specify the sorting order. For the objects defined as
PhBook A, B;

if
A < B

is true, the A object should be before the B object in the sorted sequence of PhBook objects.
The sort algorithm, which uses the < relational operator (or overloaded < operator) to determine the ordering, has a quite simple syntax:
void sort(RandomAccessIterator first,
⇒RandomAccessIterator last);


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.