Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Designing interface class in a good way

Reply
Thread Tools

Designing interface class in a good way

 
 
The Cool Giraffe
Guest
Posts: n/a
 
      02-25-2007
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?

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...

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?

*almost = until i got tired after about 2 hours

--
Vänligen Kerstin Viltersten
(The Cool Giraffe)


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      02-25-2007
The Cool Giraffe wrote:
> 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?


The "body of the method" is its *definition*. So, false.

> 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 ();


What's "DerivedClassA"?

> Is that correct? Do i need "new" somewhere? I'm a Java
> boy really so i like using "new" every now and then...


We C++ boys use "new" every now and then too, whether we like
it or not.

> 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?


groups.google.com.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
Jerry Coffin
Guest
Posts: n/a
 
      02-25-2007
In article <>,
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 counterproductive.

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.
 
Reply With Quote
 
The Cool Giraffe
Guest
Posts: n/a
 
      02-26-2007
Jerry Coffin wrote/skrev/kaita/popisal/schreibt :
> 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.


So, since i only will provide the declarations of the members,
hence, only providing virtual methods - is it possible to only
provide the header file, then? Or do i have to provide a cpp
file anyway and if so what does it contain?

<snip>
>> 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.



Here i have created an example for what i think i'd like to
ask about. Please point out the errors and/or iffy spots.
The cout is working (includes to that and std:: are implicit).

// file Base.h
class Base {
public:
Base ();
virtual void doSome ();
};

// file Base.cpp
// empty or non-existing

// file DeriA.h
// empty or non-existing

// file DeriA.cpp
#include "Base.h"
class DeriA : public Base {
public:
DeriA () {}
void doSome () { cout << "Test A"; }
};

// file DeriB.h
// empty or non-existing

// file DeriB.cpp
#include "Base.h"
class DeriB : public Base {
public:
DeriB () {}
void doSome () { cout << "Test B"; }
};

// file Test.h
// empty or non-existing

// file Test.cpp
#include "Base.h"
class Test {
public:
Base base;
void assignA () { base = DeriA (); }
void assignB () { base = DeriB (); }
};

Above anything you might have to say i'd like to
explicitly ask about the following.

1. Is my not-using destructors a problem?
2. What exactly should i include in Test-class?
3. I've read that Base base; will create an object of that
class. On the other hand, it's abstract, so it won't do it. Does
it mean that i'm supposed to declare a handle base to take
care of the problem? Is it Base& base; or perhaps should i
use Base* base; instead?

--
Vänligen Kerstin Viltersten
(The Cool Giraffe)


 
Reply With Quote
 
paul.joseph.davis@gmail.com
Guest
Posts: n/a
 
      02-26-2007
On Feb 26, 2:39 am, "The Cool Giraffe" <giraf...@viltersten.com>
wrote:
> Jerry Coffin wrote/skrev/kaita/popisal/schreibt :
>
>
>
> > giraf...@viltersten.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.

>
> So, since i only will provide the declarations of the members,
> hence, only providing virtual methods - is it possible to only
> provide the header file, then? Or do i have to provide a cpp
> file anyway and if so what does it contain?
>
> <snip>
>
> >> 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.

>
> Here i have created an example for what i think i'd like to
> ask about. Please point out the errors and/or iffy spots.
> The cout is working (includes to that and std:: are implicit).
>
> // file Base.h
> class Base {
> public:
> Base ();
> virtual void doSome ();
>
> };
>
> // file Base.cpp
> // empty or non-existing
>
> // file DeriA.h
> // empty or non-existing
>
> // file DeriA.cpp
> #include "Base.h"
> class DeriA : public Base {
> public:
> DeriA () {}
> void doSome () { cout << "Test A"; }
>
> };
>
> // file DeriB.h
> // empty or non-existing
>
> // file DeriB.cpp
> #include "Base.h"
> class DeriB : public Base {
> public:
> DeriB () {}
> void doSome () { cout << "Test B"; }
>
> };
>
> // file Test.h
> // empty or non-existing
>
> // file Test.cpp
> #include "Base.h"
> class Test {
> public:
> Base base;
> void assignA () { base = DeriA (); }
> void assignB () { base = DeriB (); }
>
> };
>
> Above anything you might have to say i'd like to
> explicitly ask about the following.
>
> 1. Is my not-using destructors a problem?
> 2. What exactly should i include in Test-class?
> 3. I've read that Base base; will create an object of that
> class. On the other hand, it's abstract, so it won't do it. Does
> it mean that i'm supposed to declare a handle base to take
> care of the problem? Is it Base& base; or perhaps should i
> use Base* base; instead?
>
> --
> Vänligen Kerstin Viltersten
> (The Cool Giraffe)



Your lack of destructors is a problem. Classes with virtual functions
should have virtual destructors. The base class can be a simple empty
function if need be.


For Base.* you either need pure virtual specifiers or an
implementation:

If you're shooting for pure abstract ( To mimick java's interface
type ) you probably don't need a constructor.

class Base {
public:
virtual Base() {}
virtual void doSome () = 0 ;
} ;

DeriA.h and DeriB.h must not be empty and contain a declaration of the
class. Ie:

DeriA.h:

class DeriA {
public:
DeriA ();
virtual ~DeriA() ;
virtual void doSome () ;
};

Test.h can be empty.

Test.cpp needs to include DeriA.h and DeriB.h to use those classes
though.

HTH,
Paul Davis

 
Reply With Quote
 
throatslasher
Guest
Posts: n/a
 
      02-26-2007
On Feb 26, 1:07 am, "paul.joseph.da...@gmail.com"
<paul.joseph.da...@gmail.com> wrote:
> Your lack of destructors is a problem. Classes with virtual functions
> should have virtual destructors. The base class can be a simple empty
> function if need be.


Somebody makes this overgeneralization every month or two. Please
think before you write!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Designing User Interface imresearcher@gmail.com ASP .Net 0 03-13-2007 11:47 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Public Poll/advice on designing Controls that need Styles to look good...How?!? Sky Sigal ASP .Net 2 08-09-2004 06:20 PM
Designing an interface with exceptions Jim Sculley Java 7 07-15-2004 10:04 PM
Designing Data Interface for Very Large Files [more than GB size] shailesh kumar C++ 6 11-17-2003 09:23 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57