Thread: Constructor

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    57

    Constructor

    can i pass parameters into the initial constructor or do i have to use another one

    class blabla
    {
    blabla(//some parameters here??)
    blabla(//or in this one if i want)
    ~blabla()
    //and so on
    }

    i kno i can overload the constructor but can the initial one take parameters

    was writing a fair size program using structs and then stumbled across these (might as well start over because classes are the answer)

    classes rock
    =@-OmegatronO-@=

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Yes you can!

    Code:
    class Test
    {
        public:
        int iValue;
        Test();  // Default
        Test( int iTemp ); // Alternate
        ~Test();
    };
    
    Test::Test()
    {
        iValue = 0;
    }
    
    Test::Test( int iTemp )
    {
        iValue = iTemp;
    }
    
    Test::~Test()
    {
    }
    
    int main()
    {
        Test Class1;  // Uses default constructor, iValue is 0;
        Test Class2( 50 ); // Use alternate constructor, iValue is 50.
    
        return 0;
    
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    57
    so is it like this:

    class blabla
    {
    //all default parameters declared here
    blabla()
    blabla(//or in this one if i want)
    ~blabla()
    //and so on
    }
    =@-OmegatronO-@=

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >can i pass parameters into the initial constructor
    Every class has a default constructor that takes no arguments, you can then overload the constructor to do whatever you want in different situations.
    Code:
    class Fooby
    {
        int data;
      public:
        // Must have a default, but you don't have to define it
        Fooby();
        // You can also overload
        Fooby ( int input );
    };
    
    // Fooby class fleshy
    Fooby :: Fooby() : data ( 10 )
    {
      // data is set to a default value of 10
    }
    
    Fooby :: Fooby ( int input ) : data ( input )
    {
      // data is set to user input
    }
    The default constructor is just that, you don't HAVE to have it in your code because the class will generate it for you. If you want something specific to be done in the constructor then you will either have to define the default or overload.

    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User JasonLikesJava's Avatar
    Join Date
    Mar 2002
    Posts
    175
    Can you do this?

    class Fooby
    {
    int data;
    double data2;
    public:
    Fooby();
    Fooby ( int input );
    Fooby (double input);
    };

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Can you do this?
    Yes, the compiler determines which overloaded function to use based on the type and number of arguments, so two overloaded constructors with one argument each but of different types is quite legal.

    -Prelude
    My best code is written with the delete key.

  7. #7
    Unregistered
    Guest
    Yes, that's a type of polymorphism. (For constructors)

    Code:
    class Object {
    public:
              Object();
              Object(double t);
              Object(char* t);
              ~Object();
    };
    Depending on the arguments passed, the compiler will choose the appropriate function from it's look-up list.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 06-10-2008, 08:38 PM
  2. C++ have a constructor call another constructor
    By QuestionC in forum C++ Programming
    Replies: 4
    Last Post: 05-17-2007, 01:59 AM
  3. Replies: 3
    Last Post: 03-26-2006, 12:59 AM
  4. Need help in classes
    By LBY in forum C++ Programming
    Replies: 11
    Last Post: 11-26-2004, 04:50 AM
  5. Constructor with Parameter not Firing
    By BillBoeBaggins in forum Windows Programming
    Replies: 4
    Last Post: 08-26-2004, 02:17 PM