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

virtual pure functions

P: 2
I have a very simple question:
what is the purpose of using a pure function?
Which leads me to ask, what is the purpose of using an abstract class?
Mar 15 '07 #1
Share this Question
Share on Google+
2 Replies

Expert 2.5K+
P: 3,652
I assume you mean a pure virtual function?

Suppose you have a group of classes that all share some common attribute. For instance, in a bank, you have Tellers, Accountants, Janitors, etc. Now, each of these employees gets paid - but each gets paid a different amount. When you write their checks, you don't care what job they do, you just want to find out how much they get paid. All that matters to you is that they each have a way of getting paid.

So Employee is like an abstract class. In Employee, you have the pure virtual function payAmount. You don't provide the definition of payAmount, since it makes no sense that a simple Employee can get paid. Every Employee should be some more specific job - a Janitor, a Teller, or an Accountant. But each of these is an Employee, so they all must have the payAMount method.

Now, when you are calculating their pay, these people are passed as arguments - but since you don't know what job they do, they are all passed under Employee. Since you know all Employees have a payAmount method, you can call this method without worries - the compiler will determine what each Employee actually is and use that method.

In short, a pure virtual function is used when you know all subclasses should have this method, but you are unsure of how to define it generally for each subclass. An abstract class is used to define some abstract concept (such as Employee) that many concrete things have in common.

I hope that's a good explanation!
Mar 15 '07 #2

P: 2
Yes, that cleared it up quite a bit.
Thanks alot :D
Mar 15 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.