+ 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
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
+ 1
Change km in Calculate() and LetsSee() to distance.
Add a default constructor in LetsWalk. Otherwise pass arguments to the LetsWalk object.
+ 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();
}
0
Thanks i finally get the problem now ... I guess i need some practice with constructors and more oessons on classes and objects



