user2094257
user2094257

Reputation: 1715

Abstract base class member variables

I have an abstract base class "Company" and 2 subclasses "notForProfit" and "forProfit" that derive from "Company". Both sub classes have member variables "name" and "dateFormed". Is it possible to define those 2 member variables in "Company" and have the 2 subclasses inherit "Name" and "DateFormed" to use in their constructors?

Upvotes: 3

Views: 14486

Answers (2)

dtech
dtech

Reputation: 49289

A non-profit company is a company, a for-profit company is a company, but a company is not a name, and it is not a date of formation, instead a company has a name and date of formation.

"Is a" relation is inheritance. "Has a" relation is aggregation.

If both companies share the same members, then those should be in the base class, because code reuse is one of the main ideas of inheritance (aside from polymorphism)

class Company {
protected:
    Company(QString n, QDate df) : name(n), dateFormed(df) { } // protected constructor, can't create a Company instance
    QString name; // protected data members, cannot be accidentally changed
    QDate dateFormed;
public:
    virtual ~Company() {} // you always need a virtual destructor for polymorphism
    virtual void info() = 0; // abstract method, must be implemented in order to instantiate derived classes    
};

class NonProfit : public Company {
public:
    NonProfit(QString n, QDate df) : Company(n, df) { }
    void info() { qDebug() << "Non-profit company" << name << "formed on" << dateFormed; }
};

class ForProfit : public Company {
public:
    ForProfit(QString n, QDate df) : Company(n, df) { }
    void info() { qDebug() << "For-profit company" << name << "formed on" << dateFormed; }
};

And usage:

Company cp("some company", QDateTime::currentDateTime().date()); // won't work
NonProfit np("some company", QDateTime::currentDateTime().date());
np.info(); // Non-profit company "some company" formed on 2015-08-15

Polymorphism in action:

Company * c1 = new NonProfit("some company", QDateTime::currentDateTime().date());
Company * c2 = new ForProfit("some company", QDateTime::currentDateTime().date());
c1->info(); // Non-profit company "some company" formed on 2015-08-15
c2->info(); // For-profit company "some company" formed on 2015-08-15

Also note that even though the derive classes don't have declared virtual destructors, and info is not explicitly virtual, C++ will automatically generate virtual destructors as long as the base class has a declared virtual destructor, and the info will also be implicitly virtual, some people prefer to write methods as virtual in derived classes for clarity, but once virtual in the base class, a method will be implicitly virtual in the derived classes as well.

Upvotes: 11

Yael
Yael

Reputation: 1600

Yes, the relationship between base and derived is "is a", everything the base class have, it derived classes have.

code snippet:

// derived classes
#include <iostream>
using namespace std;

class Polygon 
{
protected:
int width, height;

public:
    void set_values (int a, int b) 
    { 
       width=a; 
       height=b;
    }
};

class Rectangle: public Polygon 
{
public:
    int area ()
    { 
         return width * height; 
    }
};

class Triangle: public Polygon 
{
public:
   int area ()
   {  
      return width * height / 2; 
   }
};

Upvotes: 1

Related Questions