Problem with classes.

This is a discussion on Problem with classes. within the C++ Programming forums, part of the General Programming Boards category; Hello, im still learning classes. But im just not sure of something. Let's say i make a new file called ...

  1. #1
    deletedforumuser
    Guest

    Problem with classes.

    Hello, im still learning classes. But im just not sure of something.

    Let's say i make a new file called Dog.cpp and another one called Dog.h

    In the Dog.h i create the class, and in the Dog.cpp i set the functions.


    Isn't it supposed to link itself? Do i like need to include Dog.h into Dog.cpp?


    And another question about classes.

    Code:
    class Dog
    {
    
    
    public:
    	
    	Dog(string Color, int hisAge);
    	~Dog();
    	string getColor();
    	int getAge();
    
    
    private:
    int age;
    
    };
    
    
    void Dog::Dog(string Color, int hisAge)
    {
    
    	hisAge = age;
    
    }
    
    void Dog::~Dog()
    {
    
    }
    
    int getAge()
    {
    	return age;
    }
    
    string getColor()
    {
    
    	return Color;
    
    }
    How come this code give me an error, and please, could you help me improve it so i will be better at classes next time. Thank you.

  2. #2
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    you need to declare the scope of the function

    Code:
    string getColor()
    {
    }
    this is an "unbound" function; i.e. not a member of your class.

    if you change this to:

    Code:
    string dog::getColor()
    {
    }
    now the compiler can identify that getColor is a member of dog.

    this is not required if you provide the implementations inside the class declaration (which is not recommended), because anything declared inside the class definition is, by definition, a member of that class.

  3. #3
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    and yes, you need to #include "dog.h" in dog.cpp

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    >> Do i like need to include Dog.h into Dog.cpp?
    Yes, #include "Dog.h".

    >> How come this code give me an error
    What error does it give you? Did you #include<string>? Did you use the std namespace? Did you forget to add a member variable for the Color (since you have one for the age)?

    >> could you help me improve it so i will be better at classes next time.
    You don't need to write the destructor if it is empty, but it's fine for learning right now. You should use the initializer list to intialize your member variables, look that up.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    What C++ book are you using? This should be explained in any basic treatment of classes. Anyway, the typical approach is to define the class in the .h file, and implement the member functions in the .cpp. For this, of course you have to include the .h in the .cpp. In the .cpp, you also have to qualify the member function names with the class name, e.g. Dog::getColor as opposed to just getColor.

    Google for "Thinking in C++" - it's a free, very excellent C++ book.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    void Dog::Dog(string Color, int hisAge)
    {
    
    	hisAge = age;
    
    }
    This is back to front - you are setting the incoming parameter "hisAge" to the (not yet defined) value in the class held in "age". Since hisAge is a copy of what was originally in the code, you are essentially not doing anything here.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with classes and pointers
    By Akkernight in forum C++ Programming
    Replies: 18
    Last Post: 02-21-2009, 05:21 AM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Having a problem with Classes
    By FoxTrot in forum C++ Programming
    Replies: 10
    Last Post: 09-06-2007, 07:40 PM
  4. Problem with destructors.
    By Hulag in forum C++ Programming
    Replies: 7
    Last Post: 06-11-2004, 12:30 PM
  5. problem w/ nested templatized classes
    By *ClownPimp* in forum C++ Programming
    Replies: 8
    Last Post: 10-19-2002, 07:58 AM

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