Beginners' Work

This is a discussion on Beginners' Work within the C++ Programming forums, part of the General Programming Boards category; This is my current code. Code: #include <iostream.h> int main() { class Animal { private: int Animals[20]; protected: float Height; ...

  1. #16
    Registered User
    Join Date
    Jul 2005
    Posts
    100
    This is my current code.

    Code:
    #include <iostream.h>
    
    int main() {
    
    class Animal { 
    private:
    	int Animals[20];
    protected:
    	float Height;
    	float Weight;
    }; //end class Animal
    
    class Bird : protected Animal {
    private:
    	float Wingspan;
    	int BirdsArray[10];
    }; //end class Bird
    
    cout << "--------------Bird Array Program.-------------" << endl;
    cout << "-Please type in your Bird to store in the program." << endl;
    
    enum Bird {Height, Weight, Wingspan}
    
    }//end Main
    How do I store the information into an array? Hmm..
    Last edited by blankstare77; 07-22-2005 at 05:27 PM.

  2. #17
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    You should really be adding a little code and compiling, then adding a little code and compiling. If the compiling fails, you should not add any more code until you fix the errors. At the rate you are going you will have too many compile errors to fix before you ever attempt to compile your code. Just a suggestion.

  3. #18
    C++ Beginner
    Join Date
    Jun 2005
    Posts
    39
    I'm sorry to tell you this, but that is completely wrong. =\
    You have to have at least some public objects in classes, not just private and protected, and cout isn't just going to cut it... here is a basic little class aplication that maybe you can learn and work with.

    Code:
    class Dog
    {
    public:
    Dog();
    ~Dog();
    int getage();
    int getheight();
    void setage(int age);
    void setheight(int height);
    void bark();
    private:
    int age;
    int height;
    };
    Dog::Dog()
    {
    //constructor this is a basic example so it does nothing
    }
    ~Dog::Dog()
    {
    //destructor this is a basic example so it does nothing
    }
    int Dog::GetAge() //basic class function
    {
    return age; //returns age
    }
    void Dog::SetAge(int age) //basic class function, returns nothing
    {
    age = 5;
    }
    void Dog::Bark()
    {
    cout << "Ruff Ruff" <<
    }
    
    int Dog::Getheight() //basic class function
    {
    return height; //returns height
    }
    void Dog::SetHeight(int height) //basic class function, returns nothing
    {
    height = 29;
    }
     
    main()
    {
    Dog Clifford;
    Clifford.Setage(int age);
    Clifford.SetHeight(int height);
    Bark();
    cout << "Clifford is << Clifford.GetAge << "years old/n" <<
    cout << "Clifford is also" << Clifford.GetHeight << "inches tall/n" <<
    return 0;
    }
    Note: I just made this up and didn't compile but it should work.
    I'm a beginner C++ programmer, but I have studied HTML and Java. So if you need to help me I should catch on fast =)

  4. #19
    C++ Beginner
    Join Date
    Jun 2005
    Posts
    39
    my fault I meant Clifford.Bark(); instead of just Bark();
    I'm a beginner C++ programmer, but I have studied HTML and Java. So if you need to help me I should catch on fast =)

  5. #20
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void Dog::SetHeight(int height) //basic class function, returns nothing
    {
    height = 29;
    }
    I don't think this does what you think it does. As a matter of fact, a whole lot of that doesn't do what you think it does.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #21
    Registered User
    Join Date
    Jul 2005
    Posts
    100
    I'm really confused.

  7. #22
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Renegade, it doesn't... you are assigning to a local variable (the function parameter), not the class member.

  8. #23
    Registered User
    Join Date
    Jul 2005
    Posts
    100
    Okay, first of all, who's right. Second of all, can you help me? Why is mine so wrong?

  9. #24
    C++ Beginner
    Join Date
    Jun 2005
    Posts
    39
    nvm that was my fault, i just worded it terribly, i just get annoyed when some1 fixes what i say when i think it's right. But it was wrong, and I admit it. I knew what i was talking about but i worded it wrong and u guys prolly can't read minds.
    I'm a beginner C++ programmer, but I have studied HTML and Java. So if you need to help me I should catch on fast =)

  10. #25
    Registered User
    Join Date
    Jul 2005
    Posts
    100
    Quote Originally Posted by Renegade
    nvm that was my fault, i just worded it terribly, i just get annoyed when some1 fixes what i say when i think it's right. But it was wrong, and I admit it. I knew what i was talking about but i worded it wrong and u guys prolly can't read minds.
    Okay but is your code right? Or did you just word some things wrong? Ahh.

  11. #26
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Renegade's code is not right. It has several things wrong with it, but those should probably be fixed in their own different thread.

    blankstare77, your code has several things wrong with it, but most of all I think you are going about this task the wrong way. As I said you should make sure every little change you make compiles. So start with this:
    Code:
    int main()
    {
    }
    Make sure that compiles. Then maybe add an empty class:
    Code:
    class Animal
    {
    };
    
    int main()
    {
    }
    Make sure that compiles. Continue adding one thing at a time and make sure it compiles. If it doesn't, chances are that whatever you just added was wrong. Then after you've got some actual code that compiles, you can ask if it is the right idea and nobody will have to waste time on the incorrect syntax and we can focus on the design and technique.

  12. #27
    Registered User
    Join Date
    Jul 2005
    Posts
    100
    Quote Originally Posted by Daved
    Renegade's code is not right. It has several things wrong with it, but those should probably be fixed in their own different thread.

    blankstare77, your code has several things wrong with it, but most of all I think you are going about this task the wrong way. As I said you should make sure every little change you make compiles. So start with this:
    Code:
    int main()
    {
    }
    Make sure that compiles. Then maybe add an empty class:
    Code:
    class Animal
    {
    };
    
    int main()
    {
    }
    Make sure that compiles. Continue adding one thing at a time and make sure it compiles. If it doesn't, chances are that whatever you just added was wrong. Then after you've got some actual code that compiles, you can ask if it is the right idea and nobody will have to waste time on the incorrect syntax and we can focus on the design and technique.
    The only bad thing about that is that I can't test class structures I can only test functions. People here say this is wrong:

    Code:
    class Animal { 
    private:
    	int Animals[20];
    protected:
    	float Height;
    	float Weight;
    }; //end class Animal
    
    class Bird : protected Animal {
    private:
    	float Wingspan;
    	int BirdsArray[10];
    }; //end class Bird
    Really I just need to know how to store information into the array. I strongly believe that's right because I double-checked my inheritance, and why do people say that i can't have so many private variables? Why should I use public variables?
    Last edited by blankstare77; 07-22-2005 at 09:23 PM.

  13. #28
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    You can test classes just like you can test functions. If you don't know how to use classes to test them, then that will make it more difficult, but if that is the case then you should consult your book or tutorial instead of just trying things and asking for complete explanations.

    You should use private member variables, that part is correct.

    You need a way to access those private members, though. An easy way to do that is to provide get and set functions. For example, you could make a public function part of animal called getHeight that returns the height. You could make another public function setHeight that allows you to set the height. If you want to store information in the array, you will need a function that is part of the class that can add information to the array.

    Of course, if your array is meant for the arrays mentioned in the problem at the top of this thread, then you aren't thinking of it correctly. The array inside the Animal class should be a character array to hold the description (Desc) of the animal. You have an int array. The array you put inside the Bird class is supposed to hold Birds. That array should be a variable inside your main function or some other function, it should not be part of Bird. Think about it, is a group of birds really a property of a bird?

    You need code inside your main function that creates a Bird and sets its data. You need either a constructor or some other functions to set that data inside the Bird and inside the Animal base class. The inheritance should be public instead of protected. There are other issues. If you start over with new code and add one thing at a time, you will catch the compile errors, and there won't be so many issues to deal with at once.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  4. Problems in getting OpenGL to work
    By zonf in forum C Programming
    Replies: 5
    Last Post: 02-13-2006, 03:48 AM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM

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