Derived clases and polymorphism

59 views Asked by At

The given code for my program is:

class Hive
{
    private:
        Bee* queen;
        Bee* worker;
        Bee* drone;

    public:
        Hive();
        void activity();
        ~Hive();
};

Coding instructions have been provided and I must add the code. My constructor as coded, has the following error:"Cannot allocate an object of abstract type"

Hive::Hive() //This is where my error is
{
    //Create an instance of each derived class
    queen = new Queen(1,1) ;
    worker = new Worker(1,1) ;
    drone = new Drone(1,1) ;
}

void Hive::activity()
{
    //Polymorphically call each classes work function
    queen->Work() ;
    worker->Work() ;
    drone->Work() ;
}

Hive::~Hive()
{
    //Deal with dynamic memory
    delete queen ;
    delete worker ;
    delete drone ;
}

My derived class definitions are:

class Drone : Bee /*Indicate that a Drone IS A Bee*/
{
    private:
        char fertile;

    public:
        Drone(int lifespan, char fertile);
        virtual void work();
};

class Queen : Bee /*indicate that a Queen IS A Bee*/
{
    private:
        int numBabies;

    public:
        Queen(int lifespan, int numBabies);
        virtual void work();
};

class Worker : Bee /*Indicate that a Worker IS A Bee*/
{
    private:
        int honeyMade;

    public:
        Worker(int lifespan, int honeyMade);
        virtual void work();
};

Hopefully I didn't make additional mistakes.

1

There are 1 answers

1
Morris On

The main:

include <iostream>
include "Hive.h"

using namespace std;

using namespace HiveSpace;

int main()
{
Hive h;
h.activity();
return 0;
}

Classes

#ifndef HIVE_H
#define HIVE_H

#include "Bee.h"
#include "Queen.h"
#include "Drone.h"
#include "Worker.h"

namespace HiveSpace
{
class Hive
{
    private:
        Bee* queen;
        Bee* worker;
        Bee* drone;

    public:
        Hive();
        void activity();
        ~Hive();
};
}

#endif

///

#include "Hive.h"
#include <iostream>

using namespace std;

namespace HiveSpace
{

Hive::Hive()
{

    //Create an instance of each derived class
    queen = new Queen(1,1) ;
    worker = new Worker(1,1) ;
    drone = new Drone(1,1) ;
}

void Hive::activity()
{
    //Polymorphically call each classes work function
    queen->Work() ;
    worker->Work() ;
    drone->Work() ;
}

Hive::~Hive()
{
    //Deal with dynamic memory
    delete queen ;
    delete worker ;
    delete drone ;
}
}

///

#ifndef BEE_H
#define BEE_H

namespace HiveSpace
{

class Bee
{
    private:
        int lifespan;

    public:
        Bee(int lifespan);
        //Add pure virtual function for work
        virtual void Work() = 0 ;
        //Create polymorphic destructor
        virtual ~Bee() ; // ADDED
};
}

#endif

/////

#include "Bee.h"
#include <iostream>

using namespace std;

namespace HiveSpace
{

void Bee::Work()
{
    //Nothing to do here
}

Bee::Bee(int lifespan)
{
    cout << "Constructor for Bee base class " << endl;
    this->lifespan = lifespan;
}

Bee::~Bee()
{
    //Nothing special to do here
    cout << "Destructor for Bee base class << endl;
}
}

///

#ifndef DRONE_H
#define DRONE_H

#include "Bee.h"

namespace HiveSpace
{

class Drone : Bee /*Indicate that a Drone IS A Bee*/
{
    private:
        char fertile;

    public:
        Drone(int lifespan, char fertile);
        virtual void work();
};
}

#endif

///

#include "Drone.h"
#include <iostream>

using namespace std;

namespace HiveSpace
{

//Provide a constructor which also invokes the base class constructor
Drone::Drone(int lifespan, char fertile) : Bee(lifespan)
{
 //   this->fertile = fertile ;
}

void Drone::work()
{
    cout << "Drone derived class working " << endl;
}
}

#ifndef QUEEN_H
#define QUEEN_H

#include "Bee.h"

namespace HiveSpace
{

class Queen : Bee /*indicate that a Queen IS A Bee*/
{
    private:
        int numBabies;

    public:
        Queen(int lifespan, int numBabies);
        virtual void work();
};
}

#endif

///

#include "Queen.h"
#include <iostream>

using namespace std;

namespace HiveSpace
{
//Provide a constructor which also invokes the base class constructor

Queen::Queen(int lifespan, int numBabies) : Bee(lifespan)
{
    this->numBabies = numBabies ;
}

void Queen::work()
{
    cout << "Queen derived class working"<< endl;
    numBabies++;
}
}

////

#ifndef WORKER_H
#define WORKER_H

#include "Bee.h"

namespace HiveSpace
{

class Worker : Bee /*Indicate that a Worker IS A Bee*/
{
    private:
        int honeyMade;

    public:
        Worker(int lifespan, int honeyMade);
        virtual void work();
};
}

#endif

///

#include "Worker.h"
#include <iostream>

using namespace std;

namespace HiveSpace
{
//Provide a constructor which also invokes the base class constructor
Worker::Worker(int lifespan, int honeyMade) : Bee(lifespan)
{
    this->honeyMade = honeyMade ;
}

void Worker::work()
{
    cout << "Worker derived class working" << endl;
    honeyMade++;
}
}