从类继承时,继承的类是否会通过父类重新定义继承的变量

When inheriting from a class, do the variables inherited get redefined by the inherited class through the parent class?

本文关键字:继承 新定义 变量 定义 是否 父类      更新时间:2023-10-16

我创建了两个类:stack1stack2,并定义了我自己的堆栈操作push((、pop((,isempty((和isfull((。我正试图根据输入计算一个后缀表达式。我创建了另一个名为operation的类,它是stack1stack2中的子类,因此我可以访问操作内的push((、pop((等函数。我在操作中有一个函数,名为operate((,它在堆栈上做脏活。在这个函数中,我有一个while循环,它依赖于stack1在操作完成之前不为空;然而,当我逐步执行此函数时,stack1所指向的top1已重置为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;
} };

我认为"问题"出在以下行:

stack1 st1;
stack2 st2;

这将调用默认构造函数,并将变量top1top2的值设置为零。解决方法是用一些正的非零值初始化这些变量。

因此,代码看起来像(只关注更改的部分(:

class stack1 {
private:
int num1[SIZE/2];       int top1;
public:
.....
stack1()
{
top1 = 0;           num1[SIZE/2] = {0};
} 
stack1(int top1)
{this.top1 = top1;}
};
class stack2 {
private:
int num2[SIZE/2];       int top2;   public:
public:
.....
stack2()
{
top2 = 0;           num2[SIZE/2] = {0};
} 
stack2(int top2)
{this.top2 = top2;}
};
class operation: public stack2, public stack1 {
.....
public:
int c;
int oper(void)
{
.....
//just an example, can be declared explicitly as well
stack1 st1(5);
stack2 st2(7);
.....

此外,我建议您使代码更可读(例如,在仅跨越一行的if情况下,存在3行{}(。这只是不必要的空间消耗。

最后,如果父类变量的"重新定义"意味着变量在被继承时重新断言某些值,那么它们不会,除非您明确指定(例如在我们的情况下使用构造函数为top1top2分配不同的值(。继承的类将获得父类*的状态的副本。

*这意味着它们不能被继承的类直接更改。例如,如果你最初做了:int top1=5;,那么top1应该是5,直到它在某个地方再次被重新定义(比如使用构造函数(

我从您试图用四个运算符构建一个基本计算器的问题语句中获得,并且需要使用堆栈进行基本表达式求值。假设你有一个表达式:a+b-cd/e堆栈中如下所示:顶部e->/->d->->c->-->b->+->a空这条赛道需要评估。

基于这些。。你可能正在寻找下面这样的东西:

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((代码中,您有一个有效的表达式输入到堆栈中。