473,386 Members | 1,758 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,386 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 1203
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 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 ]
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 class A have some derived classes. I want A's...
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)); return p; }
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 calling a virtual function in base class? Thanks
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 member functions can be pure"... I'm trying to think...
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.