I've got 3 classes 2 subclasses that link to a main class. (electric tools, fuel tools connect to Core tools). What i've done is made a handler class(Hardware) from these classes which has a void display in it.

vector<Hardware> hardware_stock;
    Hardware record;
    record.read(cin);
    hardware_stock.push_back(record);
    for(vector<Hardware>::size_type i = 0; i!= hardware_stock.size(); ++i)
    {
        hardware_stock[i].display();
    }

What i would like to know is how do I display just the entries for a specific class. Like i just want to display the electric tools class..(sorry if my question was written badly :x)

What you want is an identifying downcast, where all you're doing is verifying that the dynamic type of the object matches the expected type for the operation. This can be done with C++'s dynamic_cast:

vector<Hardware*> hardware_stock;

...

for(vector<Hardware*>::size_type i = 0; i != hardware_stock.size(); ++i)
{
    Electric *p = dynamic_cast<Electric*>(hardware_stock[i]);

    if (p)
        p->display();
}

One important thing about this code is that Edward changed the object types to pointers because runtime polymorphism doesn't work unless the static type is a pointer or a reference. If you don't have a pointer or a reference you only have Hardware objects, and if your class hierarchy is well designed, that would cause the code to fail because the Hardware class should be abstract.

In other words, this will do what you want and follows good object oriented design practices:

#include <iostream>
#include <string>
#include <vector>

namespace EdRules {
  using namespace std;

  class Hardware {
  public:
    // Base classes are always abstract
    virtual istream& read(istream& is) = 0;
    virtual void display() const = 0;
    virtual ~Hardware() = 0 {}
  };

  class CoreTools {
  public:
    // Base classes are always abstract
    virtual ~CoreTools() = 0 {}
  };

  class Electric: public CoreTools, public Hardware {
    double _value;
  public:
    explicit Electric(double value = 0): _value(value) {}

    virtual void display() const
    {
      cout << "Value: " << _value << '\n';
    }

    virtual istream& read(istream& is)
    {
      if (is == cin)
        cout << "Enter a double: ";

      is >> _value;

      if (is == cin)
        cin.ignore(1024, '\n');

      return is;
    }
  };

  class Fuel: public CoreTools, public Hardware {
    string _value;
  public:
    explicit Fuel(const string& value = ""): _value(value) {}

    virtual void display() const
    {
      cout << "Value: " << _value << '\n';
    }

    virtual istream& read(istream& is)
    {
      if (is == cin)
        cout << "Enter a string: ";

      return getline(is, _value);
    }
  };
}

int main()
{
  using namespace std;
  using namespace EdRules;

  vector<Hardware*> stock;

  stock.push_back(new Electric());
  stock.push_back(new Fuel());
  stock.push_back(new Fuel());
  stock.push_back(new Electric());

  for (vector<Hardware*>::size_type i = 0; i < stock.size(); ++i)
    stock[i]->read(std::cin);

  for (vector<Hardware*>::size_type i = 0; i != stock.size(); ++i) {
    Electric *p = dynamic_cast<Electric*>(stock[i]);

    if (p)
      p->display();
  }

  for (vector<Hardware*>::size_type i = 0; i < stock.size(); ++i)
    delete stock[i];
}

ty very much it works:D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.