constructor

are you really force to pass arguments when you write a constructor with parameters?

EDIT: revising question

when you create an object of a class and then you create a constructor with parameter, are you really force to pass arguments to it?
Last edited on
yes... why wouldnt you be? would you write a function that takes arguments and then complain that it takes arguments? why not just write a default constructor?
closed account (SECMoG1T)
Hi , i agree but i think it is also possible to write constructors taking default arguments that way you can either supply the arguments at will or just omit them .

for example, consider this class that lacks a default constructor but i can create an object without supplying required arguments.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Foo
{
  public:
      Foo(const std::string nm=std::string("NULL"),int val=0)
       :name(nm), value(val){}

       void print() const
       {
           std::cout<<"Name : "<<name<<" Value : "<<value<<std::endl;
       }
  private:
    std::string name;
    int value;
};

int main()
{
     Foo with_no_args, with_args("Named",100);

     with_no_args.print();
     with_args.print();
}


Last edited on
@Little Bobby Tales
can you give me example code of class with default constructor?
those are default arguments... which makes Foo() the same thing as a default constructor, except that you now have to create two new variables when calling that constructor, and then copy the values of the variables into the members, whereas it is a lot faster to just have a default constructor that supplies those values
@andy1992
oh i see
but you have created a constructor with the case that if you dont pass argument it will print null and 0.

those are default arguments... which makes Foo() the same thing as a default constructor, except that you now have to create two new variables when calling that constructor, and then copy the values of the variables into the members, whereas it is a lot faster to just have a default constructor that supplies those values 

oh i see i get it now
closed account (SECMoG1T)
A default argument can take any value that you wish to assign to it,.

Note that the example I gave is just limited to two value of the class Foo so the constructor I gave is almost equivalent to Foo() as #little bobby Tales explained above but to my opinion default arguments can do more than that , for example you can make a single constructor that equates to two or more class constructors for example

consider the following two classes Bar and Foo, Bar have five constructors while Foo have a single constructor that is equivalent to those owned by Bar.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class Bar
{
  public:
      Bar()=default;
      Bar(const std::string& nm)
       : name(nm),value(0),number(0),level(""),state(false){}
      Bar(const std::string& nm,int val)
       : name(nm),value(val),number(0),level(""), state(false){}
      Bar(const std::string& nm,int val,int no)
       : name(nm),value(val),number(no),level(""),state(false){}
      Bar(const std::string& nm,int val,int no,const std::string& lvl)
       : name(nm),value(val),number(no),level(lvl),state(false){}
      Bar(const std::string& nm,int val,int no,const std::string& lvl,bool stte)
       :name(nm),value(val),number(no),level(lvl),state(stte){}
  private:
     std::string name;
     int value;
     int number;
     std::string level;
     bool state;
};

class Foo
{
    public:
       Foo(const std::string& nm="",int val=0,int no=0,const std::string& lvl="",bool stte=false)
        :name(nm),value(val),number(no),level(lvl),state(stte){}
    private:
       std::string name;
       int value;
       int number;
       std::string level;
       bool state;
};


you can create a Foo object in any manner that a Bar object is created, however there are rules that must be followed when defining default arguments :-

i.e. If a parameter takes a default argument then all parameters to it's right side must also take default arguments.
That Bar could use a C++11 feature: delegating constructors.


The default argument values are an informative convenience feature. The function itself does not care about that value. The default value is stated in the declaration.

When you have a foo( int = 42 ); declaration and you write a function call foo() the compiler will replace your function call with a foo(42)


Both function overloading and default arguments have their uses. However, do consider http://en.cppreference.com/w/cpp/language/overload_resolution
Topic archived. No new replies allowed.