C++ 如何使用派生成员函数修改基类中的私有变量?
我的全部问题,因为我不能把它全部写在标题上:2如何用派生类B成员函数修改基类中的私有变量,该函数将调用在基类a中被重写的成员函数,该函数也将修改派生类B中的不同私有成员变量。我真的很难尝试因为我不知道如何修改来自不同类的两个成员函数中不同类中的两个私有成员变量。到目前为止,这就是我的代码。我将在代码中对我试图解决的问题发表评论 在我的.h文件中,我有:C++ 如何使用派生成员函数修改基类中的私有变量?,c++,inheritance,C++,Inheritance,我的全部问题,因为我不能把它全部写在标题上:2如何用派生类B成员函数修改基类中的私有变量,该函数将调用在基类a中被重写的成员函数,该函数也将修改派生类B中的不同私有成员变量。我真的很难尝试因为我不知道如何修改来自不同类的两个成员函数中不同类中的两个私有成员变量。到目前为止,这就是我的代码。我将在代码中对我试图解决的问题发表评论 在我的.h文件中,我有: #include <iostream> #include <string> using namespace std;
#include <iostream>
#include <string>
using namespace std;
class A{
public:
A();
A(string name_holder, double balance_holder);
int take(float amount);
string getName();
double getBalance();
void output();
private:
string name;
double balance;
};
class B:public A{
public:
B();
B(int number_of_withdrawal_holder);
int take(float amount);//overriding
void output();
private:
static int count;
};
#包括
#包括
使用名称空间std;
甲级{
公众:
A();
A(字符串名称\u持有人、双余额\u持有人);
整数(浮动金额);
字符串getName();
双getBalance();
无效输出();
私人:
字符串名;
双平衡;
};
B类:公共A{
公众:
B();
B(提款持有人的国际编号);
int take(浮动量);//重写
无效输出();
私人:
静态整数计数;
};
在我的.cpp文件中
#include "Header.h"
A::A()
{
name=" ";
balance=0;
}
A::A(string name_holder,double balance_holder)
{
name=name_holder;
balance=balance_holder;
}
int A::take(float amount)
{
if (balance >= amount && amount>=0)//if the amount is less than or equal to the balance and is nonnegative
{
balance-=amount;
cout<<"ok"<<endl;
return 0;
}
cout <<"Insufficient funds for the withdrawal to take place"<<endl;
return 1;
}
string A::getName()
{
return name;
}
double A::getBalance()
{
return balance;
}
void A::output()
{
cout<<"Name: "<<getName()<<endl;
cout<<"Balance: "<<getBalance()<<endl;
}
int B::count=0;
B::B()
{
count=0;
}
B::B(int number_of_withdrawal_holder)
{
count=number_of_withdrawal_holder;
}
int B::take(float amount)
{
if(count==0)//if this is the first withdrawal
{
++count;
return BankAccount::take(amount);
}
//the bottom part of the code gets excuted if you already withdrew atleast once-you pay a fee of 1.50(in other words i want to do a function call to the first withdraw function and subtract 1.50 from the member variable balance but I also want to modify count in the base class function take
++number_of_withdrawals;
return BankAccount::withdraw(amount);
}
#包括“Header.h”
A::A()
{
name=“”;
余额=0;
}
A::A(字符串名称\u持有者,双余额\u持有者)
{
姓名=持有人姓名;
余额=余额持有人;
}
int A::take(浮动金额)
{
if(余额>=金额&&amount>=0)//如果金额小于或等于余额且为非负
{
余额-=金额;
cout移动到一个答案:
我认为你做得不对。你的基类是一个帐户,它为你提供了一个特定帐户的交易接口,因此你不应该修改基类的任何内部内容,而应该修改公共接口提供的内容,因此你的“费用”也是一个取款操作
正如注释中真正提到的,A::take()应该声明为虚拟的
class A {
...
virtual int take(float amount);
...
}
class B: public A {
B() {
m_nNumberOfWithdraw = 0;
}
B::B(int number_of_withdrawal_holder) {
m_nNumberOfWithdraw =number_of_withdrawal_holder;
}
int B::take(float amount) {
int nResult = A::take(m_nNumberOfWithdraw > 0 ? amount + 1.50 :amount);
if (nResult == 0)
m_nNumberOfWithdraw++;
return nResult;
}
protected:
int m_nNumberOfWithdraw;
};
private
成员甚至对派生子类也是私有的。如果您想允许派生子类访问成员,请将其设置为protected
,而不是private
。这样的成员仍然不是公共的,但可以由子类访问。我们在这里具体讨论的是什么函数、成员等?我想这是很简单的很难看到…int take(float amount);//重写
因为A::take
不是虚拟的,B::take
实际上并没有重写它,而是隐藏它。还有-什么是BankAccount
?(例如,您有BankAccount::setName的定义,但没有声明)。您的代码没有显示B
的成员试图访问或修改A
的私人成员。具体问题是什么?请重新表述开头的段落或将其缩短。事实上,这很难理解。尽管我不确定这是否真的是您的问题。