标题:作用域问题?
只看楼主
liudw2
Rank: 4
等 级:业余侠客
帖 子:85
专家分:248
注 册:2011-7-31
结帖率:100%
 问题点数:0 回复次数:1 
作用域问题?
#include <iostream>
#include <vector>
using namespace std;

class stack{
    public:
    stack();
    stack(int);
    void opp();

    private:
    int num;
};

stack::stack()
{
    num = 120;
}

stack::stack(int new_num)
{
    num = new_num;
}
void stack::opp()
{
    vector<int> s;
    int i = 2;
    while (i != num)
    {
        bool on = true;//
        for (int j = 2; j != i; ++j)
        {
            if (i % j == 0)
            {
                on = false;
                break;
            }
        }
        if (on)
        {
            s.push_back(i);
        }
        i++;
    }
    for (int k = s.size() - 1; k >= 0; --k)
    cout << s[k] << " ";
}
int main()
{
    stack f;
    f.opp();
    cout << "\n";
    cout << "Please enter an integer:";
    int a;
    cin >> a;
    stack k(a);
    k.opp();
    return 0;
}

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

class stack{
    public:
    stack();
    stack(int);
    void opp();

    private:
    int num;
};

stack::stack()
{
    num = 120;
}

stack::stack(int new_num)
{
    num = new_num;
}
void stack::opp()
{
    vector<int> s;
    int i = 2;
    bool on = true;
    while (i != num)
    {
        for (int j = 2; j != i; ++j)
        {
            if (i % j == 0)
            {
                on = false;
                break;
            }
        }
        if (on)
        {
            s.push_back(i);
        }
        i++;
    }
    for (int k = s.size() - 1; k >= 0; --k)
    cout << s[k] << " ";
}
int main()
{
    stack f;
    f.opp();
    cout << "\n";
    cout << "Please enter an integer:";
    int a;
    cin >> a;
    stack k(a);
    k.opp();
    return 0;
}所得的结果不同,这是为什么,放在while里面的变量跟放在外面的有什么区别?
搜索更多相关主题的帖子: private include public false 
2012-06-26 22:02
liudw2
Rank: 4
等 级:业余侠客
帖 子:85
专家分:248
注 册:2011-7-31
得分:0 
第一个是输出:
113 109。。。3 2
Please enter an integer:
第二个输出是:
空白行
Please enter an integer:
2012-06-26 22:04



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-372292-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.108473 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved