Accessing values of class objects

This is a discussion on Accessing values of class objects within the C++ Programming forums, part of the General Programming Boards category; I am building a program requiring making some classes with inheritance. I can't seem to be able to access the ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    2

    Accessing values of class objects

    I am building a program requiring making some classes with inheritance. I can't seem to be able to access the members of the class objects. My code looks like this:

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <time.h>
    
    class Animal{
          public:
          int number;
          int legs;
          Animal(){
               number = rand()%9;
               }
          };
          
    class Fish : public Animal{
          public:
          Fish () : Animal (){
               legs = 0;
               }
          
          };
          
    class Bird : public Animal{
          public:
          Bird () : Animal (){
               legs = 2;
               }
          };
          
    class Insect : public Animal{
          public:
          Insect () : Animal (){
               legs = 6;
               }
          };
          
    
                
    int main() {
        
        Animal *a, *b, *x;
        
        a = new Fish;
        b = new Bird;
        x = new Insect;
        
        cout<< a.number << "\t" << a.legs << endl;
        cout<< b.number << "\t" << b.legs << endl;
        cout<< x.number << "\t" << x.legs << endl;
        
        system("pause");
        return 0;
    }
    Basically, all animals have a random number, and each animal type has a different number of legs. When running, I get an error saying that 'number' and 'legs' are not declared. Help?

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    I think you meant to do this:
    Code:
    cout<< a->number << "\t" << a->legs << endl;
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    2

    Solved!

    Awesome! Exactly what I needed... Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Best way to report errors in class objects
    By IceDane in forum C++ Programming
    Replies: 3
    Last Post: 08-16-2009, 12:08 PM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  4. Replies: 4
    Last Post: 06-18-2005, 02:26 PM
  5. accessing Class members
    By fizisyen in forum C++ Programming
    Replies: 3
    Last Post: 01-07-2004, 05:18 PM

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