Simple question...

I'll just ask a simple question... Whats the point of declaring variables after my main() when you already have it in the class private section?
I already tried removing the variables after the main but it says error...

#include <iostream>
#include <string>
using namespace std;

class ParkingLot
{
public:
void setPlate(string a)
{
plate = a;
}
void setPark(int b)
{
park = b;
}
void setPay1 (int c)
{
pay1 = c;
}
void setPay2 (int d)
{
pay2 = d;
}
void setHours (int e)
{
hours = e;
}
string getPlate()
{
return plate;
}
int getPark()
{
return park;
}
int getPay1()
{
return pay1;
}
int getPay2()
{
return pay2;
}
int getHours()
{
return hours;
}
private:
string plate;
int park;
int pay1,pay2; //Whats the use of this?
int hours;
};

int main()
{
for(;;)
{
ParkingLot Arevalo;
string plate;
int park,pay1,pay2; //When you have this??
char command;


cout<<"Welcome to the Parking Lot!!"<<endl;
cout<<"-----------------------------"<<endl;
cout<<"Parking Lot Fee(s)"<<endl;
cout<<"First 3 hours = P50/hr"<<endl;
cout<<"Exceeding hours = P70/hr"<<endl;
cout<<"-----------------------------"<<endl;
cout<<"Please enter your Plate # (***-***): ";
cin>>plate;
cout<<"How long are you gonna park? (hours): ";
cin>>park;
cout<<"-----------------------------"<<endl;
Arevalo.setPlate(plate);
Arevalo.setPay1(50*park);
Arevalo.setPay2(70*park);
Arevalo.setPark(park);

cout<<"Your Plate # is "<<Arevalo.getPlate()<<endl;

if (park<=3)
{
cout<<"You will pay: "<<Arevalo.getPay1()<<" Php"<<endl;
}
else if (park>3)
{
cout<<"You will pay: "<<Arevalo.getPay2()<<" Php"<<endl;
}

cout<<"-----------------------------"<<endl;
cout<<"Would you like to park again?(y/n): ";
cin>>command;

if (command == 'n')
{
break;
}
}
cout<<"Thank you for parking with us!"<<endl;

system("PAUSE");
return 0;
}
From the compiler's perspective those variables are unrelated. In main you use the variables plate and park to store the input from the user before passing them on to the class functions. You can name these variables something completely different if you want because they are different variables than the ones inside the class. You don't need pay1 and pay2 in main because they are never used
Last edited on
This can be a bad thing to do. You can do it, but when you have multiple entities with the exact same name, it can become challenging to read the code as you can become tangled up as to which one is which, esp in larger code blocks and complex designs.

There is a time and a place for it -- for example, it is nice if 2 classes that have a member that represents the same thing might have the same name, if you are careful about how you do it. Namespaces can help with this, to explicitly differentiate them in shared code and removal of that with a using statement in unshared areas.

Its a double edged sword ... it can help make your code better, or it can make it a mess. Naming things in code is one of the most critical ways to make your code really good, following the self documenting principles where each item tells the reader a lot of info is so very helpful for large projects.

Topic archived. No new replies allowed.