c++ - 在 C++ 中将类实例添加到对象层次结构的问题

标签 c++ oop object vector hierarchy

在我们的项目中,我们需要创建三个类:Customers、Campaigns 和 Advertisements。每个客户都有一个事件 vector ,每个事件都有一个广告 vector :

客户 -> 事件 -> 广告

当我们尝试将 Advertisement 实例添加到 给定客户的事件。

当我们尝试将广告添加到给定的广告系列时,我们永远看不到它 实际上添加了广告。广告数量保持不变。

我们都是 C++ 的新手,不确定如何使用我们认为与问题相关的引用来解决这个问题。

客户类:

#pragma once
#include <vector>
#include <cstdio>
#include <string>
#include "campaign.h"

using namespace std;

class Customer
{
    string name;
    int id;
    vector<Campaign> campaigns;
public:


    Customer(string name, int id)
    {
        this->name = name;
        this->id = id;
    }

    string GetName()
    {
        return name;
    }

    void SetName(string name)
    {
        this->name = name;
    }

    int GetId()
    {
        return id;
    }

    void SetId(int id)
    {
        this->id = id;
    }

    bool AddCampaign(Campaign campaignObject)
    {
        campaigns.push_back(campaignObject);
        return true;
    }

    bool CommitAdvertisement(Ad ad, int campaignID)
    {
        for (int i = 0; i < campaigns.size(); i++)
        {
            if (campaigns[i].GetId() == campaignID)
            {
                campaigns[i].CommitAdvertisement(ad);
                return true;
            }
        }
        return false;
    }

    bool hasActiveCampaigns()
    {
        for (Campaign i : campaigns)
        {
            if (i.IsActive())
            {
                return true;
            }
        }
        return false;
    }

    vector<Campaign> GetAllCampaigns()
    {
        return campaigns;
    }

    vector<Ad> GetAllAdsForCampaign(int campaignID)
    {
        for (int i = 0; i < campaigns.size(); i++)
        {
            if (campaigns[i].GetId() == campaignID)
            {
                return campaigns[i].GetAllAds();
            }
        }
    }
};

事件类:

#pragma once
#include <ctime>
#include <string>
#include <vector>
#include <iostream>
#include <exception>
#include <ctime>
#include "Ad.h"

using namespace std;


class Campaign
{
    string name;
    int id;
    time_t fromDateTime;
    time_t toDateTime;
    float campaignCost;
    vector<Ad> ads;

public:
    Campaign(time_t fromDateTime, time_t toDateTime, int id, string name, float campaignCost)
    {
        this->fromDateTime = fromDateTime;
        this->toDateTime = toDateTime;
        this->id = id;
        this->name = name;
        this->campaignCost = campaignCost;
    }

    time_t GetFromDateTime()
    {
        return fromDateTime;
    }

    void SetFromDateTime(time_t fromDateTime)
    {
        this->fromDateTime = fromDateTime;
    }

    time_t GetToDateTime()
    {
        return toDateTime;
    }

    void SetToDateTime(time_t toDateTime)
    {
        this->toDateTime = toDateTime;
    }

    int GetId()
    {
        return id;
    }

    void SetId(int id)
    {
        this->id = id;
    }

    string GetName()
    {
        return name;
    }

    void SetName(string name)
    {
        this->name = name;
    }

    float GetCampaignCost()
    {
        return campaignCost;
    }

    void SetCampaignCost(float campaignCost)
    {
        this->campaignCost = campaignCost;
    }

    bool IsActive()
    {
        time_t now = time(NULL);

        if (fromDateTime <= now && toDateTime >= now)
        {
            return true;
        }
        return false;
    }

    bool CommitAdvertisement(Ad ad)
    {
        for (Ad i : ads)
        {
            if (i.GetId() == ad.GetId())
            {
                return false;
            }
        }
        ads.push_back(ad);
        return true;
    }

    bool DeleteAdvertisement(int id)
    {
        for (int i = 0; i < ads.size(); i++)
        {
            if (ads[i].GetId() == id)
            {
                ads.erase(ads.begin() + i);
                return false;
            }
        }
        return false;
    }

    vector<Ad> GetAllAds()
    {
        return ads;
    }
};

广告类

#pragma once
#include <string>
#include <vector>
#include <iostream>
#include <exception>
#include "AdType.h"

using namespace std;

class Ad
{

private:

    string name;
    string adText;
    AdType adType; 
    int id;

public:

    Ad(string name, string text, int id, AdType type = AdType::PLAINTEXT)
    {
        this->name = name;
        this->adText = text;
        this->id = id;
        this->adType = type;
    }

    string GetName()
    {
        return name;
    }

    void SetName(string name)
    {
        this->name = name;
    }

    int GetId()
    {
        return id;
    }

    void SetId(int id)
    {
        this->id = id;
    }

    AdType GetType()
    {
        return adType;
    }

    void SetType(AdType type)
    {
        this->adType = type;
    }

    string GetText()
    {
        return adText;
    }

    void SetText(string adText)
    {
        this->adText = adText;
    }
};

现在,我们已经成功地向给定客户添加了一个广告系列,并且能够添加一个 广告实例,但不能再添加了。

此代码片段显示了此尝试:

    // Create a customer
    Customer *the_client = new Customer("Foobar Inc", 123);

    // Create a campaign
    Campaign *the_campaign = new Campaign(begin, end, 1234, "campaign_name", 123455.0f);

    // Create an ad
    Ad *the_first_ad = new Ad("First ad", "adtext", 12345656, AdType::PLAINTEXT);

    // create another ad
    Ad* the_second_ad = new Ad("Second ad", "adtext", 12345656, AdType::PLAINTEXT);

    // add the campaign to the customer
    the_client->AddCampaign(*the_campaign);

    // Add the ad to the customers' list of ads
    the_client->CommitAdvertisement(*the_first_ad, 1234);

    // Print out how many ads there are in the given campaign
    cout << "There are now " << the_client->GetAllAdsForCampaign(1234).size() << "ads int the campaign" << endl;

    // Add the second ad to the customers' list of ads
    the_client->CommitAdvertisement(*the_second_ad, 1234);

    // Again - print out how many ads there are in the given campaign
    cout << "There are now " << the_client->GetAllAdsForCampaign(1234).size() << "ads int the campaign" << endl;

很有可能我们完全做错了,但我们非常欢迎任何指导。

最佳答案

CommitAdvertisement 将返回 false,如果已经有具有相同 ID 的广告,则添加失败。

这就是您正在做的。第一个和第二个广告的 ID 均为 12345656。因此它按照您的要求进行操作。

增加 the_second_ad 的 ID。

关于c++ - 在 C++ 中将类实例添加到对象层次结构的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58837558/

相关文章:

c++ - OpenSSL SSL_shutdown 收到信号 SIGPIPE,Broken pipe

c++ - clgetPlatformIDs() 返回 -1001 即使 nvidia.icd 存在并包含 'libcuda.so'

java - 为什么有些库会定义自己的集合?

oop - 在尝试遵循 SOLID 原则时,我的类(class)设计有多精细?

java - 如何从我拥有的对象中获取变量?

ruby-on-rails - 维特斯 : Replace accepts_nested_attributes (one-to-many) with a form object

c++ - 我怎样才能让这个 += 运算符工作?

c++ - 如何从 C++ catch(...) block 中获取错误消息?

java - 将内部变量作为方法中的参数传递

Javascript 对象数据库