Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9119837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:29:27+00:00 2026-06-17T05:29:27+00:00

I am having issues with polymorphism I have set these basic classes up methods

  • 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T05:29:28+00:00Added an answer on June 17, 2026 at 5:29 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Am having issues with printing different sized PDF's using GhostScript (V9.05). The PDF in
Im having issues getting this to work, maybe its not even possible? I have
I'm having issues with an input validation code I have. I enter in my
I am having issues understanding how std::set (or std::map etc) identify unique keys. The
I am having issues testing which icon my notifyIcon is using. I have a
Having issues with BootStrap.groovy loading development data. Prior it had always loaded data, but
I've been having issues with on/off removing event handlers that I don't want to,
Having issues with screen progress. I have a screen displaying information from a database.
Having issues with my apps on Google Play. I have a free app which
Having regex issues.. I have a series of directories that hold .htm templates... all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.