In article <54************ *@mid.individua l.net>,
gi******@vilter sten.com
says...
I'm designing an ABC and in connection to that i have run
into some "huh!" and "oh...". Let me put it as a list.
1. Since the class will only contain bodies of the methods,
only the header file is needed. There will be no definitions
provided until i derive the ABC. True or false?
I think you mean it will contain only declarations of the methods, not
definitions. A declaration has no body, so to speak.
This is not necessarily the case: at least as I've always used the term,
an abstract base class contains at least one pure virtual function (I.e.
which is declared but not defined). Other functions may or may not be
defined in the base class.
If you want, you can certainly create an abstract base class that's
simliar to a Java interface -- i.e. that only declares functions, but
doesn't define any of them. C++, however, doesn't _require_ anything
like this.
2. Since i'll have two different classes (both derived from
the original ABC) i'll use the following syntax in my main
class using the derivation.
AbstractClass* obj;
obj = DerivedClassA ();
Is that correct? Do i need "new" somewhere? I'm a Java
boy really so i like using "new" every now and then...
Use of new (or lack thereof) is related primarily to lifetime. If you're
creating an object that needs to live after execution exits from the
current scope, chances are you'll need to use new to create it. If you
want the object to cease to exist when execution exits from the current
scope, new is probably unnecessary and may be counterproducti ve.
Good use of an ABC doesn't require that when you create the object you
use a pointer (or reference) to the base class. Consider something like:
class base { /* whatever */ };
class derived : public base { /* whatever */ };
void some_func(base &b) { /* use object */ }
int main() {
derived x;
some_func(x);
return 0;
}
This allows some_func to operate any derivative of base, even though
were we create base, we're just creating an automatic object.
3. I have looked through the whole of the internet (almost*)
for a simple syntax example that will show how to set up
such a derivation scheme but to no avail. Any pointers?
Perhaps if you told us what you're really trying to accomplish we could
provide more help. Right now, it's not clear what sort of "derivation
scheme" you really want.
--
Later,
Jerry.
The universe is a figment of its own imagination.