Urgent....Array help....I need to know this to go on in the book!!!!!!

This is a discussion on Urgent....Array help....I need to know this to go on in the book!!!!!! within the C++ Programming forums, part of the General Programming Boards category; //Listing 12.9 Multiple virtual functions called in turn #include <iostream> using namespace std; class Mammal { public: Mammal():itsAge(1) { } ...

  1. #1
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427

    Urgent....Array help....I need to know this to go on in the book!!!!!!

    //Listing 12.9 Multiple virtual functions called in turn

    #include <iostream>
    using namespace std;

    class Mammal
    {
    public:
    Mammal():itsAge(1) { }
    virtual ~Mammal() { }
    virtual void Speak() const { cout << "Mammal speak!\n"; }
    protected:
    int itsAge;
    };

    class Dog : public Mammal
    {
    public:
    void Speak()const { cout << "Woof!\n"; }
    };


    class Cat : public Mammal
    {
    public:
    void Speak()const { cout << "Meow!\n"; }
    };


    class Horse : public Mammal
    {
    public:
    void Speak()const { cout << "Winnie!\n"; }
    };

    class Pig : public Mammal
    {
    public:
    void Speak()const { cout << "Oink!\n"; }
    };

    int main()
    {
    Mammal* theArray[5];
    Mammal* ptr;
    int choice, i;
    for ( i = 0; i<5; i++)
    {
    cout << "(1)dog (2)cat (3)horse (4)pig: ";
    cin >> choice;
    switch (choice)
    {
    case 1: ptr = new Dog;
    break;
    case 2: ptr = new Cat;
    break;
    case 3: ptr = new Horse;
    break;
    case 4: ptr = new Pig;
    break;
    default: ptr = new Mammal;
    break;
    }
    theArray[i] = ptr;
    }
    for (i=0;i<5;i++)
    theArray[i]->Speak();
    return 0;
    }

    /* Can someone tell me what happens when let's say you press one and the first choice is called up? This book got ahead of itself and put something arrays here even though the book has not talked about arrays, please help*/
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    >>Mammal* theArray[5];

    That is an array of pointers...... it means that theArray is a group of pointers with 5 members, all of which are pointers to the base class (Mammal).

    What its asking you to do is fill this array with pointers to the objects you are making instances of (Dog,Pig, Mammal.....).

    As they are base class pointers, when you call the virtual function Speak(); for a Dog, it will print Woof, for a pig, it will print Oink....etc


    I recognise this from Teach yourself C++ in 21 days......( I suffered with trying to learn from that book to )......the version I had didnt have the new type headers....but as they are now using the headers and then dumping all of the std namespace in, it shows that they didnt really give it much of a decent revision..... Ah well.......

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. a phone book program
    By mackieinva in forum C Programming
    Replies: 2
    Last Post: 09-19-2007, 06:31 AM
  2. URGENT: Help wanted...in C
    By iamjimjohn in forum C Programming
    Replies: 16
    Last Post: 05-18-2007, 05:46 AM
  3. Replies: 6
    Last Post: 11-09-2006, 02:28 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21