Thread: Need Help Understanding What The Heck I'm Doing On "Classes"

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    67

    Need Help Understanding What The Heck I'm Doing On "Classes"

    Hey everyone, sorry for bringing up this question, knowing that there are thousands of online tutorials and books, but I have gone through mostly every kind of tutorial I can find, and I still need some better explaining on when too set up a 'class' and how..?

    I'm quite new too C++ but knowledgable on the basics, and I'm very interested in 'classes'. I know that a class HAS to have a constructor and a destructor ( I know a constructor initialize's variables, but I don't understand what they mean about the destructor being used to clean up the constructor's mess?) I know about "public", "private", and "protected", but besides that I just can't understand what i'm doing..?

  2. #2
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    i think that c++ class do not really need to include a destructor unless you have create allocated memory with pointer.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    48
    Every class doesn't need to have a constructor/destructor, but every class will have one in the end. If you do not provide one, the compiler will create one. A constructor is the very first thing that happens when an object is created, while the destructor is called when a object is deleted or goes out of scope.


    Consider

    Code:
    
    ---------------------- Car.h----------------------
    class Car
    {
        public:
            Car();
            virtual ~Car();
            void Drive();
    
    
        protected:
        private:
            int numberOfWheels;
            Engine *engine;
    };
    
    ---------------------- Car.cpp----------------------
    #include "Car.h"
    
    
    Car::Car() :
    numberOfWheels(4)
    {
        this->engine = new Engine(4); // Create a 4 cyl engine for this car
    }
    
    
    Car::~Car()
    {
        delete engine;
    }
    
    
    void Car::Drive()
    {
        engine->start();
        // Do stuff
        engine->stop();
    }
    
    ---------------------- Main.cpp----------------------
    #include "Car.h"
    
    
    int main(int argc, char** argv)
    {
        Car *myCar = new Car(); // <-- Constructor called
        myCar->Drive();
        delete myCar; // <-- Destructor called
        
        { // Beginning local scope
            Car differentCar; // <-- Constructor called
            differentCar.Drive();
        } // Scope ends, differentCar destructor called
    }
    As you can see, when car is "new"'d, the constructor is called. The constructor then sets up some default variables and in this case, allocates some memory to represent the engine ( a class not included ). Then delete is called, which results in the constructor getting called to clean things up. If there wasn't a destructor, the memory allocated for engine would never have been released, resulting in a memory leak.

    Then back to main.cpp, you will notice another Car being created, although not using new. This still results in the constructor being called. Then when the variable differentCar goes out of scope, the destructor is called and its memory is cleaned up and the object is disposed.

    You can actually have multiple constructors, that take a variety of parameters. For example, I could have added: Car::Car(int numCylinders) and Car::Car(string carName), etc. Constructors however cannot return a value, so you should keep internals to a minimum. There is however only ever a single destructor.

    Quote Originally Posted by cplusplusnoob View Post
    Hey everyone, sorry for bringing up this question, knowing that there are thousands of online tutorials and books, but I have gone through mostly every kind of tutorial I can find, and I still need some better explaining on when too set up a 'class' and how..?

    I'm quite new too C++ but knowledgable on the basics, and I'm very interested in 'classes'. I know that a class HAS to have a constructor and a destructor ( I know a constructor initialize's variables, but I don't understand what they mean about the destructor being used to clean up the constructor's mess?) I know about "public", "private", and "protected", but besides that I just can't understand what i'm doing..?
    Last edited by Serapth; 11-29-2011 at 09:49 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Understanding an "Inorder Traversal"
    By madmax2006 in forum C Programming
    Replies: 3
    Last Post: 02-25-2010, 04:52 AM
  2. Help understanding how "operator char*()" works.
    By Lawn Gnomusrex in forum C++ Programming
    Replies: 28
    Last Post: 10-05-2008, 01:59 PM
  3. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  4. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM
  5. What the heck is a "syntax error"...?
    By face_master in forum C++ Programming
    Replies: 1
    Last Post: 08-28-2001, 06:06 AM