Thread: Constructors

  1. #1
    OCD script writer syrel's Avatar
    Join Date
    Sep 2004
    Posts
    12

    Constructors

    k, i have looked through 2 different books and my instructor doesnt have the greatest knack for explaining things.

    Can anyone briefly explain the point of using a constructor within a class? Is it just a required piece of code needed for classes? Or is it just an attempt at confusing the compiler for giving such incredibly odd and annoying error messages?

    i understand that a constructor is a member of the class that has the same name but does not return anything, but i cant find out why a constructor is necessary. any help is appreciated.
    (3.0 Units of C++ Data Structures) - (Lab Time) = Death

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    In a constructor you would initalize all the member variables to certain values. Basically a constructor is what allows you to put an object into a known state.
    Last edited by Thantos; 09-21-2004 at 01:55 AM.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    The constuctor of a class is called automatically when the class object is created. For this reason, the constuctor is a good place to initialize variables and do any other setup that is necessary for your class.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Arrays and Constructors
    By Verdagon in forum C++ Programming
    Replies: 1
    Last Post: 07-20-2005, 07:20 PM
  2. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 04:24 PM
  3. constructors, arrays, and new
    By Thantos in forum C++ Programming
    Replies: 6
    Last Post: 05-30-2004, 06:21 PM
  4. constructors in classes
    By Kenman in forum C++ Programming
    Replies: 16
    Last Post: 07-28-2003, 07:35 AM
  5. Copy constructors and private constructors
    By Eibro in forum C++ Programming
    Replies: 5
    Last Post: 11-24-2002, 10:16 AM