Class Inheritance

Oct 16, 2015 at 11:40pm
From what I have and read, a derived class can access all of the non-private members of its base class. My newest assignment works with class derivatives and operator overloading. the assignment states that the derived class inherits all members of the base function. Is this even possible? I would like to know before I question the teacher.....
Oct 17, 2015 at 12:02am
It can see the assignment operator of the base class, the derived class has its own which is different.
Oct 17, 2015 at 12:27am
but if some members are private, such as variables, how does the derived class access them?
Oct 17, 2015 at 12:34am
ok, after reading a little further, I am allowed to add functions to the base class to allow the derived class to access private data......
Oct 17, 2015 at 12:49am
Does it even make sense to combine operator overloading with polymorphism? In a majority of cases, it does not, so I generally say you should pick one or the other. Trying to mix operator overloading with polymorphism is usually a nightmare and a half because C++ isn't designed to support it.
Oct 18, 2015 at 11:32pm
I'm still confused. I like the idea of polymorphism but lets say, I have a bankaccount class and checkingaccount class that is derived from bankaccount. All of the info, such as accountname, balance, accountnumber, etc...is private. How does the private data, such as balance get assigned to the checkingaccount class?
Oct 18, 2015 at 11:39pm
What is functionally different between a 'bank account' and a 'checking account'? What does 'checking account' add that should not be in 'bank account'? In my opinion, there is no distinction to be made and so you should not need to separate the concepts into two different classes.
Last edited on Oct 18, 2015 at 11:40pm
Oct 18, 2015 at 11:46pm
How does the private data, such as balance get assigned to the checkingaccount class?
It doesn't.

Think of a class as a structure that occupies memory. It always has a size. A derived class will add to that size.

For example:
1
2
3
4
5
6
7
class B {
    char code[4];
};

class D : public B {
    double price;
};

If the size of a double is 8 bytes, the size of B is 4 bytes and the size of D is 12 bytes.

As B::code is private, it is not visible to D. But D carries around B::code wherever it's instantiated. It just doesn't have access to it.
Oct 19, 2015 at 12:14am
As B::code is private, it is not visible to D. But D carries around B::code wherever it's instantiated. It just doesn't have access to it.

ok, that makes sense. I guess I am making it harder than it needs to be.
Topic archived. No new replies allowed.