• 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
  • 相关阅读:
    现在连Linux都搞不懂,当初我要是这么学习操作系统就好了!
    一时技痒,撸了个动态线程池,源码放Github了
    Java线程池ThreadPoolExecutor使用和分析(一)
    canch----1.对缓存的思考
    1.java 内存数据库--H2数据库使用指南
    What’s the difference between persist, save, merge and update? Which one should you use?
    primary key's generator in JPA entity
    STM32F103驱动M24256 256k存储芯片进行读写
    【Proteus+51单片机学习笔记】-51/52系列单片机简介
    【STM32项目笔记】STM32CubeMX+Keil+Proteus联合实现LED闪烁
  • 原文地址:https://www.cnblogs.com/bq12345/p/3033875.html
Copyright © 2020-2023  润新知