+ 7

constructor is confusing

i am trying to make this code using constructors but it isnt working properly. plzz help. i think the problem is with constructor... plz explain how a constructor works and its syntax... for reference regarding constructors i have already visited the following websites https://www.codesdope.com/cpp-classes-and-objects/ http://www.learncpp.com/cpp-tutorial/85-constructors/ https://www.tutorialspoint.com/cplusplus/cpp_constructor_destructor.htm and sololearn too... but no help. https://code.sololearn.com/cXJW4ZX9XPqw/?ref=app

28th May 2018, 9:58 PM
Saurabh Tiwari
Saurabh Tiwari - avatar
5 Respostas
+ 1
Needs to be... s1 = a; s2 = b; Your code currently saying "take the values of s1 and s2 and use them to overwrite the values of a and b I just passed in" class area { public : int s1,s2; area(int a, int b) { //specifying that must be passed 2 int arguments s1= a; //assigning a to member s1 s2= b; //assigning b to member s2 } /* note that we don't need parameters for returnarea. We can already get values from object members s1 and s2, since they were assigned in constructor, which must be called before invoking this function. */ void returnarea() { cout<<s1*s2; } }; int main() { int a, b; cin>>a>>b; area *blah = new area(a,b); // passes a and b to constructor, assigns pointer to blah blah -> returnarea(); //calls returnarea function on object return 0; } (apologies if this isn't the epitome of great code. Don't know C++ very well haha... works though)
29th May 2018, 3:56 PM
kumquatfelafel
kumquatfelafel - avatar
+ 6
Hector Angel still it isnt working... btw thnx 4 ur reply
29th May 2018, 4:33 AM
Saurabh Tiwari
Saurabh Tiwari - avatar
+ 5
hi there your constructor must receive the parameters ex. class area { public : int s1,s2; area(int a, int b) //declaring constructor { a=s1; b=s2; returnarea (a,b); } void returnarea(int l,int w) { cout<<l*w; } }; int main() { int x; int y; cin>>x>>y; area s = s.new(x,y) return 0; }
28th May 2018, 10:18 PM
Hector Angel
Hector Angel - avatar
+ 4
kumquatfelafel thnx for the help... can u explain me your code...
29th May 2018, 4:28 PM
Saurabh Tiwari
Saurabh Tiwari - avatar
+ 1
I will try, though take what I say with a grain of salt, because, as stated, don't really know C++ very well. First, let's take a look at some code that accomplishes something more along the lines of what you appear to have been trying to do originally. class area { public : int s1,s2; area() //declaring constructor { } void returnarea() { cout<<s1*s2; } }; int main() { area *s = new area(); //This creates a new area object and assigns pointer to s cin>>s -> s1>> s -> s2; //this assigns values to members s1 and s2 of object at s s -> returnarea(); // this invokes returnarea function on s return 0; } At least, that's my understanding of it? In any case, the ints s1 and s2 here are specific to an individual area object, so you must create object before trying to assign values. You couldn't, e.g., give a baby a name without first having a baby. "new area()" here creates area -edit- modified original answer. See above
29th May 2018, 4:43 PM
kumquatfelafel
kumquatfelafel - avatar