473,325 Members | 2,872 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,325 software developers and data experts.

is Pure virtual Function have body?if not then why ?and if yes is it any useful

can anybody tell me pure virtual function in c++ have body present or not?
Jun 13 '07 #1
4 6509
Hmmm... what do you want to know?
Be a bit more specific
Are you looking for something like this?
Expand|Select|Wrap|Line Numbers
  1. virtual void someFunction() = 0;
  2.  
Jun 13 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
Yes, a pure virtual function can have a body.

All pure virtual means is that you can't call the function using an object that has declared or has inherited the pure virtual function. Because of this, you cannot create objects of classes with pure virtual functions.

However, you can call the pure virtual function from a derived class.

Expand|Select|Wrap|Line Numbers
  1. class base
  2. {
  3.     public:
  4.        void virtual AMethod() = 0;
  5. };
  6. void base::AMethod()
  7. {
  8.     cout << "Hello" << endl;
  9. }
  10.  
  11. class derived : public base
  12. {
  13.     public:
  14.        void Test();
  15. };
  16. void derived::Test()
  17. {
  18.      base::AMethod();  //OK.
  19. }
  20.  
Sometimes you can keep common processing in the pure virtual function just like you keep common data in the base class.
Jun 13 '07 #3
Yes, a pure virtual function can have a body.

All pure virtual means is that you can't call the function using an object that has declared or has inherited the pure virtual function. Because of this, you cannot create objects of classes with pure virtual functions.

However, you can call the pure virtual function from a derived class.

Expand|Select|Wrap|Line Numbers
  1. class base
  2. {
  3.     public:
  4.        void virtual AMethod() = 0;
  5. };
  6. void base::AMethod()
  7. {
  8.     cout << "Hello" << endl;
  9. }
  10.  
  11. class derived : public base
  12. {
  13.     public:
  14.        void Test();
  15. };
  16. void derived::Test()
  17. {
  18.      base::AMethod();  //OK.
  19. }
  20.  
Sometimes you can keep common processing in the pure virtual function just like you keep common data in the base class.

Can you Explain me in deep?and what is the use of pure virtual functions ?
plz explain in brief
Jun 14 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
A pure virtual function is to force the derived class to overrride it. What it measn is that you cannot call the pure virtual method using an object of the class that declares the pure virtual method or using an object of a derived class that inherits the method.

Suppose you design a class and you want everyone to have an Open() method.

Expand|Select|Wrap|Line Numbers
  1. class MyClass
  2. {
  3.      virtual void Open();
  4. };
  5.  
So, everybody derives form MyClass and implements an Open() method. All except the doofus in the last cubicle who implements a Start() method instead and refuses to write an Open() method.

By coding:
Expand|Select|Wrap|Line Numbers
  1. class MyClass
  2. {
  3.      virtual void Open() = 0;
  4. };
  5.  
any class without an Open() method will inherit the pure virtual function. Therefore, objects of those classes cannot call the Open() method.

The compiler enforces this by not allowing objects to be created using classes that have pure virtual funcitons or that have inherited pure virtual functions.

Now the codeof the doofus won't compile until an Open method is implemented.

Class designers use these pure virtual functions to tell you what you must implement in your class.
Jun 14 '07 #5

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

Similar topics

12
by: cppaddict | last post by:
Hi, I know that it is illegal in C++ to have a static pure virtual method, but it seems something like this would be useful when the following 2 conditions hold: 1. You know that every one...
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...
21
by: sks | last post by:
Hi , could anyone explain me why definition to a pure virtual function is allowed ?
3
by: Dmitry Prokoptsev | last post by:
Hello, I need to write a class for exceptions which can be thrown, caught, stored and thrown once again. I have written the following code: --- begin --- #include <string> class Exception...
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...
6
by: Miguel Guedes | last post by:
Hello, I recently read an interview with Bjarne Stroustrup in which he says that pure abstract classes should *not* contain any data. However, I have found that at times situations are when it...
1
by: jinendrashankar | last post by:
Hi All, 1.Please check this code and its output which is working fine while pure virtual functions( FUN1) prototype of Class CA is different then Class CB Function (FUN1). 2. In Class...
2
by: manjuks | last post by:
Hi, I have defined pure virtual destructor in base class. As far as I know pure virtual function makes us to redefine the same function in derived classes also. and also we can not create a...
14
by: Jack | last post by:
Hi, I meet a question with it , I did not get clear the different betteen them, for example: #include <iostream>
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.