-2

So I have a problem with an abstract class!

This is the Product.h

#ifndef PRODUCT_H
#define PRODUCT_H
#include <string>

using namespace std;

class Product
{
    private:
        string description;
        int d,m,y;
        float weight;

    public:
        Product();
        Product(string description,int d,int m,int y, float weight);
        void print();
        virtual bool isExpiring()=0;
};

#endif

This is Product.cpp

#include "Product.h"
#include <string>
#include <iostream>
using namespace std;

Product::Product()
{
    description="";
    d=0;
    m=0;
    y=0;
    weight=0.0;
}

Product::Product(string description,int d,int m,int y, float weight)
{
    this->description=description;
    this->d=d;
    this->m=m;
    this->y=y;
    this->weight=weight;
}

void Product::print()
{
    cout << "Description: " << description<<endl;
    cout << "Expiration date: " << d <<"/" << m << "/" << y << endl;
    cout << "Weight: " << weight << endl;
}

This is ShortTermProduct.h

#ifndef SHORTTERMPRODUCT_H
#define SHORTTERMPRODUCT_H
#include "Product.h"

class ShortTermProduct : public Product
{
    public:
        ShortTermProduct();
        void Print();
        bool isExpiring();
};

#endif

This is LongTermProduct.h

#ifndef LONGTERMPRODUCT_H
#define LONGTERMPRODUCT_H
#include "ShortTermProduct.h"
#include <string>

using namespace std;

class LongTermProduct 
{
    private:
        int lifetime;
        string open, open_date;

    public:
        LongTermProduct(int lifetime,string open,string open_date);
        void print();
        bool isExpiring();
};

#endif

And finally this is Fridge.h

#ifndef FRIDGE_H
#define FRIDGE_H
#include "LongTermProduct.h"
class Fridge : public LongTermProduct
{
    private:
        Product products[10];

    public:
        fridge();
        void addShortTermProduct (ShortTermProduct p);
        void addLongTermProduct (LongTermProduct p);
        void printFridgeProducts();
        bool isExpiring();
};

#endif

The errors I am getting are these:

  • [Error] invalid abstract type 'Product' for 'products'
  • [Error] cannot declare field 'Fridge::products' to be of abstract type 'Product'

Any help would be grate !Thanks in advance !

TonyK
  • 1
  • 1
  • 3
  • Possible duplicate of [Why can't we create an instance of an abstract class?](https://stackoverflow.com/questions/5131567/why-cant-we-create-an-instance-of-an-abstract-class) – Kinan Al Sarmini May 28 '17 at 00:34
  • Which part of "cannot declare field 'Fridge::products' to be of abstract type 'Product'" you did not understand? `Product` is an abstract class, and you are attempting to declare a class member that's an array of abstract classes. This is not valid C++. The End. – Sam Varshavchik May 28 '17 at 00:40
  • Also note that it is generally wise to declare abstract base class's destructor `virtual`, too. – bipll May 28 '17 at 01:46

2 Answers2

1

The error occurred because the Product class has a pure virtual method, therefore you should use pointer to declare it. if you want to use c++ fixed array size, I suggest you to look at std::array here

std::array<Product*, 50> products;
Erman
  • 1,543
  • 17
  • 26
-2

virtual bool isExpiring()=0; is a pure virtual function (abstract), therefore Product is an abstract class.

Product products[10]; 

What you are doing here is trying to create 10 objects of an abstract type Product. But you cannot create an object from an abstract class. What you want are Product pointers which can point to pointers of derived classes:

Product** products = new Product*[10];
Shiro
  • 2,610
  • 2
  • 20
  • 36