+ 1

Some one please tell me whats wrong with this code😄😄

I thought after making the constructor i was supposed to get access to the private integer https://code.sololearn.com/cFsTD7pE19Dl/?ref=app

12th Nov 2020, 4:58 PM
Michael Kamau
Michael Kamau - avatar
4 Answers
+ 2
you're using wrong variable here. the instance variable named "distance" but you're using "km" in Calculate and LetsSee method, same with NoOfSteps. you'll also need to define another constructor without parameter if you want to initiate it like. LestWalk Check otherwise you'll need to provide the argument needed when initiating the object
12th Nov 2020, 5:13 PM
Rei
Rei - avatar
+ 1
Change km in Calculate() and LetsSee() to distance. Add a default constructor in LetsWalk. Otherwise pass arguments to the LetsWalk object.
12th Nov 2020, 6:26 PM
Mensch
Mensch - avatar
+ 1
to explain practically what rai says do this NOTE: variables declared in a constructor are not accessible outsite the costructutor #include<iostream> using namespace std; class Letswalk{ private: int steps; int distance; int NoOfsteps; public: Letswalk(int go,int km,int noOfsteps) { steps=go; distance=km; NoOfsteps=noOfsteps; } void Calculate(){ cout<<"Enter the distance you walked in kilometres:"; cin>>distance; // changed here km to distance NoOfsteps=distance/steps; // changed } void LetsSee(){ cout<<"\nYou walked "<<NoOfsteps<<" in a diatance of"<<distance<<endl; // change from } }; int main() { Letswalk Check(2,2,1000); // changed from "Letswalk Check()" Check.Calculate(); Check.LetsSee(); }
12th Nov 2020, 7:02 PM
Sacalivin Obiri
Sacalivin Obiri - avatar
0
Thanks i finally get the problem now ... I guess i need some practice with constructors and more oessons on classes and objects
20th Nov 2020, 3:56 PM
Michael Kamau
Michael Kamau - avatar