Inheritance

W

Wilson

Hi,

Again quite a simple question i know. I am new to OO programming and
want to understand fully inheritance. I have been told that it could
be used to greatly improve an accounting program i was working on by
using a class account and let other classes inherit from it (e.g
checking, savings e.t.c). i cannot see how this improves the program
at all. Please advise as i am having trouble understanding
inheritance. thanks

wilson
 
J

John Harrison

Wilson said:
Hi,

Again quite a simple question i know. I am new to OO programming and
want to understand fully inheritance. I have been told that it could
be used to greatly improve an accounting program i was working on by
using a class account and let other classes inherit from it (e.g
checking, savings e.t.c). i cannot see how this improves the program
at all. Please advise as i am having trouble understanding
inheritance. thanks

wilson

Presumably all accounts have something in common (e.g. all have a
balance) but at the same time they have differences (maybe you can only
go overdrawn on a checking account).

The idea of having an account class and deriving a checking_account and
savings_account from it is that you can put all the things that are in
common in the base account class, and all the things that are different
in the derived checking_account and savings_account classes.

But it's not enough to use just inheritance. In order to fully exploit
inheritnace you need to know about virtual functions. Using virtual
functions allows you to manipulate accounts without knowing exactly what
kind of account you have.

All of this will be in any decent book on C++. I suggest you get
yourself one. It's a big subject and not something that can be
adequately explained in a usenet posting.

john
 
J

jalina

Wilson a écrit :
Hi,

Again quite a simple question i know. I am new to OO programming and
want to understand fully inheritance. I have been told that it could
be used to greatly improve an accounting program i was working on by
using a class account and let other classes inherit from it (e.g
checking, savings e.t.c). i cannot see how this improves the program
at all. Please advise as i am having trouble understanding
inheritance. thanks

wilson
I have made an accounting program (in java but I hope it is ok) and have
an inheritance but on transactions Here is the principle (do not try and
compile, it won't, just for demo purpose)

class TransactionBase {
// Base transaction for all transaction
double amount;
date transactionDate;
}

// A payent is a transaction
class Payment: public Transaction {
Payee thePayee; // this is only for payement transactions
// Payment class keep amount and transactionDate
// from base class.
}

// Another kind of transaction
class Transfer: public Transaction {
Account toAccount; // this is specific to this
// kind of transaction. Still have
// an amount and a transactionDate
// (inherited from TransactionBase)
}


HTH
J.
 

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

Forum statistics

Threads
474,159
Messages
2,570,885
Members
47,419
Latest member
ArturoBres

Latest Threads

Top