Need to know about Constructor

0 Usama tariq · April 30, 2015

#include
#include
using namespace std;

class Sally{
    int j;
    public:
        Sally (){}
Sally(int x){
j=x;
}

Sally operator+(Sally jd){
Sally newjd;
newjd.j = j + jd.j;
return newjd;

}
getj(){
    return j;
}        
        
};

main(){
Sally a(5),b(2);
Sally c;
c= a+b;
cout
If I dont use blank constructor in this program the compiler giff error.

My question is that is it necessary to give a blank constructor before we give a constructor with arguments?
Or is it just my compiler messing up.

Post a Reply

Replies

Oldest  Newest  Rating
0 Usama tariq · April 30, 2015
Sorry everything went into the code block.
0 Saim Shujaat · April 30, 2015
Try using your constructor with arguments in public. Might work
+1 Matthew Cushing · May 1, 2015
I actually see a couple issues with your code.  I'll tell you about these before answering your question:

1.  main must always be declared as an int and you should return it with 0.  So it should always be

int main()
{

//BODY OF MAIN

return 0;
}

Also, your function "getj()" doesn't have a return type.  It should be declared

int getj()
{
return j;
}

I'm not sure how you were able to compile your code with those syntax errors but anyways onto your question about constructors.  I'm assuming you were watching the tutorial on here since you are using a similar class name to the one in the tutorial about constructors.  The problem Bucky failed to mention in that video is that he didn't mention the difference between a "default constructor" and a "constructor" as well as the fact that he used a string in the video opposed to an integer.  So:

The first constructor you call in your class is the default constructor.  The default constructor's variables must be initialized.
What you encountered by declaring a default constructor with no parameters and then declaring a regular constructor with
what you thought should work is one of the problems with C++.  C++ has a little issue of letting you do things that  you shouldn't do and not letting you know their is a problem.

Also, as a rule of thumb, you should always declare your variables to a default value.  Otherwise you just have garbage in that variable.  Basically, your "default" constructor should look like this:

class Sally
{
int j;
public:
Sally() //DEFAULT CONSTRUCTOR THAT INITIALIZES
{
int j = 0;
}
Sally(int x) //REGULAR CONSTRUCTOR THAT WILL HAVE ARGUMENTS
{
x = j;
}

Now, you can use a constructor with arguments without having to call a default constructor first.  To do so, you must do 2 things.  First is to initialize x to 0 within the parameters and second is to understand what you are doing wrong within the body of your constructor.  You are assigning x into j when you should be assigning j into x.  This is what it should look like:
Sally(int x = 0)
{
    j = x;
}

Hope I helped.
                                        
  • 1

C++

107,275 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator