Noob constructor question

This is a discussion on Noob constructor question within the C++ Programming forums, part of the General Programming Boards category; Hi, I am having a little trouble understanding constructiors and member functions. I am reading 2 books on c++, sams ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    49

    Noob constructor question

    Hi, I am having a little trouble understanding constructiors and member functions. I am reading 2 books on c++, sams teach yourself c++ in 21 days and c++ interactive course. I am up to the chapter on constructors and both books take a different approach to using them. In the second book the member functions are in the class
    example
    Code:
    class dog
    {
    private:
    int d;
    public:
    int bark()
    {
    std::cout<<"Woof";
    }
    };
    and in the first book they just call the function which is outside the class
    Code:
    class dog
    {
    private:
    int d;
    public:
    int bark();
    };
    
    int bark()
    {
    std::cout<<"Woof";
    }
    does it make a difference?, which one is the usual way or doesn't it matter?
    Also, with constructors, do you type its body within the class or outside of it?
    I'm not quite sure if i understand constructors, are they just for defing the classes variables when an object is created and the deconstuctors get rid of them when the object dies?
    Any help with this would be appreciated
    Thanks

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Are these both in the .cpp or are they split to .h and .cpp files (pertains more to the second example)?

  3. #3
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571

    Re: Noob constructor question

    Well, the second example should be

    Code:
    class dog
    {
    private:
    int d;
    public:
    int bark();
    };
    
    int dog::bark()
    {
    std::cout<<"Woof";
    }
    The two ways are virtually identical (the only difference being that the first one acts as if its functions were declared with the inline keyword), but the second is usually preferred. This is because typically, a class is split into its interface (usually put in a .h file) and implementation (usually in a .cpp file).

    Note that in both cases, the functions are a part of the class. In the first case, the function definition is inside the class definition. In the second case, the function is declared in the class definition, but is defined elsewhere.

    Constructors and member functions can be defined within the class definition, or outside of it. It doesn't truly matter.
    Last edited by Cat; 08-08-2003 at 11:44 PM.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    249
    which one is the usual way
    Member function definitions are usually outside the class braces, except when they're just used to do a very small task that typically fits on one line. Remember that, you are not calling the function anywhere in your example. Just remember:

    Code:
    class foo
    {
    
    void poo(int x); //decleration
    
    };
    
    ...
    
    poo(x); //call
    
    ...
    
    void foo::poo(int x) //definition
    {
    return x;
    }
    I'm not quite sure if i understand constructors, are they just for defing the classes variables when an object is created and the deconstuctors get rid of them when the object dies?
    Pretty much, yes. Constructors are used to initalize the class variables, so that each time you create a new varaible of that class type, you don't have to worry about intializing every single variable. Very convenient.
    Deconstructors arn't such a big deal. You normally don't need them unless your are allocating memory in the class(ie using the new operator). Correct me if I'm wrong?

    sams teach yourself c++ in 21 days
    That's an awful book IMO. Plus you can get it online for free. Why don't you search these boards for some better C++ books out there, there are tons of topics on it. I'm sure you'll find one that you like better.
    Last edited by funkydude9; 08-09-2003 at 11:05 AM.
    Well, there are a few things wrong with your code:

    1) It does not work.
    2) It does not work.
    3) It does not work.

    Hope this helps.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    funkydude,

    Code:
    void poo(int x) //definition
    {
    return x;
    }
    Looks like you didn't learn anything from this thread. Try reading Cat's post.

    "Deconstructors arn't such a big deal."

    Deconstructors?? Good grief.

    Elite,

    A great beginning C++ book is "Ivor Horton's Beginning C++". At the very least, that is a great reference for any beginner. Throw your present books in the trash, and get the book I recommended, and you will have an easier time learning C++. Good luck.
    Last edited by 7stud; 08-09-2003 at 01:48 AM.

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    249
    Looks like you didn't learn anything from this thread. Try reading Cat's post.
    Heh, whoops.
    Well, there are a few things wrong with your code:

    1) It does not work.
    2) It does not work.
    3) It does not work.

    Hope this helps.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Noob to programming need help with a project
    By Wheelsonbus in forum C Programming
    Replies: 6
    Last Post: 02-25-2009, 03:46 AM
  2. Noob in need of help.
    By gec5741 in forum C++ Programming
    Replies: 18
    Last Post: 01-23-2009, 03:25 PM
  3. Noob printf question
    By lolguy in forum C Programming
    Replies: 3
    Last Post: 12-14-2008, 08:08 PM
  4. noob needs help!!!!:(
    By tykenfitz in forum C Programming
    Replies: 1
    Last Post: 07-10-2005, 09:49 AM
  5. Just a little request from a noob...
    By Lee134 in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 03-18-2005, 05:45 AM

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