Okay, I am still trying to get used to C++ since I mostly write in Java. I am also new with the two constructor thing mentioned in detail below.

Here, I am investigating on writing a class with two constructor. That is, I have heard that you are allowed to write a class with a constructor and another constructor which is basically the same but has more parameters in it.

eg. First constructor might be add()
Second constructor might be add(int x, int y)

How will I go about getting both to work correctly depending on which to use? I know that constructors go into the .h file so I tried putting them on back to back lines. I tried searching online to see am I on the right track but there was not the result I was looking for.

Yes, you're on the right track.
Just write it and you'll see.

I tried and when I write the function for the class, it produces problems with the second constructor. I think this stems to the fact that the function does not specify exactly which constructor to use, and it shouldn't anyways. So how do I eliminate this problem? Sorry for being too vague.

well Limiter,

compiler knows which constructor to use by the number of arguments you specify at the time of creating a new object of that class. suppose you wrote the class with name "add" (as apparent from your chosen constructor name) and now you want to create two objects of this class, "obj1" and "obj2" using first and second constructors respectively then you would create these objects as follows:

add obj1; //object created using parameter less constructor
add obj2(3, 5)// object created using two parameter constructor

Now if this is not exactly what you are trying (and subsequently failing) to do then please paste all the code from all files of your program so that we can look at it more closely and better understand your problem. You may also be getting some syntax error.

Minor comment to the above. You said

I know that constructors go into the .h file

Fortunately, that isn't true.

A lot of beginners (with C++/C) seem to think that there is some magic about .h and .cxx or .cpp files. There isn't. The compiler just (effectively) makes a single file out of all the #include "file" and the file that is being compiled. If you like you can declare all of your classes in .cpp files and your code in .junk files, it doesn't matter, it is just a convention.

Once you are aware of that, you only have to consider that the compiler tries to have the minimum information that can allow it to compile each file to make an object file, and that it is doing in top-to-bottom. The after object files have been made, the linker needs everything.

In practice that means that if you want to specify any function of a class, you need 100% of the class's declaration, and 100% of the declaration any other classed that you want to use, but 0% of any definition.

Example:

// this Is the DECLARATION 
class A
{
   private:
      // stuff..
   public: 
      void method();
};
// THIS CODE MUST BE BELOW the declaration either in the same file or by an include:
void A::method()
{
   // do stuff
}

The example is just what you need when you compile, the includes are a quick way to help you not have to copy the same code in different places.

Constructors are the same as methods, e.g.

// this Is the DECLARATION 
class A
{
   private:
      int value;
      // stuff..
   public: 

      A(const int);         // declaration of a constructor 
      void method();
      // stuff.
      A(const double,const double);      // another constructor
};

// THIS CODE MUST BE BELOW the declaration either in the same file or by an include:
A::A(const int x) : value(x)
{
   // do stuff
}

// stuff
void A::method() { // stuff }

// this could be in a different file to the previous constructor.
A::A(const double A,const double B) : value((A>B) ? 1 : -1)
{}

In C++ you normally have at least two constructors, typically [no always] you will have a default constructor e.g. A::A() {} that takes no values, and
typically you will have another constructor, called the copy constructor e.g. A::A(const A& Other) {} . The context of the call determines the constructor called e.g.

A item;        // default constructor called.
A nextItem(item);  // copy constructor called
A* IPtr=new A();    // default constructor called.

Bit long (sorry) but this seems to be a continuing mistake of many of the Java to C++ programmers. Hopefully, this makes the problem clear.

You identify which constructor to use by its 'signature'.
The signature is the name of the class, plus the number, data type and order of the parameters.
Therefore in order to create an object using the constructor of your choice you only need to invoke the correct class name and pass it the correct number of parameters. Each parameter must be of the correct data type and in the correct order. That is basically how the particular constructor is identified.

Okay, I am still trying to get used to C++ since I mostly write in Java. I am also new with the two constructor thing mentioned in detail below.

Here, I am investigating on writing a class with two constructor. That is, I have heard that you are allowed to write a class with a constructor and another constructor which is basically the same but has more parameters in it.

eg. First constructor might be add()
Second constructor might be add(int x, int y)

How will I go about getting both to work correctly depending on which to use? I know that constructors go into the .h file so I tried putting them on back to back lines. I tried searching online to see am I on the right track but there was not the result I was looking for.

Thanks, that is pretty clear, I will let you know if I have more problems.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.