Noeud:Preliminaries, Noeud « Next »:, Noeud « Up »:Containers and Iterators



Preliminaries

Throughout this chapter we'll focus on using objects as elements of containers. Instead of demonstrating the different containers and algorithms (etc.) using primitive data types, we'll instead use a simple C++ class (although, initially with vector, we'll begin with primitive data types).

The examples will revolve around using an address class - albeit very simple (and unrealistic!). Here's the header file, Address.hh:

     /* Address.hh */
     #ifndef Address_hh
     #define Address_hh
     
     #include <string>
     
     class Address
     {
     public:
       Address(){name=street=city=""; phone=0;}
       Address(string n, string s, string c, long p);
       void print() const;
       bool operator < (const Address& addr) const;
       bool operator == (const Address& addr) const;
       string getName() const {return name;}
       string getStreet() const {return street;}
       string getCity() const {return city;}
       long getPhone() const {return phone;}
     private:
       string name;
       string street;
       string city;
       long phone;
     };
     
     #endif
     
     Example 3.1: Address.hh
     

... and the definition, Address.cc:

     /* Address.cc */
     #include "Address.hh"
     
     Address::Address(string n, string s, string c, long p)
     {
       name = n;
       street = s;
       city = c;
       phone = p;
     };
     void Address::print() const
     {
       cout <<
         "Name: " << name << ", " <<
         "Street: " << street << ", " <<
         "City: " << city << ", " <<
         "Phone: " << phone << endl;
     };
     bool Address::operator < (const Address& addr) const
     {
       if (name < addr.getName())
         return true;
       else
         return false;
     };
     bool Address::operator == (const Address& addr) const
     {
       if (name == addr.getName())
         return true;
       else
         return false;
     };
     
     Example 3.2: Address.cc
     

We've added operators because they'll be useful later when we come to sort the elements using different containers. The equality operators are not very strict; we're only interested in comparing names, and consider that two people with the same name to the same person; this isn't really important, given the limited nature of the examples to follow.

In addition, we'll be using a header file to keep a number of different Address objects in:

     /* AddressRepository.hh */
     #ifndef Address_Repoditory_hh
     #define Address_Repoditory_hh
     
     #include "Address.hh"
     
     Address addr1("Jane", "12 Small St.", "Worcs", 225343);
     Address addr2("Edith", "91 Glib Terrace", "Shrops", 858976);
     Address addr3("Adam", "23 Big St.", "Worcs", 443098);
     Address addr4("Jane", "55 Almond Terrace", "Worcs", 242783);
     Address addr5("Bob", "2 St. Annes Walk", "Oxford", 303022);
     
     #endif
     
     Example 3.3: AddressRepository.hh