Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
can we declare a constructor of an abstract class?
if yes then why? if no then also why?(but it is allow in java)
Posted
Comments
Andrew Brock 19-Jan-11 22:52pm    
Since you are asking a lot of questions regarding basics of C++ I would suggest reading a book or some tutorials
Sergey Alexandrovich Kryukov 19-Jan-11 23:41pm    
You're absolutely right. We're doing fun exercise, where only systematic education really works. Good reading does the trick.
Sergey Alexandrovich Kryukov 20-Jan-11 0:19am    
Besides, as I already said about previous question, it looks like OP is doing fine trolling to us, which is fine :-)
Andrew Brock 20-Jan-11 0:35am    
If it wasn't fine, or I didn't like it I just wouldn't answer
Sergey Alexandrovich Kryukov 20-Jan-11 15:16pm    
:-)

Abstract class just means that there is 1 or more methods that have no code defined.
It is perfectly legal to supply a constructor because any member variables of the class need to be initialised still (especially private ones)
class Graph {
public:
	Graph(int nMyInt) : m_nMyInt(nMyInt) { }
	virtual void GraphFunc() = 0; //our virtual function
	int GetMyInt() { return m_nMyInt; }

private:
	int m_nMyInt;
};

class Linear : public Graph {
public:
    Linear() : m_nMyInt(20) { }
    virtual void GraphFunc() { /* some code for this func */ }
};
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 20-Jan-11 0:14am    
My 5 for your answer. I used your sample for additional notes.
Thank you.
--SA
Nuri Ismail 20-Jan-11 3:34am    
Good answer. 5
Andrew's answer is very good but need some notes.

I would like to emphasize that the concept of abstract class is irrelevant to availability of constructor(s). That could cause some misunderstanding. See below.

As Andrew correctly explained us, "abstractness" of a class is recognized by one or more "pure virtual functions", his example: GraphFunc(). The compiler will not compile initialization if such class -- for a good reason (what should happen with the instance if someone calls a pure virtual function?). For more modern OOP languages a special keyword abstract is used to denote a class (or a method, a syntax used for the same purpose as C++ as pure virtual) as abstract. The sole purpose of the abstract class is to server as a base class for derived non-abstract classes.

Using declarations from Andrew's sample, we can add:

C++
class RectangularGraph : public Graph {
public:
    RectangularGraph(int nMyInt, int extraData) :
       Graph(nMyInt) { m_extraData = extraData; }
    virtual void GraphFunc() { /* some body */ }
private:
    int m_extraData;
};

//...

//Graph graph(1); //would not compile
RectangularGraph(2); //all correct


So, my point here is, if an abstract class cannot be "constructed" (a class instance can not be initialized), why it needs a constructor?

This is because a constructor, strictly speaking, has nothing to do with instance initialization -- it is designed to initialize part of the instance: see constructor RectangularGraph: it is composed of two parts: it's base is constructed first (came from Graph class), then the rest of the class RectangularGraph. Finally, it should be noted that a parameterless constructor of a base class (no matter abstract or not) is called automatically.

This way, not only constructors of abstract class is allowed -- it's very important. Look at the sample: how else m_nMyInt (see Andrew's sample) could be initialized, if it is not accessible for derived classes?
 
Share this answer
 
Comments
JF2015 20-Jan-11 0:16am    
Good explanation! 5+
Andrew Brock 20-Jan-11 0:17am    
My 5. Thanks for elaborating
Nuri Ismail 20-Jan-11 3:35am    
Very good explanation indeed. Have a 5.
Both answers from Andrew Brock and SAKryukov are very good and I'm sure that they clarified a lot of things for you. I have a small addition about constructors.

In some cases it is handy to use the Virtual Constructor Idiom[^] in your abstract base classes. For additional details see this item[^] from C++FAQ[^]. :)
 
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