|
|
 |
| Author |
Message |
imanish11111

|
Posted: Visual C++ Language, Abstract class object |
Top |
i hav read in all books dat we cant create object of abstract classes.. . and reasons like they are too abstract to create an object.. .. i want to know more of these reasons why we cant create objects...... . . . .
manish
Visual C++5
|
| |
|
| |
 |
Bite Qiu - MSFT

|
Posted: Visual C++ Language, Abstract class object |
Top |
hello
AFAIK, Abstract class is incomplete, which means it has at least one pure virtual function. When we create an instance of an object, a certain memory space will be allocated to contain this object, abstract class does not have enough information to achieve this allocation. so it can not create an abstract object.
Supplement is welcome :)
Bite
|
| |
|
| |
 |
Marius Bancila

|
Posted: Visual C++ Language, Abstract class object |
Top |
You cannot create an instance of an abstract class simply because it doesn't have an implementation of all functions (it has at least one pure virtual function). What would you expect do to if you could create one, and then call the function that doesn't have an implementation So, that's the reason.
|
| |
|
| |
 |
Vivek Ragunathan

|
Posted: Visual C++ Language, Abstract class object |
Top |
Hi
An abstract class is one that has atleast one pure virtual methods.
Normally when an object of a class is created, the vtable is filled in
with the address of the virtual methods, which is one of the reasons
why an object for an abstract class cannot be created. Assuming that
the related vtable entry is filled with null, this would cause a
exception thrown at run-time, and that is not desirable. This is
something related to the implementation.
Theoritically, an abstract class is much like an interface. It
describes what the class does, and does not say how it does. At run
time, we need how to do, more than what to do. So an object of a class
that know what to do but not how to do is not worth creating, it is
conceptual.
Another interesting point to note is that in C++, pure virtual functions can have a method body.
Regards
Vivek Ragunathan
|
| |
|
| |
 |
Marius Bancila

|
Posted: Visual C++ Language, Abstract class object |
Top |
Vivek Ragunathan wrote: | | Another interesting point to note is that in C++, pure virtual functions can have a method body. |
|
That is exactly what I had in mind to add
class foo
{
public:
virtual void run() const = 0 {std::cout << "foo::run" << std::endl;}
};
class bar : public foo
{
public:
virtual void run() const { foo::run(); std::cout << "bar::run" << std::endl; } };
Though run() has a body in foo, it is still pure virtual, so you cannot create in instance of foo. But you can create one of bar. int main()
{
foo *f = new bar;
f->run();
delete f;
return 0;
}
|
| |
|
| |
 |
imanish11111

|
Posted: Visual C++ Language, Abstract class object |
Top |
thank . i have a doubt classes like
class foo { public: virtual void run() const = 0 {std::cout << "foo::run" << std::endl;} };
in above case comiler is having each information to create an object then why cant we create an object of above class ..
please reply..
|
| |
|
| |
 |
Marius Bancila

|
Posted: Visual C++ Language, Abstract class object |
Top |
Because the language is designed so that classes with pure virtual functions (even if they have an implementation) cannot be instantiated. That doesn't mean the pure virtual function (in base) cannot be called. It can, as I've shown you, but you are not allowed to instantiate the abstract class. As said earlier, an abstract class is like an interface (which only defines a contract). You cannot intantiate an interface.
|
| |
|
| |
 |
Bite Qiu - MSFT

|
Posted: Visual C++ Language, Abstract class object |
Top |
in above case comiler is having each information to create an object then why cant we create an object of above class .. |
|
good question :)
in this scenario, YES! it has enough information to create an instance, but can compiler always feel confidence to do so or you may suggest the compiler to validate the abstract class and make a decision afterwards, again YES!, you can implement an compiler that allowing you to do this, but this is an contravention to the concept of abstract class.
|
| |
|
| |
 |
Bite Qiu - MSFT

|
Posted: Visual C++ Language, Abstract class object |
Top |
Nevermind about the memory stuff, I think the best answer is like what Marius indicated, the language designed to be so.
|
| |
|
| |
 |
Vivek Ragunathan

|
Posted: Visual C++ Language, Abstract class object |
Top |
Hi
You must considering two aspects here - concept and implementation.
Virtual Call Mechanism and Abstract classes is a concept. In the C++, the abstract classes can have pure virtual functions with method body. May be in my D++, I may not implement that way. If you are writing an abstract class in C++, follow its rules. Nothing mysterious.
Regards
Vivek Ragunathan
|
| |
|
| |
 |
| |
|