简体   繁体   English

C ++派生类

[英]C++ derived classes

Having some trouble figuring out how to go about an assignment I have. 在弄清楚如何进行我的作业时遇到一些麻烦。 The assignment is dealing with package objects We were told to have a base class that called CPackages , and two classes derived from it called COverNightPackage and CTwoDayPackage . 该分配正在处理包对象。我们被告知有一个名为CPackages的基类,以及一个CPackages派生的两个类COverNightPackageCTwoDayPackage That was all fine, I could do that. 很好,我可以做到。

The problem I am having is that the over night package MAY or MAY NOT have a courier. 我遇到的问题是通宵包裹可能没有快递。 If a courier is required, details such as company name, address etc. needs to be recorded. 如果需要快递,则需要记录详细信息,例如公司名称,地址等。 If a package needs a courier, when show details of all packages is called it will have to say if one is needed for that package. 如果包裹需要快递,则在调用显示所有包裹的详细信息时,将不得不说该包裹是否需要一个。 There will also be another menu option to display all the details of the couriers used. 也将有另一个菜单选项显示所使用快递的所有详细信息。

What is the best way to add couriers? 添加信使的最佳方法是什么? Make a courier class? 进行快递课吗? I've been stuck on this for a couple days now, haven't done anything like it before. 我已经坚持了几天,以前没有做过类似的事情。

Yes you do need a courier class. 是的,您确实需要快递课程。 Now to tell your class if courier is required or not you have two ways. 现在告诉您的班级是否需要快递,您有两种方法。 You can create a constructor in your class which takes Courier class object as null.Some thing like 您可以在类中创建一个构造函数,该构造函数将Courier类对象作为null。

#include <iostream>
using namespace std;

class {
    public:
    Courier();
};
Courier::Courier(){

};

class COverNightPackage {
    public:
    COverNightPackage(Courier* obj);
};

COverNightPackage::COverNightPackage(Courier* obj){
        if(obj == NULL){
            cout<<"Courier is null"<<"\n";
        }else{
            cout<<"Courier is not null"<"\n"; \\ Store package info here
        }

};
int main() {
    Courier* o2 = new Courier();
    COverNightPackage* o1 = new COverNightPackage(NULL);
    COverNightPackage* o3 = new COverNightPackage(o2);
    return 0;
}

Now you can store its value as per your convenience. 现在,您可以根据需要存储其价值。 While showing the value you can check if courier class is null or not. 在显示值时,您可以检查courier class是否为null。

Second way could be to keep a Courier class object inside your class and provide a setter to help the user to set value at runtime. 第二种方法是将Courier类对象保留在您的类中,并提供设置器以帮助用户在运行时设置值。 Hope it will help you :-) 希望它能对您有所帮助:-)

Indeed, you will need a Courier class, and you should store a pointer to this Courier class within your COverNightPackage class. 确实,您将需要一个Courier类,并且应该在COverNightPackage类中存储一个指向该Courier类的指针。 You just want to be sure that this pointer is set to NULL when there is no Courier . 您只需要确保没有Courier时将此指针设置为NULL To do this, you should create two constructors for your COverNightPackage . 为此,您应该为COverNightPackage创建两个构造COverNightPackage Use initializers for each constructor to be sure that the pointer to the Courier class is either initialized to NULL or pointing to a Courier class. 对每个构造函数使用初始化器,以确保将指向Courier类的指针初始化为NULL或指向Courier类。

class Courier;

class COverNightPackage: public CPackages {
public:
  COverNightPackage()
   : m_courier(NULL)
  { }

  COverNightPackage(Courier* courier)
   : m_courier(courier)
  { }

private:
  Courier* m_courier;
}

Alternatively, you can accomplish this using a single constructor with a default argument of NULL 另外,您可以使用一个默认值为NULL构造函数来完成此操作

class Courier;

class COverNightPackage: public CPackages {
public:
  COverNightPackage(Courier* courier = NULL)
   : m_courier(courier) { }

private:
  Courier* m_courier;
}

If the constructor is called with no argument, the Courier pointer will be initialized to NULL 如果没有参数调用构造函数,则Courier指针将初始化为NULL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM