Object Scope

Hi I wrote a program to find the largest palindrome that is product of two three digit numbers. The program works correctly, but I'm baffled by something...the program only works if I instantiate the Palindrome object p inside the for loop...if I move the object outside the loop and try to call its method p.isPalindrome() inside the loop...the isPalindrome(int) breaks?

i'm trying to learn C++ after years of Java and am at a loss by this.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include <stack>


class Palindrome{

public:
Palindrome();

bool isPalindrome(  int n);

private:
std::stack<int> s;
int dig;
int num;
};

Palindrome::Palindrome(){;    }




bool Palindrome::isPalindrome(  int num){
  int num2 =num;
    while (num >0){
        //get digit store in stack
        dig = num % 10;
        s.push(num % 10);
        num /=10;
    }



// pop stack, check if equals digits
for(int i = 0;i < (int) s.size(); i++){
    dig = num2 % 10;
    if(dig !=  s.top()){
        return false;
        }
        num2 /=10;
        s.pop();
    }

return true;
}





using namespace std;


int main ()
{
 int num1= 10;
 int num2= 10;
int product=  9009;
int largestP =1;

int start =100;
int end = 1000;


//find largest palindrome that is product of num1 and num2
for(num1 =start;num1< end ;num1++)
    for(num2 =start; num2 < end ; num2++)
    {
    product = num1  * num2;
//WHY DO I HAVE TO PUT THE NEXT LINE INSIDE THIS LOOP?
    Palindrome  p;

 if( p.isPalindrome( product) == 1  && product> largestP ){
        largestP = product;
        }
    }
cout<< "here: " << largestP <<endl;

return 0;
}
You need an object to access a class method that isn't declared static in C++. However, you can only declare a function static if it only accesses static variables.

In your case you can't just add public static whatever like you do in Java.
I'm not exactly sure what you're asking, but you don't have to put that line in the loop. Objects have the same scope as any other variable, so you can place it/instantiate the class anywhere before you use the object.

eker676 is right, if you are asking why you need to instantiate the class.
thanks for the responses and sorry if the question was unclear. I see you're right, Zhuge, as I just ran a little experiment with a different class using loops and objects.

for some reason though, the code up above does not work properly . if I move the Palindrome p instantiation up above the loop

ie the following change breaks the program and largestP at the loop's end stores 1
1
2
3
4
5
6
7
8
9
10
11
12
 Palindrome  p;

for(num1 =start;num1< end ;num1++)
    for(num2 =start; num2 < end ; num2++)
    {
    product = num1  * num2;


 if( p.isPalindrome( product) == 1  && product> largestP ){
        largestP = product;

        }



...but now, i see that whatever is behind that discrepancy does not have to do with objects or with their scope.
Last edited on
The problem is that isPalindrome should really be a function, with s, dig, num declared as local variables, not a method of a class. By making them external to the function, you've given them a lifetime of their own.

The problem isn't one of scope as such, it's just that when p is declared within the loop, it's destroyed each time, clearing down the stack s.

When you declare p at a higher scope, you don't start with an empty stack, but whatever state it was left in on the previous call.
thanks kbw... i see that you are obviously correct!
Topic archived. No new replies allowed.