Click to See Complete Forum and Search --> : Abstract Class problem


Saurabh Gupta
February 12th, 2003, 01:00 AM
CMy is a abstract class. As per abstract class definition we can not make any instance of that. But it is not giving any error in MSDEV IDE.

#include <iostream.h>
class CMy
{
public:
CMy(){}
CMy(int i)
{
this->i=i;
};

~CMy()
{
};
virtual void show() const =0;

int i;
};

void show(CMy &myObject)
{
myObject.i = 7;
}

void main(int argc, char* argv[])
{
CMy(9);
show(CMy(6));
}

SeventhStar
February 12th, 2003, 01:37 AM
well sionce the class has a constructor and a destructor it's not abstract.

deesan
February 12th, 2003, 03:01 AM
well sionce the class has a constructor and a destructor it's not abstract.

But there is a pure virtual function. AFAIK it's meaning that CMy is a abstract class.

SeventhStar
February 12th, 2003, 03:07 AM
Originally posted by deesan


But there is a pure virtual function. AFAIK it's meaning that CMy is a abstract class.
Okay. You CAN call it abstract but the constructor and destuctor allow you to create it.

deesan
February 12th, 2003, 04:11 AM
We don't need to declare constructors and destructors for all classes.
If some class don't have one of them, then compiler will generate default constructor (destructor)

So if class have not constructor, it's not meaning that we can't create variable of this class.

Am I wrong???

Graham
February 12th, 2003, 04:23 AM
No, no, no, no, no.

You cannot create an object of abstract class type no matter how many constructors you provide.


// Two things: don't use the ".h" forms of these includes
// and why is it here anyway?
#include <iostream.h>

class CMy
{
public:
CMy(){}
CMy(int i)
{
this->i=i; // Use an initialiser list
}; // this semicolon is redundant.

~CMy() // If you're going to make this class abstract,
// you really ought to make this virtual
{
}; // this semicolon is redundant.

virtual void show() const =0;

int i; // You really should make data members private
};

void show(CMy &myObject)
{
myObject.i = 7;
}

void main(int argc, char* argv[])
{
CMy(9);
show(CMy(6));
}


It's possible that the compiler optimises away both temporary objects, but it really ought to generate an error. There must be more to this. It's probably something to do with temporaries and references - anyone with a copy of the standard able to explain it? Unless it's a bug in VC++.

Graham
February 12th, 2003, 04:26 AM
Note that if you declare an actual object in main() - e.g. "CMy f(6)", then compilation fails with expected errors about abstract classes.

Saurabh Gupta
February 12th, 2003, 10:58 PM
Hi folks,
I am asking only one thing.
Is it MSDEV ID(VC++) bug or i could be happened. that was the
sample program so there could be some flaw but here my concern abt abstract class behavior .
Thanks for yours input.

Saurabh

mdmd
February 12th, 2003, 11:49 PM
VC6 seemed to be an issue so I searched and found
a good discussion here to determine if it is a bug. Slightly different
from this in syntax( several different methods )but still generating
a ctor call

http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_20250027.html

With VC7 that the correct compiler error is now generated
for the code you posted here.