首页 > 解决方案 > 从类继承时,继承的变量是否通过父类被继承的类重新定义?

问题描述

我创建了两个类:stack1stack2,并定义了我自己的 push()、pop()、isempty() 和 isfull() 的堆栈操作。我正在尝试从输入中计算后缀表达式。我创建了另一个名为operation的类,它是stack1stack2的子类,因此我可以在operation中访问 push()、pop() 等函数。我在操作中有一个名为operate() 的函数,它在堆栈上执行脏活。在这个函数中,我有一个 while 循环,它依赖于stack1在操作完成之前不为空;但是,当我单步执行此函数 top1 时,其中stack1指向,已重置为0。有没有办法克服这个/我做错了什么?这是我第一次使用类等,所以我不确定来龙去脉。

以下是我的类的定义:

class stack1 {
    private:
        int num1[SIZE/2];       int top1;
    public:
        void push1(int data)
        {
            if (is_full1());
            else
            {
                num1[top1] = data;
                top1++;
            }
        }
        int pop1(void)
        {
            if(is_empty1());
            else
            {
                top1--;
                return num1[top1];
            }
        }
        int is_empty1(void)
        {
            if(top1 == 0)
            {
                return 1;
            }else
            {
                return 0;
            }
        }
        int is_full1(void)
        {
            if(top1 == SIZE)
            {
                return 1;
            }else
            {
                return 0;
            }
        }

        stack1()
        {
            top1 = 0;           num1[SIZE/2] = {0};
        } };

class stack2 {
    private:
        int num2[SIZE/2];       int top2;   public:
        void push2(int data)
        {
            if (is_full2());
            else
            {
                num2[top2] = data;
                top2++;
            }
        }
        int pop2(void)
        {
            if(is_empty2());
            else
            {
                top2--;
            return num2[top2];
            }
        }
        int is_empty2(void)
        {
            if(top2 == 0)
            {
                return 1;
            }else
            {
                return 0;
            }
        }
        int is_full2(void)
        {
            if(top2 == SIZE)
            {
                return 1;
            }else
            {
                return 0;
            }
        }

        stack2()
        {
            top2 = 0;           num2[SIZE/2] = {0};
        } };

class operation: public stack2, public stack1 {
    private:
        int answer;
        int a;
        int b;
        int num_cnt;
        int ans;
        int from_st1;
        int from_st2;
    public:
        int c;
        int oper(void)
        {
            answer = 0;
            a = 0;
            b = 0;
            num_cnt = 0;
            ans = 0;
            c = 0;
            stack1 st1;
            stack2 st2;
            while(!st1.is_empty1())
            {
                from_st1 = st1.pop1();
                if(from_st1 == plus)
                {
                    st2.push2(from_st1);
                }else if(from_st1 == minus)
                {
                    st2.push2(from_st1);
                }else if(from_st1 == mult)
                {
                    st2.push2(from_st1);
                }else if (from_st1 == divide)
                {
                    st2.push2(from_st1);
                }else if(num_cnt == 1)
                {
                    num_cnt = 0;
                    if(ans == 0)
                    {
                        answer = b;
                        ans++;
                    }
                    a = from_st1;
                    from_st2 = st2.pop2();
                    if(from_st2 == plus)
                    {
                        c = a+answer;
                    }else if(from_st2 == minus)
                    {
                        c = a-answer;
                    }else if(from_st2 == mult)
                    {
                        c = a*answer;
                    }else if(from_st2 == divide)
                    {
                        c = a/answer;
                    }
                }else
                {
                    b = from_st1;
                }
                num_cnt++;
            }
        return c;
        }

        operation()
        {
            answer = 0;
            a = 0;
            b = 0;
            num_cnt = 0;
            ans = 0;
            from_st1 = 0;
            from_st2 = 0;
        } };

标签: c++classinheritance

解决方案


我从问题陈述中得出您正在尝试使用四个运算符构建一个基本计算器,并且需要使用堆栈完成基本表达式评估。假设您有一个表达式: a+bc d/e 在堆栈中看起来像这样: TOP e->/->d-> ->c->-->b->+->a EMPTY 并且这条轨道需要评估。

基于这些..您可能正在寻找如下内容:

class stack {
    private:
        int num[SIZE];       
        int top;
    public:
        void push(int data)
        {
            if (is_full1());
            else
            {
                num[top] = data;
                top++;
            }
        }
        int pop(void)
        {
            if(is_empty());
            else
            {
                top--;
                return num[top];
            }
        }
        int is_empty(void)
        {
            if(top == 0)
            {
                return 1;
            }else
            {
                return 0;
            }
        }
        int is_full(void)
        {
            if(top == SIZE)
            {
                return 1;
            }else
            {
                return 0;
            }
        }

        stack()
        {
            top = 0;           num[SIZE] = {0};
        } 
};

class operation {
    private:
        int answer;
        int op1;
        int op;

    boolean isOperator(int val) {
        boolen retVal = false;;

        if (val == plus ||
            val == minus ||
            val == divide ||
            val == mult) {
                retVal = true;
            } 
        else {
            retVal = false;
        }
        return retVal;
    }
    public:
        int oper(stack st1)
        {
            int from_st1 = 0;
            while(!st1.is_empty())
            {
                from_st1 = st1.pop();
                if(isOperator(from_st1))
                {
                    op = from_st1;
                }
                else if(answer != 0)
                {
                    op1 = from_st1;
                    if(op == plus)
                    {
                        answer = op1 + answer;
                    }else if(op == minus)
                    {
                        answer = op1 - answer;
                    }else if(op == mult)
                    {
                        answer = op1 * answer;
                    }else if(op == divide)
                    {
                        answer = op1 / answer;
                    }
                }
                else
                {
                    answer = from_st1;
                }
            }
        return answer;
        }

        operation()
        {
            answer = 0;
            op1 = 0;
            op = 0;
        } 
};

注意:您可以使用一个堆栈进行所有评估,不需要两个堆栈。对于此分配,您的操作数不能等于 +、-、* 和 / 的整数值。您在 main() 代码中的堆栈中有一个有效的表达式输入。


推荐阅读