如何将子类的对象添加到基类的向量

时间:2018-12-01 12:42:36

标签: c++ pointers polymorphism

我有这样的类:Customer,Order,OrderByPhone和OrderByInternet。订单类是抽象的,具有诸如OrderByPhone和OrderByInternet的子类。我在Customer类中有一个Order对象的向量(当客户下订单时(byPhone或byInternet)。我要实现的是,客户可以将OrderByPhone对象或OrderByInternet对象添加到Order对象的向量中。

下面是一些代码:

vector<Order*> listOfOrder;

void customer::addOrder(Order *orderObj)
{
    listOfOrder.push_back(orderObj);
} // these are inside Customer class

Controller类具有listCustomers和函数create Order,例如:

void createOrder(vector <customer> &listCustomer) {
    double tax;
    double total;
    int accntNumCheck;
    int orderTypeOption;
    string url;
    string clerkName;
    int phoneNum;
    //order orderObj; cannot create an object of order because its an abstract class but you can create a pointer of orders
    Order *orderObj;
    vector<Order *> listOfOrder;
    bool found = false;
    customer *foundCust; //it is a pointer coz we need to add the order into the original customer;

    cout << "Enter Customer Account No: " << flush;
    cin >> accntNumCheck;

    for(int unsigned i = 0; i < listCustomer.size(); i++){
        if(listCustomer[i].getAccntNum() == accntNumCheck){
            cout<< listCustomer[i].getAccntName();
            found = true;
            foundCust = &listCustomer[i];
        }
    }
    if (found) {
        cout << "1. Order By Phone " << endl;
        cout << "2. Order By Internet" << endl;
        cout << "Your Option >>> " << flush;
        cin >> orderTypeOption;

        switch (orderTypeOption) {
        case 1:
            cout << "Enter clerk name: " << flush;
            cin >> clerkName;
            cout << "Enter Phone Number: " << flush;
            cin >> phoneNum;
            cout << "Enter Tax: RM" << flush;
            cin >> tax;
            cout << "Enter Total: RM" << flush;
            cin >> total;
            orderObj = new orderByPhone(tax, total, clerkName, phoneNum);
            //(iterCust)->addOrder(orderObj); this is incorrect but idk why
            foundCust->addOrder(orderObj);
            break;
        case 2:
            cout << "Enter URL: " << flush;
            cin >> url;
            cout << "Enter Tax: RM" << flush;
            cin >> tax;
            cout << "Enter Total: RM" << flush;
            cin >> total;
            orderObj = new orderByInternet(tax, total, url);
            //(iterCust)->addOrder(orderObj); this is incorrect but idk why
            foundCust->addOrder(orderObj);
            break;
        }
        delete foundCust; //MUST DELETE THE POINTER AFTER WE USED IT
    }
    else{
        cout << "Customer not found!";
    }
}

这是Controller中的主要功能

int main()
{
    vector <customer> listCustomer;
    int option;
    do
    {
        option=menu();
        switch (option)
        {
        case 1: addCustomer(listCustomer);
                break;
        case 2: createOrder(listCustomer);
                break;
        case 3: print(listCustomer);
                break;
        default: cout<<"Bye"<<endl;
        }
    }while (option <=3);

    listCustomer.clear();
    return 0;
}

我在打印时遇到问题,并且我确定如何将子对象添加到基类的向量中的方式存在问题。很抱歉没有提供所有代码,因为我认为这太多了,因此我仅上传了必要的代码。附言->我可以设法添加客户并打印(用于检查)。基本上,我需要在Customer类中添加Order(第一个代码)的帮助。感谢您抽出宝贵的时间阅读所有这些内容。

0 个答案:

没有答案