Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Default Constructors

NosiphoNkonyaneNosiphoNkonyane JohannesburgMember Posts: 1

hi guys,
i am a programming student and i am struggling with a default constructor that is supposed to initialize private variables to 0 and nulls. i am very lost.

Answers

  • pseudocoderpseudocoder Member Posts: 701 ✭✭✭

    your member functions have direct access to variables of that class. in the default constructor, assign any variables as required. you can also make a constructor that takes arguments to assign to the variables.

    say you have a class named "foo" with a private variable named "int_var", a default constructor, and a constructor that takes an integer as an argument

    foo::foo()
    {
        // default constructor
        int_var = 0;
    }
    
    foo::foo(int init_value)
    {
        // constructor that takes an integer
        int_var = init_value;
    }
    

    to call the default constructor, instantiate a foo object with no argument. to call the other constructor, pass in an integer.

    foo a          /* call default constructor */,
        b(100);    /* call constructor taking an argument */
    

    at least, that how I remember it... I don't use c++ very often.

Sign In or Register to comment.