Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
Hi!
Why can't one instantiate an object of an abstract class?
Give compiler view of statement.

Thanks for your time!
Posted
Updated 12-Oct-11 7:45am
v3
Comments
Philippe Mori 13-Oct-11 8:37am    
The purpose of an abstract class is that it cannot be instantiate... thus your question does not make any sense. It is like asking why we cannot open a locked door...
Santosh K. Tripathi 25-Mar-15 12:58pm    
You know meaning of locked door. just think about a person who haven't heard about lock and he just know about door!
"It is like asking why we cannot open a locked door..."

my answer would we "there is some mechanism or some arrangement is made on the door that prevents it from opening. And this arrangement or mechanism is call lock."

Because it is abstract which in simple terms means not real, or an imaginary thing. you have to make a real class that is based on the abstract ideas to be able to instantiate it. This has nothing to do with the compiler, it is one of the rules of The C++ Programming Language[^].
 
Share this answer
 
Comments
Manfred Rudolf Bihy 12-Oct-11 13:34pm    
"... it is one of the rules of The C++ Programming Language."
or Java ...
or C# ...

A good generic description can be found here Wiki: Abstract Type.
Have a 5!
Richard MacCutchan 12-Oct-11 15:18pm    
Well the OP specified that he was interested in C++ so I chose not to confuse with other languages.
Espen Harlinn 12-Oct-11 14:54pm    
Good reply :)
Sergey Alexandrovich Kryukov 12-Oct-11 23:14pm    
Many ears ago I have read that, in many cases, the best answer to the children's endless "why?" would be "because", or "because this is how things work in life". In such cases children just need a confirmation. This case is similar. Even though "abstract" does not exactly mean "not real", the idea of your explanation is generally right. My 5.
--SA
Richard MacCutchan 13-Oct-11 5:07am    
Thanks, "not real" was a rather feeble attempt to explain a concept; not an easy thing to do (for me).
Here is a good language independant descritpion found on Wikipedia: http://en.wikipedia.org/wiki/Abstract_type[^].

Best Regards,

—MRB
 
Share this answer
 
Comments
Espen Harlinn 12-Oct-11 14:54pm    
Wikipedia is a great thing :)
Manfred Rudolf Bihy 12-Oct-11 15:00pm    
Yes, indeed! :)
Sergey Alexandrovich Kryukov 12-Oct-11 23:10pm    
My 5, too.
--SA
An abstract class in C++ does not have enough information to instantiate a valid object.
Read this link for more detail: http://msdn.microsoft.com/en-us/library/c8whxhf1.aspx[^]
 
Share this answer
 
Hi
We can't instatiate an oject of absract class because abstract class give as informations about the behavior but there are no implementation.
Absract class should be inherited, the sub class will inherit the behavior but the implementation is a matter of the sub class.

hope that this will help.
 
Share this answer
 
Here's the definition of an abstract class:
C++
#include <iostream>
// Interface to classes that can be used with iostream classes
class IStreamable {
public:
   virtual void read(std::istream& is) = 0; // abstract method!
   virtual void write(std::ostream& os) const = 0; // abstract method!
};
std::istream& operator>>(std::istream& is, IStreamable& s);
   s.read(is);
   return is;
}
std::ostream& operator<<(std::ostream& os, const IStreamable& s);
   s.write(os);
   return os;
}

This class defines, not the structure of a class, but a particular behaviour: any class that is derived from it and implements the abstract methods can interact with streams just like any of the built-in types.

For instance consider this class:
C++
#include"IStreamable.h" // make sure you've set your include path!
class MyData : public IStreamable {
private:
   int myint;
   double mydouble;
   char mychar;
public:
   MyData(int i, double d, char c) : myint(i), mydouble(d), mychar(c) {}
   virtual void read(std::istream& is);
   virtual void write(std::ostream& os) const;
};

void MyData::read(std::istream& is) {
   is >> myint >> mydouble >> mychar;
}
void MyData::write(std::ostream& os) const {
   os << myint << ' ' << mydouble << ' ' << mychar;
   // note: don't forget to add some whitespace separators!
}


Now, if you instantiate an object of class MyData, you can easily stream it any way you like, but there is no point instantiating IStreamable, as it has neither data elements to stream, nor the function implementation required to make it work:

C++
#include "MyData.h"

int main() {
   MyData a(3, 3.14, 'p'); // ok, a instantiates MyData
   std::cout << a << std::endl;  // ok, prints out "3 3.14 p"
//   IStreamable s; // error: cannot instantiate abstract class!
//   cout << s; // error: no implementation for IStreamable::write(std::ostream&)
   return 0;
}

As you can see it doesn't make sense to create an instance of IStreamable, and, to add insult to injury, the compiler cannot possibly do that either, as it cannot find any member function definitions.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900