Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Objects in Array loops, need help

Objects in Array loops, need help

Scheduled Pinned Locked Moved C / C++ / MFC
oopquestiondata-structureshelp
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sivyo
    wrote on last edited by
    #1

    Alright, this is the thing... I have to create an Array for a number of objects in a setting with polymorphism. Each object in the array needs to do the same thing... so I figure I can just create a loop for each item in an array. Now, the method is called the same thing because of Polymorphism/inheritance Although the method is placed somewhere else for each object... so I have 10 classes. the Base class is called: Animals From Animals come: Marine, Reptiles, walkers From there we have 6 different types of animals deriving from those.

    #include "Gazelles.h"
    #include "Goldfish.h"
    #include "Sharks.h"
    #include "Snakes.h"

    #include <iostream>

    using namespace std;

    int main(int argc,char* argv[])
    {

    Animals *ourAnimals[6]; //the array

    Animals *goldfish = new Goldfish();
    Animals *crocodile = new Crocodiles();
    Animals *elephant = new Elephants();
    Animals *gazelles = new Gazelles();
    Animals *shark = new Sharks();
    Animals *snakes = new Snakes();

    ourAnimals[0]=goldfish;
    ourAnimals[1]=crocodile;
    ourAnimals[2]=elephant;
    ourAnimals[3]=gazelles;
    ourAnimals[4]=shark;
    ourAnimals[5]=snakes;

    for(i=0;i<6;i++)
    {
    ourAnimals[i] // ->to method I want it to go converse();
    }

    return 0;
    }

    each animal has converse, I am a bit of a noob with this obviously, so how do I get each object in the array to do what I want it to do?

    D N C CPalliniC 4 Replies Last reply
    0
    • S Sivyo

      Alright, this is the thing... I have to create an Array for a number of objects in a setting with polymorphism. Each object in the array needs to do the same thing... so I figure I can just create a loop for each item in an array. Now, the method is called the same thing because of Polymorphism/inheritance Although the method is placed somewhere else for each object... so I have 10 classes. the Base class is called: Animals From Animals come: Marine, Reptiles, walkers From there we have 6 different types of animals deriving from those.

      #include "Gazelles.h"
      #include "Goldfish.h"
      #include "Sharks.h"
      #include "Snakes.h"

      #include <iostream>

      using namespace std;

      int main(int argc,char* argv[])
      {

      Animals *ourAnimals[6]; //the array

      Animals *goldfish = new Goldfish();
      Animals *crocodile = new Crocodiles();
      Animals *elephant = new Elephants();
      Animals *gazelles = new Gazelles();
      Animals *shark = new Sharks();
      Animals *snakes = new Snakes();

      ourAnimals[0]=goldfish;
      ourAnimals[1]=crocodile;
      ourAnimals[2]=elephant;
      ourAnimals[3]=gazelles;
      ourAnimals[4]=shark;
      ourAnimals[5]=snakes;

      for(i=0;i<6;i++)
      {
      ourAnimals[i] // ->to method I want it to go converse();
      }

      return 0;
      }

      each animal has converse, I am a bit of a noob with this obviously, so how do I get each object in the array to do what I want it to do?

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      Sivyo wrote:

      how do I get each object in the array to do what I want it to do?

      How about something like:

      Animal *ourAnimals[3];

      ourAnimals[0] = new Goldfish();
      ourAnimals[1] = new Crocodile();
      ourAnimals[2] = new Elephant();

      for (int i = 0; i < 3; i++)
      ourAnimals[i]->converse(); // Animal::converse() is virtual

      "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      S 1 Reply Last reply
      0
      • S Sivyo

        Alright, this is the thing... I have to create an Array for a number of objects in a setting with polymorphism. Each object in the array needs to do the same thing... so I figure I can just create a loop for each item in an array. Now, the method is called the same thing because of Polymorphism/inheritance Although the method is placed somewhere else for each object... so I have 10 classes. the Base class is called: Animals From Animals come: Marine, Reptiles, walkers From there we have 6 different types of animals deriving from those.

        #include "Gazelles.h"
        #include "Goldfish.h"
        #include "Sharks.h"
        #include "Snakes.h"

        #include <iostream>

        using namespace std;

        int main(int argc,char* argv[])
        {

        Animals *ourAnimals[6]; //the array

        Animals *goldfish = new Goldfish();
        Animals *crocodile = new Crocodiles();
        Animals *elephant = new Elephants();
        Animals *gazelles = new Gazelles();
        Animals *shark = new Sharks();
        Animals *snakes = new Snakes();

        ourAnimals[0]=goldfish;
        ourAnimals[1]=crocodile;
        ourAnimals[2]=elephant;
        ourAnimals[3]=gazelles;
        ourAnimals[4]=shark;
        ourAnimals[5]=snakes;

        for(i=0;i<6;i++)
        {
        ourAnimals[i] // ->to method I want it to go converse();
        }

        return 0;
        }

        each animal has converse, I am a bit of a noob with this obviously, so how do I get each object in the array to do what I want it to do?

        N Offline
        N Offline
        Nuri Ismail
        wrote on last edited by
        #3

        You should read about Virtual Functions & Polymorphism[^]. :) The converse() method must be virtual in your base class and you need appropriate implementation for this method in the derived classes. :)

        Nuri Ismail

        1 Reply Last reply
        0
        • S Sivyo

          Alright, this is the thing... I have to create an Array for a number of objects in a setting with polymorphism. Each object in the array needs to do the same thing... so I figure I can just create a loop for each item in an array. Now, the method is called the same thing because of Polymorphism/inheritance Although the method is placed somewhere else for each object... so I have 10 classes. the Base class is called: Animals From Animals come: Marine, Reptiles, walkers From there we have 6 different types of animals deriving from those.

          #include "Gazelles.h"
          #include "Goldfish.h"
          #include "Sharks.h"
          #include "Snakes.h"

          #include <iostream>

          using namespace std;

          int main(int argc,char* argv[])
          {

          Animals *ourAnimals[6]; //the array

          Animals *goldfish = new Goldfish();
          Animals *crocodile = new Crocodiles();
          Animals *elephant = new Elephants();
          Animals *gazelles = new Gazelles();
          Animals *shark = new Sharks();
          Animals *snakes = new Snakes();

          ourAnimals[0]=goldfish;
          ourAnimals[1]=crocodile;
          ourAnimals[2]=elephant;
          ourAnimals[3]=gazelles;
          ourAnimals[4]=shark;
          ourAnimals[5]=snakes;

          for(i=0;i<6;i++)
          {
          ourAnimals[i] // ->to method I want it to go converse();
          }

          return 0;
          }

          each animal has converse, I am a bit of a noob with this obviously, so how do I get each object in the array to do what I want it to do?

          C Offline
          C Offline
          Cedric Moonen
          wrote on last edited by
          #4

          Not sure I really understood your question: you want all specific Animal do something specific in the converse method ? If that is the case, that is really simple: simply declare the function in the base class (Animal) as a virtual function and override it for each specific animal. Each specific animal can then do something specific.

          Cédric Moonen Software developer
          Charting control [v2.0] OpenGL game tutorial in C++

          1 Reply Last reply
          0
          • D David Crow

            Sivyo wrote:

            how do I get each object in the array to do what I want it to do?

            How about something like:

            Animal *ourAnimals[3];

            ourAnimals[0] = new Goldfish();
            ourAnimals[1] = new Crocodile();
            ourAnimals[2] = new Elephant();

            for (int i = 0; i < 3; i++)
            ourAnimals[i]->converse(); // Animal::converse() is virtual

            "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

            S Offline
            S Offline
            Sivyo
            wrote on last edited by
            #5

            yea, I did that right after I posted this... I should of figured, the complier didn't reconize the method from ctrl space so I thought it didn't work... but it did... problem solved... Thanks for your ansswer, I am sure I will have more stupid questions soon

            1 Reply Last reply
            0
            • S Sivyo

              Alright, this is the thing... I have to create an Array for a number of objects in a setting with polymorphism. Each object in the array needs to do the same thing... so I figure I can just create a loop for each item in an array. Now, the method is called the same thing because of Polymorphism/inheritance Although the method is placed somewhere else for each object... so I have 10 classes. the Base class is called: Animals From Animals come: Marine, Reptiles, walkers From there we have 6 different types of animals deriving from those.

              #include "Gazelles.h"
              #include "Goldfish.h"
              #include "Sharks.h"
              #include "Snakes.h"

              #include <iostream>

              using namespace std;

              int main(int argc,char* argv[])
              {

              Animals *ourAnimals[6]; //the array

              Animals *goldfish = new Goldfish();
              Animals *crocodile = new Crocodiles();
              Animals *elephant = new Elephants();
              Animals *gazelles = new Gazelles();
              Animals *shark = new Sharks();
              Animals *snakes = new Snakes();

              ourAnimals[0]=goldfish;
              ourAnimals[1]=crocodile;
              ourAnimals[2]=elephant;
              ourAnimals[3]=gazelles;
              ourAnimals[4]=shark;
              ourAnimals[5]=snakes;

              for(i=0;i<6;i++)
              {
              ourAnimals[i] // ->to method I want it to go converse();
              }

              return 0;
              }

              each animal has converse, I am a bit of a noob with this obviously, so how do I get each object in the array to do what I want it to do?

              CPalliniC Offline
              CPalliniC Offline
              CPallini
              wrote on last edited by
              #6

              Sivyo wrote:

              I am a bit of a noob with this obviously, so how do I get each object in the array to do what I want it to do?

              Reading again the polymorphism chapter on your favourite C++ book may help, I guess. :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

              In testa che avete, signor di Ceprano?

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups