Unix C++
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Virtual classes help needed

3 posters

Go down

Virtual classes help needed Empty Virtual classes help needed

Post by Maithreyi Mon Mar 29, 2010 11:34 am

Can somebody give a simple example to demonstrate virtual classes.

According to Wikipedia a virtual class is an inner class that can be overridden by the subclass of an outer class.

Help would be greatly appreciated.

Maithreyi

Posts : 76
Points : 142
Join date : 2010-03-03
Age : 36
Location : Haldia

Back to top Go down

Virtual classes help needed Empty Re: Virtual classes help needed

Post by mightyganesh Mon Mar 29, 2010 3:40 pm

it is very clear from the statement itself!

its just like virtual function! where we have a function with same signature in base class and derived class which creates difference in execution when derived class object is referred by base class pointer identifier and derived class identifier!
in order to remove this prob we use virtual function in base class!

similar to that we can have inner classes in the base which might create anomalies when we have inner class with same name in derived class!
so in order to avoid that we use virtual keyword to create virtual inner classes!!

(note :inner classes are there in C++ but they are not in our portion)
mightyganesh
mightyganesh

Posts : 53
Points : 92
Join date : 2010-02-26
Age : 35

http://ganeshguna.blogspot.com

Back to top Go down

Virtual classes help needed Empty Re: Virtual classes help needed

Post by Christopher Mon Mar 29, 2010 3:43 pm

Have a look at the example. Class 'Inner' inside Base is overwritten in Derived Class. Also doit() method is defined as virtual in Base class which creates a object for Inner class and calls its disp() function.

Now create a base class reference and assign a derived class object. Now call its doit() function. This time this creates object of 'Derived::Inner' class. Base::Inner is hidden. This is called Virtual Class.
Code:
#include<iostream.h>

using namespace std;

class Base
{
   int a;

public:
   class Inner
   {
   public:
      int k;

      virtual void disp()
      {
         cout<<"Base inner Executed";
      }
   };

   virtual void doit()
   {
      Inner a;
      a.disp();
   }
};

class Derived:public Base
{
   int a;

public:
   class Inner
   {
   public:
      int k;

      void disp()
      {
         cout<<"Derived inner Executed";
      }
   };

   void doit()
   {
      Inner a;
      a.disp();
   }
};


int main()
{
   Base *b=new Derived;
   b->doit();
}

i guess this is d concept behind virtual class... But im not sure..
Christopher
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 35

https://unixcpp.forumotion.com

Back to top Go down

Virtual classes help needed Empty Re: Virtual classes help needed

Post by Christopher Mon Mar 29, 2010 3:44 pm

@Ganesh
class cannot be declared as virtual. Only functions can be virtual.
Christopher
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 35

https://unixcpp.forumotion.com

Back to top Go down

Virtual classes help needed Empty Re: Virtual classes help needed

Post by Maithreyi Mon Mar 29, 2010 3:56 pm

Yes classes cannot be virtual.But,the example you have given is in no way different from the concept of virtual functions,except that there is nesting shown.Would do for now I guess, since we don't have resources to refer to!

Maithreyi

Posts : 76
Points : 142
Join date : 2010-03-03
Age : 36
Location : Haldia

Back to top Go down

Virtual classes help needed Empty Re: Virtual classes help needed

Post by mightyganesh Mon Mar 29, 2010 5:41 pm

ya i also feel its virtual function over loading!!

virtual class is oop concept but its not present in C++!!

look into this link!

http://en.allexperts.com/q/C-1040/thing-virtual-class-abstract.htm
mightyganesh
mightyganesh

Posts : 53
Points : 92
Join date : 2010-02-26
Age : 35

http://ganeshguna.blogspot.com

Back to top Go down

Virtual classes help needed Empty Re: Virtual classes help needed

Post by mightyganesh Mon Mar 29, 2010 5:44 pm

the link which was given in wikipedia virtual class topic refers to topic described using java only!

http://caesarj.org/index.php/ProgrammingGuide/VirtualClasses
mightyganesh
mightyganesh

Posts : 53
Points : 92
Join date : 2010-02-26
Age : 35

http://ganeshguna.blogspot.com

Back to top Go down

Virtual classes help needed Empty Re: Virtual classes help needed

Post by Christopher Mon Mar 29, 2010 5:45 pm

Remove class 'Inner' in Derived class and execute the program. Now "Base::Inner" object ll be created.

So in previous case, it s hidden by derived::inner class. That is the concept of virtual class according to this article.

http://caesarj.org/index.php/ProgrammingGuide/VirtualClasses
Christopher
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 35

https://unixcpp.forumotion.com

Back to top Go down

Virtual classes help needed Empty Re: Virtual classes help needed

Post by Maithreyi Tue Mar 30, 2010 10:47 am

Thanks!

Maithreyi

Posts : 76
Points : 142
Join date : 2010-03-03
Age : 36
Location : Haldia

Back to top Go down

Virtual classes help needed Empty Re: Virtual classes help needed

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum