By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 1,902 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

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

P: 13
can anybody tell me pure virtual function in c++ have body present or not?
Jun 13 '07 #1
Share this Question
Share on Google+
4 Replies


P: 94
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
Expert Mod 5K+
P: 9,197
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

P: 13
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
Expert Mod 5K+
P: 9,197
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

Post your reply

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