SSD5_ Exercise 2分析

这个题文件给了一大堆,其实最关键的只有两个 Listing.h及Group.h

 class Listing and class Group

题目总的来说是比较好的,可惜我运行不成功,所以在此只好把要做的事情做好

先看

Class Listing

Class Listing models a collection of advertisements. This class contains a private data member of type vector<Advertisement*>. This vector stores pointers to Advertisement objects.

Notice from the declaration of class Listing that the keyword typedef has been used to create alternate names for types vector<Advertisement*> and vector<Advertisement*>::iterator. Use these alternate names throughout the auction project.

  • virtual void add(Advertisement* ptr);

Adds the Advertisement pointer given by the parameter to the vector objects.

  • virtual iterator begin();

This returns an iterator to the first Advertisement* in vector objects.

  • virtual iterator end();

This returns an iterator to the last Advertisement* in vector objects.

  • virtual Advertisement* operator[](const int& number);

This returns the Advertisement pointer whose number equals the parameter number. Note, this is not the same as returning the pointer that exists and at index number. 

#ifndef LISTING_H
#define LISTING_H

#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
#include <functional>

#include "Advertisement.h"

using namespace std;

class Listing;

class Listing {

protected:
    typedef vector<Advertisement*> Container;

public:
    typedef Container::iterator iterator;

protected:
    Container objects;

public:
    virtual Advertisement* operator[](const int& number);
    virtual ~Listing() {};

    virtual void add(Advertisement* ptr);

    virtual iterator begin();
    virtual iterator end();

};

#endif

有了上面英文的说明我们可以实现Listing的方法

Listing.cpp

#include "Listing.h"
#include <algorithm>
#include <functional>

using namespace std;
/*
 * This returns the Advertisement pointer whose number equals the parameter number.
 * Note, this is not the same as returning the pointer that exists and at index number.
 */
Advertisement* Listing::operator[](const int& number) {

    Listing::iterator it;

    for (it = this->begin(); it != this->end(); it++) {
        if ((*it)->getNumber() == number) {
            return *it;
        }
    }
    return NULL;
}
//Adds the Advertisement pointer given by the parameter to the vector objects.
void Listing::add(Advertisement* ptr) {
    objects.push_back (ptr);
}
//This returns an iterator to the first Advertisement* in vector objects
Listing::iterator Listing::begin() {
    return objects.begin();
}
//This returns an iterator to the last Advertisement* in vector objects.
Listing::iterator Listing::end() {
    return objects.end();
}

 

Class Group

Class Group models a collection of clients. This class contains a private data member of type vector<Client*>. This vector stores pointers to Client objects.

As in class Listing, the declaration of class Client uses the keyword typedef to create alternate names for types vector<Client*> and vector<Client*>::iterator. Use these alternate names throughout the auction project.

  • virtual void add(Client* ptr);

Adds the Client pointer given by the parameter to the vector objects.

  • virtual iterator begin();

This returns an iterator to the first Client* in vector objects.

  • virtual iterator end();

This returns an iterator to the last Client* in vector objects.

  • virtual Client* operator[](const string& email);

This returns the Client pointer whose object's email equals the parameter email. 

#ifndef GROUP_H
#define GROUP_H

#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
#include <functional>

#include "Client.h"

using namespace std;

class Group;

class Group {

protected:
    typedef vector<Client*> Container;

public:
    typedef Container::iterator iterator;

protected:
    Container objects;

public:
    Client *operator[](const string& email);

    virtual ~Group() {};

    virtual void add(Client* ptr);

    virtual iterator begin();
    virtual iterator end();

};

#endif

 这样我们也能得到Group.cpp

#include "Group.h"
#include <algorithm>
#include <functional>

using namespace std;
//This returns the Client pointer whose object's email equals the parameter email. 
Client* Group::operator[](const string& email) {

    Group::iterator it;
    for (it = this->begin(); it != this->end(); it++) {
        if ((*it)->getEmail() == email) {
            return *it;
        }
    }

    return NULL;
}
//Adds the Client pointer given by the parameter to the vector objects.
void Group::add(Client* ptr) {
    objects.push_back (ptr);
}
//This returns an iterator to the first Client* in vector objects.
Group::iterator Group::begin() {
    return objects.begin();
}
//This returns an iterator to the last Client* in vector objects.
Group::iterator Group::end() {
    return objects.end();
}

这个题就算做完了

Submit only the following.

  1. Listing.cpp - finished implementations of class Listing and Listing::iterator
  2. Group.cpp - finished implementation of class Group and Group::iterator
posted @ 2013-04-21 14:14  强子~Developer  阅读(420)  评论(0编辑  收藏  举报