user2086751
user2086751

Reputation:

how to access elements of a derived class through a parent class in c++?

class game_list
{
    public:
        string name;
        float price;
        string platform;
        string console;
        string conditin;
        bool is_portable;

};

class catridgeClass:public game_list
{
    string N_bits;
    bool is_import;
};

game list is dynamically created and a pointer called mainPointer points to that dynamic object. But I am confused on how to access cartridgeClasss elements through the parent class game_list. I already tried mainPointer->N_bits. Seems like it doesnt work that way? Sorry I am just a noob at c++.

Upvotes: 1

Views: 142

Answers (4)

Ian Medeiros
Ian Medeiros

Reputation: 1776

To access catridgeClass attributes, you will need a catridgeClass object.

First, you need to correct your class to have public attributes.

class catridgeClass:public game_list
{
   public:
    string N_bits;
    bool is_import;
};

class anotherClass: public game_list
{
   public:
      string other_member;
};

Than you need to get a catridgeClass object from the caller function:

int main()
{
   std::vector<game_list *> games;

   games.push_back(new catridgeClass);
   games.push_back(new anotherClass);

   for(int i=0; i<games.size(); i++)
   {
      //You will need to get a catridgeClass object to access it's members.
      catridgeClass *catridge_ptr = dynamic_cast<catridgeClass *>(games[i]); 

      //Or to access anotherClass.
      anotherClass *another_ptr = dynamic_cast<anotherClass*>(games[i]);

      //If the conversion is not possible, dynamic_cast will return a null pointer
      if(catridgeClass != NULL)
        std::cout << catridge->N_bits << std::endln;
      if(anotherClass  != NULL)
        std::cout << anotherClass->other_member << std::endln;
   }

   //And of course, you need to avoid a memory leak
   for(int i=0; i<games.size(); i++)
      delete games[i]

   return 0;
}

Keep in mind that it's not a desired design. If you are trying to use polimorphism, is because all that classes share common behaviours. A better approach would be to use interfaces.

Upvotes: 2

Nathan Hiemenz
Nathan Hiemenz

Reputation: 61

A parent class has no information about its child class. The only way to do what you want is to either cast like Dory suggested -- which I usually frown upon -- or to create a virtual function in the parent class that the child class redefines.

Why do I frown upon the cast? Most of the time dynamically casting an object to get data from it represents poorly written code. I'm not sure what the rest of your code looks like, but my guess is that there's a better way to write this via shared functionality. For example, let's say what you want to use these items for is displaying string information. In such a case, the best way to write it would be using virtual functions:

class game_list
{
    public:
        string name;
        float price;
        string platform;
        string console;
        string conditin;
        bool is_portable;
        public virtual void PrintInfo() 
        { 
            cout << "name: " << name << ", price: " << price; //etc
        } 

};

class catridgeClass:public game_list
{
    string N_bits;
    bool is_import;
    public virtual void PrintInfo()
    {
        game_list::PrintInfo();
        cout << ", bits: " << bits << ", is import: " << is_import;
    }
};

Now calling mainPointer->PrintInfo() will print the correct information regardless of its underlying type. Much nicer, and it's the "Object-Oriented Way."

Upvotes: 1

Dory Zidon
Dory Zidon

Reputation: 10719

try to use dynamic_cast

catridgeClassPtr = dynamic_cast<catridgeClass*>(GameListPtr);

catridgeClassPtr->N_bits;

full info on dynamic_cast: http://www.cplusplus.com/doc/tutorial/typecasting/

Upvotes: 0

yoones
yoones

Reputation: 2474

Try a virtual method getNBits() = 0; in parent class, and define it in child class.

See: C++ Virtual/Pure Virtual Explained

Upvotes: 1

Related Questions