470,571 Members | 2,440 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,571 developers. It's quick & easy.

pure virtual member functions

Hi Everyone,

It seems that pure virtual member functions can be defined in the
abstract base class, but what is the need (or) use to do so? and how
is it done?

Thanks in advance!!!
Dec 1 '07 #1
2 1074
Rahul <sa*****@yahoo.co.inwrote:
It seems that pure virtual member functions can be defined in the
abstract base class, but what is the need (or) use to do so? and how
is it done?
Always check the FAQ first:
http://www.parashift.com/c++-faq-lite/abcs.html
Dec 1 '07 #2
On Dec 1, 12:08 pm, Rahul <sam_...@yahoo.co.inwrote:
Hi Everyone,

It seems that pure virtual member functions can be defined in the
abstract base class, but what is the need (or) use to do so? and how
is it done?

Thanks in advance!!!

Why not define a pure-virtual member function if thats what the
hierarchy requires?
C++ does not impose the rule that an abstract class is an 'interface'
with no implementation. Neither does the language prevent you from
indeed choosing that route.
You can implement that member function where its needed in those
special cases where providing a definition in the abstract base fits
the requirement.

#include <iostream>

class abstract
{
public:
virtual void foo() = 0;
};

void abstract::foo()
{
std::cout << "abstract::foo()\n";
}

class concrete : public abstract
{
public:
void foo()
{
abstract::foo();
std::cout << "concrete::foo()\n";
}
};

int main()
{
concrete instance;
instance.foo();
}

/*
abstract::foo()
concrete::foo()
*/

Now consider what would happen if you needed to design a class
'derived' that also inherits from abstract (assume that deriving from
concrete would not fit the requirements for whatever reason). Consider
what would happen if
std::cout << "abstract::foo()\n";
where say... 60 lines of code that you need to maintain. All of a
sudden, its far from being silly.

Dec 2 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by santosh | last post: by
37 posts views Thread by WittyGuy | last post: by
10 posts views Thread by Martin Vorbrodt | last post: by
6 posts views Thread by pakis | last post: by
21 posts views Thread by sks | last post: by
7 posts views Thread by sam_cit | last post: by
10 posts views Thread by Rahul | last post: by
13 posts views Thread by Mike -- Email Ignored | last post: by
1 post views Thread by livre | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.