Virtual classes help needed
3 posters
Virtual classes help needed
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.
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
Re: Virtual classes help needed
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)
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)
Re: Virtual classes help needed
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.
i guess this is d concept behind virtual class... But im not sure..
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..
Re: Virtual classes help needed
@Ganesh
class cannot be declared as virtual. Only functions can be virtual.
class cannot be declared as virtual. Only functions can be virtual.
Re: Virtual classes help needed
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
Re: Virtual classes help needed
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
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
Re: Virtual classes help needed
the link which was given in wikipedia virtual class topic refers to topic described using java only!
http://caesarj.org/index.php/ProgrammingGuide/VirtualClasses
http://caesarj.org/index.php/ProgrammingGuide/VirtualClasses
Re: Virtual classes help needed
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
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
Maithreyi- Posts : 76
Points : 142
Join date : 2010-03-03
Age : 36
Location : Haldia
Permissions in this forum:
You cannot reply to topics in this forum
|
|