Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to best use an abstract base class as an interface without duplicating sibling classes function overrides
    primarykey
    data
    text
    <p>I have an abstract interface <code>Person</code> which is inherited by <code>Customer</code> and <code>SalesPerson</code>. <code>Person</code> contains pure virtual functions for every member function of the two derived classes that need to be called by each other through references to the base(polymorphism). This allows me to decouple my types right?</p> <p>How do I stop my derived classes inheriting the pure virtual functions of the other derived classes and becoming abstract without mirroring the sibling classes overridden functions with dummy non-pure virtual functions throughout my derived classes?</p> <pre><code>class Person { public: virtual int const GetNumberOfPurchases() const = 0; virtual long const GetId() const = 0; virtual void AddPurchase() = 0; virtual void DisplayCustomerDetails() const = 0; virtual void DisplaySalesPersonStats() = 0; virtual void SetContact(Person * SalesP) = 0; }; class SalesPerson: public Person { private: long const id; // Assumption: Sales people never change their ID Person *bestCustomer; Person *worstCustomer; vector&lt;Person *&gt; v_Client; virtual int const GetNumberOfPurchases() const { return 0; }; // dummy to avoid inheriting pure virtual function virtual void AddPurchase() {} virtual void DisplayCustomerDetails() const {} virtual void SetContact(Person * SalesP) {} public: SalesPerson(); virtual ~SalesPerson(){}; Person const *GetBestCustomer() const; Person const *GetWorstCustomer() const; virtual long const GetId() const { return id; } void DisplaySalesPersonStats(); float const CalculateMeanAverageSales(); void SignUpCustomer(Person * aCustomer); void RegisterSale(long customerId); void CalculateBestAndWorstCustomers(); void DisplayClientList(); long GenerateSalesPersonKey(); }; class Customer: public Person{ private: long ID; int birthYear; bool isCurrentMember; unsigned numberOfPurchases; const Person *contact; // Assumption: Each Customer has a single assigned contact SalesPerson virtual void DisplaySalesPersonStats() {} // Dummy to avoid inheriting pure virtual from Person public: Customer(const int aBirthYear); virtual ~Customer() {} virtual long const GetId() const; int const GetBirthYear() const; void SetBirthYear(int aBirthYear); bool const GetIsCurrentMember() const; void ToggleIsCurrentMember(); virtual int const GetNumberOfPurchases() const; virtual void AddPurchase(); virtual void DisplayCustomerDetails() const; virtual void SetContact(Person * SalesP); long GenerateCustomerKey(); }; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload