Inheritance between three classes

W

Wilson

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.
 
J

Jim Langston

Wilson said:
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.
 
G

Grizlyk

Wilson said:
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/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,160
Messages
2,570,889
Members
47,420
Latest member
ZitaVos505

Latest Threads

Top