I'm experimenting on classes and constructors and i'm trying to choose a specific declaration for a class object depending on something like an if-statement. I've written a simple example showing what i tried to do, which is not working. Even though it fulfills the if-statement, it prints the "id" of the first declared object and if i don't declare it before the if statement i get the error "a not declared in this scope" for the print. Is there a way to re-declare a class object and use it afterward through an if-statement?
class potato{
private:
int id;
public:
void get_id(){
cout<<id<<endl;
}
potato(int x){
id=x;
}
};
int main(){
int i=9;
potato a(i);
if(i==3){
potato a(5);
}
else
potato a(3);
a.get_id();
}