By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,332 Members | 1,404 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,332 IT Pros & Developers. It's quick & easy.

Inheritance between three classes

P: n/a
Hi, a very simple question. I am trying to understand inheritance
using c++ and dont cee how i could use three classes to create an
accounting program using inheritance. e.g one class containing members
for both, and then a class for checking accounts and a class for
savings. Finally could you just clarify that this is the correct use
of i nheritance because i am thoroughly confused.

Feb 25 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"Wilson" <tp****@googlemail.comwrote in message
news:11**********************@q2g2000cwa.googlegro ups.com...
Hi, a very simple question. I am trying to understand inheritance
using c++ and dont cee how i could use three classes to create an
accounting program using inheritance. e.g one class containing members
for both, and then a class for checking accounts and a class for
savings. Finally could you just clarify that this is the correct use
of i nheritance because i am thoroughly confused.
Well, you would have a base class such as account. It would have common
variables for both (balance) as well as methods (withdraw, deposit). The
derived classes would maybe be Checking and Savings. Then you could have a
class that could store accounts.

It could be done that way.
Feb 25 '07 #2

P: n/a

Wilson wrote:
>
Hi, a very simple question. I am trying to understand inheritance
using c++ and dont cee how i could use three classes to create an
accounting program using inheritance. e.g one class containing members
for both, and then a class for checking accounts and a class for
savings. Finally could you just clarify that this is the correct use
of i nheritance because i am thoroughly confused.
1. What C++ book are you using whithout description how make inherited
classes?

2. Inheritance is one of the way to create new classes from existing one.
Other way is composition. Other way is explicit repeat of previous
declaration.

In C++ best case to use inheritance is "inheritance of interface".
"Inheritance of interface" is only way to make run-time templates (functions
are working with unknown at compile time derived classes). If it is possible
that it is better to use composition insted of inheritance due to
composition makes classes more independent and it is easy to maintain
program.

class A
{
public:
int i;
};

//inheritance
class B: public A { };

//composition
class C
{
public:
A a;
};

//explicit redeclaration
class D
{
public:
int i;
};

--
Maksim A. Polyanin
http://grizlyk1.narod.ru/cpp_new

"In thi world of fairy tales rolls are liked olso"
/Gnume/
Feb 25 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.