473,406 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

virtual pure functions

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
2 1422
Ganon11
3,652 Expert 2GB
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
Yes, that cleared it up quite a bit.
Thanks alot :D
Mar 15 '07 #3

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

Similar topics

62
by: christopher diggins | last post by:
Since nobody responded to my earlier post , I thought I would try to explain what I am doing a bit differently. When multiply inheriting pure virtual (abstract) base classes, a class obviously...
39
by: Ele | last post by:
Is it correct to say that Whenever a class has a virtual member function, define its destructor as "virtual"? Can a destructor as "pure virtual"? When is it needed to do so? For an interface,...
11
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining...
37
by: WittyGuy | last post by:
Hi, I wonder the necessity of constructor and destructor in a Abstract Class? Is it really needed? ? Wg http://www.gotw.ca/resources/clcm.htm for info about ]
12
by: placid | last post by:
Hi if a have the following classes class A { public: A(); virtual ~A(); virtual string someFunction() const = 0;
10
by: mark | last post by:
I have this class: class Selections { OSStatus Init(); protected: CFMutableSetRef selectionObjects; static void CFASelectionsApplier(const void* value, void* ctx); OSType ready; public:...
10
by: John Goche | last post by:
Hello, page 202 of Symbian OS Explained by Jo Stichbury states "All virtual functions, public, protected or private, should be exported" then page 203 states "In the rare cases where a...
14
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
7
by: sam_cit | last post by:
Hi Everyone, I wanted to know as to what is the exact difference between a virtual function and a pure virtual function? Thanks in advance!!!
17
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.