2015-08-19 79 views
2

我的程序給定的代碼是:衍生clases和多態性

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

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

編碼的指令的已經提供了,我必須添加代碼。作爲編碼我的構造,具有以下錯誤:「無法分配抽象類型的對象」

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 ; 
} 

我的派生類中的定義是:

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(); 
}; 

希望我沒有做額外的錯誤。

+0

提供一種可以直接測試代碼。目前你有一堆片段。 –

+0

這個錯誤也會告訴你,你做錯了什麼,指向你在這裏省略的類(Bee)有一個純虛函數的函數,因此需要在所有類中重新實現。 –

+0

我明白了。我會提供整個程序。謝謝你們 – Morris

回答

0

主要:

include <iostream> 
include "Hive.h" 

using namespace std; 

using namespace HiveSpace; 

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

#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++; 
} 
} 
+0

這應該工作。指示公衆在班級建設者指示蜜蜂didn,t也解決我的問題。 – Morris