Ah, This reminds me of your earlier post yesterday which I didn't have time to reply to.
This will help out others who wish to reply as well.
I would recommend you go back to that code, there is no need to make everything public or use protected inheritance.
With lines 15 to 17 those variables are private because no access was specified, in other words they are private by default. That means that they are only available to member functions of that class only. The same thing is happening in your original code.
Private variables are a good thing, it is very tempting to make them protected, but that is bad practice, because if one needs to change something it can break existing code .
The thing to do, is make use of initialiser lists in your constructors, derived classes can call base class constructors from their initialiser list too.
in the
Member initialization in constructors section
When you have public functions that are inherited, you shouldn't redefine them in derived classes. If you want different behaviour for them, make them virtual. Not necessarily pure virtual, that is for when you need your function to be different in every derived class. Ordinary virtual means you can define a function once, and it applies to every derived class underneath, unless you provide a new definition.
Btw don't have
using namespace std;
, it defeats the purpose of having namespaces by bringing in the entire std ns into the global namespace, causing naming conflicts. There are lots of words one might use for variables & functions that are already in std, such as
std::count
,
std::left
,
std::right
,
std::next
,
std::distance
etc. Rather than worry about whether a variable is already in std, just put
std::
before each std thing as in
std::cout
,
std::string
etc.
Ideally, one should put their own stuff into their own namespace, like this:
In the hesder file:
1 2 3 4 5 6 7
|
namespace BANK {
class Bankaccount {
}
} // end of BANK ns
|
In the .cpp file:
1 2 3 4 5
|
namespace BANK {
Bankaccount::Bankaccount() {}//ctor
} // end of BANK ns
|
When you come to use it, you need to put the namespace and class in, like this:
BANK::Bankaccount MyBank(); // creates a Bankaccount object
Namespaces can be nested, and one can use a
using
statement to provide an alias for it to shorten up references to them:
1 2
|
using MyNameSpace = mns;
using MyNameSpace::MyNestedNameSpace = mnns;
|
If you look at third party libraries like boost , they do this all the time.
Hope all goes well :+)