0

I am having issues with polymorphism I have set these basic classes up methods will be added later but I want the different data members to be accessible from these class.

class square
{
public:
    bool canBeBought;
    string name;
};

class property : public square
{ 
public:
    int rent;
    int colour;
    int cost;
    bool bought;
};

class specialSquare : public square
{
private:

public:
};

Here is the code that I am calling

square* properties[23];
for(int i = 0; i < 23; i++)    
{
    if(propertyStrings.at(i).substr(0,8) == "Property")
    {
        istringstream ss(propertyStrings.at(i).substr(11,21));
        string temp;
        properties[i] = new property;
        while(!ss.eof())
        {
            properties[i]->bought = false;
            properties[i]->name = propertyStrings.at(i).substr(0,11);
            cout << "Name: " << properties[i]->name << endl;
            ss >> temp;
            properties[i]->cost = atoi(temp.c_str());
            cout << "Cost: "<< properties[i]->cost << endl;
            ss >> temp;
            properties[i]->rent = atoi(temp.c_str());
            cout << "Rent: "<< properties[i]->rent << endl;
            ss >> temp;
            properties[i]->colour = atoi(temp.c_str());
            cout << "Colour: "<< properties[i]->colour << endl << endl;

            break;
        }
    }
}

my issue is that because the name variable is in the square class it works fine but the data members for the property class don't get recognised. My aim was to try and get all square data stored in one array thats the property class and the specialSquare class as this would make things easier later on in my program.

bobthemac
  • 1,172
  • 6
  • 26
  • 59

3 Answers3

1

I guess your problem is that the compiler doesn't recognize your "properties" pointers as pointing to instances of class Property, because you saved them as pointers to Square. Casting properties[i] to a pointer to class Property (((property*)properties[i])->colour) should do the trick (you're explicitly telling the compiler that this is, in fact, that class, and you have made sure that it cannot be another class).

If Property and Special_Square are not, in fact, Squares, polymorphism may be the wrong choice here, though. There are other ways to accomplish the task, e.g. by an array of variants.

SvenS
  • 795
  • 7
  • 15
  • Using a cast is neither safe nor performant. The best solution is to use a temporary property* pointer. – skurton Jan 08 '13 at 14:40
  • Yes, to reduce redundant code one should save the casted pointer in an auxiliary variable. But as that's common (programmer's) sense, and the cast itself (be it explicit or implicit) is the solution to the problem at hand, I didn't think it needed mentioning ;) – SvenS Jan 08 '13 at 15:21
0

Why don't you just have an array of the derived type? Does the array need to contain multiple types of Square? If it does, them you can use dynamic_cast<> to change the type of the pointer.

I would however not use an array of pointers. If you use one of the std library collections, you will save yourself lots of headaches with memory management...

    void DoSomething(std::vector<property>& properties)
    {
        // do something with...
    }

    int _tmain(int argc, _TCHAR* argv[])
    {
        std::vector<property> properties;

        property x;
        x.cost = 1234;

        properties.push_back(x);

        DoSomething(properties);

        return 0;
    }
Jon Rea
  • 9,337
  • 4
  • 32
  • 35
0

It's normal because properties[i] is a square pointer, look at your declaration:

square* properties[23];

This means you can only acces to square class members, which are canBeBought and name.

The solution is to replace:

properties[i] = new property;

by:

property * prop = new property;
properties[i] = prop;

Then, in your while loop, just use prop-> instead of properties[i]->.

skurton
  • 337
  • 2
  • 9