Judging by your other question , it seems you don't understand how classes work. Classes are a set of functions that work with data.
functions themselves do not contain memory in the class. Next class:
struct dumb_class { void foo(){} void bar(){} void baz(){}
It has an int size. No matter how many functions you have, this class will only take up the space needed to work with int . When you call a function in this class, the compiler will give you a pointer to the place where the data is stored in the class; this is a pointer to this .
So, the function somewhere lies in memory, is loaded once at the beginning of your program and is waiting for a call with data to work.
Virtual functions are different. The C ++ standard does not define how virtual functions should behave, only what should be in this case. Typically, implementations use what is called a virtual table, or vtable for short. Vtable is a table of function pointers that, like ordinary functions, are allocated only once.
Take this class and suppose our constructor uses vtables:
struct base { virtual void foo(void); }; struct derived { virtual void foo(void); };
The compiler will need to create two vtables, one for the base and one for the derived. They will look something like this:
typedef func_ptr; func_ptr __baseTable[] = {&base::foo}; func_ptr __derivedTable[] = {&derived::foo};
How does he use this table? When you instantiate the class above, the compiler slides with a hidden pointer that points to the correct vtable. Therefore, when you say:
derived d; base* b = &d; b->foo();
When the last row is executed, it goes to the correct table ( __derivedTable in this case), goes to the correct index (in this case, 0) and calls this function. As you can see, this will cause a call to derived::foo , which is what should happen.
Note that in the future this is the same as derived::foo(b) , passing b as the this pointer.
So, when virtual methods are present, the size class will increase by one pointer (pointer to vtable.) Multiple inheritance will change a little, but basically it's the same thing. You can get more information on the C ++ - FAQ .
Now, to your question. I have:
struct base { virtual void foo(void) = 0; };
and base::foo has no implementation. This makes the pure abstract base::foo function. So, if I called it as stated above:
derived d; base* b = &d; base::foo(b);
What behavior should we expect? Being a pure virtual method, base::foo does not even exist. The above code is undefined behavior and can do anything: from nothing to failure, with anything in between. (Or worse.)
Think of what an abstract abstract function is. Remember that functions do not take data; they describe how to manipulate data. A pure abstract function states: "I want to call this method and manipulate my data. How you do it is up to you."
So, when you say: βWell, let me call an abstract method,β you respond to the above: βBefore me? No, you do it.β to which he will reply "@ ββ# ^ @ # ^". It just doesn't make sense to tell someone who says "do it," "no."
To answer your question directly:
"why can't we create an object for an abstract class?"
Hopefully now you see that abstract classes define only the functionality that a particular class should have. The abstract class itself is just a blue font; you do not live in blue prints, you live in houses that produce blue prints.