Hi,
Hoping someone can tel me where I'm going wrong, or what I should do.
I'm trying to use some pure virtual functions on some polymorphic classess but am unsure how to do it correctly.
Hiopefully this code will explain it a bit better:
vehicle.h :
using namespace std;
class Vehicle
{
public:
Vehicle(int publicmpg = 0);
protected:
int protectedmpg;
};
Vehicle::Vehicle(int publicmpg)
{
protectedmpg = publicmpg;
}
/////////////////////////////////
class VectorClass
{
public:
//Declaring pure virtual function here causing Vehicle to become the ABC
virtual void AddACar() = 0;
protected:
};
/////////////////////////////////
class SportsCar : public Vehicle
{
public:
SportsCar(int publicsixspeed = 0, int publicmpg = 0);
//pure virtual member function for SportsCar
void AddACar()
{
cout << "Inside myfunc() for SportsCar objects" << endl;
}
protected:
int protectedsixspeed;
};
SportsCar::SportsCar(int publicsixspeed, int publicmpg) : Vehicle(publicmpg)
{
protectedsixspeed = publicsixspeed;
}
main.cpp :
#include <iostream>
#include <vector>
#include "Vehicle.h"
using namespace std;
int main()
{
vector<Vehicle*> vectorname;
int publicmpg = 11;
int publicsixspeed = 22;
SportsCar node(publicmpg, publicsixspeed);
SportsCar *ptrToSportsCar;
ptrToSportsCar = &node;
vectorname.push_back( ptrToSportsCar );
cout << "past push" << endl;
vectorname[0]->AddACar();
cout << "Past myfunc() call " << endl;
system ("PAUSE");
return 0;
}
Visual Studio reports:
'DeleteACar' : is not a member of 'Vehicle'
I'm going to use DeleteACar() to delete nodes from the Vector by passing which node as an argument, but before I attempt to write the function, I'm stuck as how to correctly configure a pure virtual function.
Any advice or advice is most appreciated.
Thanks!
Carrots.