472,337 Members | 1,479 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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 1147
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

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...
37
by: WittyGuy | last post by:
Hi, I wonder the necessity of constructor and destructor in a Abstract Class? Is it really needed? ? Wg ...
10
by: PengYu.UT | last post by:
Hi, A pure function is called in the base function constructor. It generate a run time error: "pure virtual method called". My problem is that...
10
by: Martin Vorbrodt | last post by:
Example code in one of my books intrigues me: class B { public: B* Clone() const { B* p = DoClone(); assert(typeid(*p) == typeid(*this));...
6
by: pakis | last post by:
I am having a problem of pure virtual function call in my project. Can anyone explaine me the causes of pure virtual function calls other than...
21
by: sks | last post by:
Hi , could anyone explain me why definition to a pure virtual function is allowed ?
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!!!
10
by: Rahul | last post by:
Hi, I tried to create a abstract class by having a non-virtual member function as pure, but i got a compilation error saying "only virtual...
13
by: Mike -- Email Ignored | last post by:
Is a pure virtual function in allowed in a template base class? In any case, I have one working. Am I skating on thin ice? Thanks, Mike.
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...

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.