With code tags, this:
[code]
int Address::getZip(){
int zip;
return zip;
}
[/code]
Becomes this:
1 2 3 4
int Address::getZip(){
int zip;
return zip;
}
Much easier to read.
Anyway, in this code snippit, you are declaring a variable named 'zip' on line 2. This will create a new variable with that name. Note that this means you now have two seperate variables with the same name: The one that's part of the class, and the one that you just created which is local to this function.
Note that the local zip is uninitialized and so it will contain garbage.
When you return zip you are not returning the 'zip' that is part of the class, but instead are returning the local zip. You want to return the class's zip.
The solution here is to not declare a new variable in this function. You do not need or want it.
#include <iostream>
usingnamespace std;
class Address{
private:
string streetName, city, state;
int zip, streetNumber;
public:
void setStreetNumber(int);
void setZip(int);
void setNames(string, string, string );
int getStreetNumber();
int getZip();
char getStreetName(); // <- these 3 functions all return 'char' when you probably
char getCity(); // want them to return a string
char getState();
};
void Address::setStreetNumber(int a){
streetNumber = a;
}
void Address::setZip(int a){
zip = a;
}
void Address::setNames( string sn, string s, string c){
streetName = sn;
state = s ;
city = c ;
}
int getStreetNumber(){ // <- This is a global function named 'getStreetNumber', and is not
return streetNumber; // part of your Address class. (Notice, you are missing the :: operator to
} // make it part of the class)
int Address::getZip(){
return zip;
}
string Address::getStreetName(){
return streetName;
}
string Address::getCity(){
return city;
}
string Address::getState(){
return state;
}