About class constructors (CPP) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

About class constructors (CPP)

I'm now on the stage that explains work with constructors. I implemented something like a constructor in my little project. Actually, in "real life" I wouldn't do it (use class) in the code like that but I need experience using constructor. The code works like it is but frankly speaking I do not understand why I declared this constructor that way and why I should input same data twice in lines 71 and 72. I am really confused. Could anybody briefly explain how I can correctly use constructors in my case or at least refer me in right way? Thanks. https://code.sololearn.com/c8A8S6Km5rK4

27th Dec 2019, 10:02 PM
Dmitriy Sychov
Dmitriy Sychov - avatar
4 Answers
+ 2
Why not just use the class' private variables in the function(s), which you already provided through the constructor, instead of providing them again in the function parameters? The question, for you, is more like should windDir call windSpeed to get the value of tws, or should the user do it and provide the argument? 1 might be an optimization option, the other easier to use?
27th Dec 2019, 11:13 PM
Dennis
Dennis - avatar
+ 1
CeePlusPlus, It was clear that I had to give some data to a function. I was confused why I couldn't awoid entering the same data in line 71. Looks like it isn't a problem of lines 71 and 72 but of the lines where I declare the class and its constructor in the beginning.
27th Dec 2019, 10:15 PM
Dmitriy Sychov
Dmitriy Sychov - avatar
+ 1
@Dimitriy Sychov You had to enter the same data in line 71 because you made the constructor have some parameters to input. Also, you can do what @Dennis said, use the class's private variables for the function(s).
27th Dec 2019, 11:17 PM
CeePlusPlus
CeePlusPlus - avatar
0
You have to input the same date in line 72 because the function requires parameters.
27th Dec 2019, 10:07 PM
CeePlusPlus
CeePlusPlus - avatar