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

转载于:https://www.cnblogs.com/bq12345/archive/2013/04/21/3033875.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值