473,778 Members | 1,901 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Designing interface class in a good way

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)
Feb 25 '07 #1
5 1608
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.c om.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Feb 25 '07 #2
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.
Feb 25 '07 #3
Jerry Coffin wrote/skrev/kaita/popisal/schreibt :
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.
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)
Feb 26 '07 #4
On Feb 26, 2:39 am, "The Cool Giraffe" <giraf...@vilte rsten.com>
wrote:
Jerry Coffin wrote/skrev/kaita/popisal/schreibt :
giraf...@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.

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

Feb 26 '07 #5
On Feb 26, 1:07 am, "paul.joseph.da ...@gmail.com"
<paul.joseph.da ...@gmail.comwr ote:
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 overgeneralizat ion every month or two. Please
think before you write!

Feb 26 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2142
by: E G | last post by:
Hi! I am having problems in designing a class. First, I have a base class that allocates a 3D data set and allows some other mathematical operations with it, something like this: template <typename T> class BasicArray {
12
1892
by: James Brown | last post by:
Hi all, Having problems designing a template-class. I'll describe my scenario first then show what I've come up with so far: Need a class to provide pointer/array-like access to an area of physical memory located on a piece of custom hardware - this memory is only accessible using machine specific i/o so I want to hide all this in a class. I'm imagining
6
2953
by: Gary James | last post by:
This may not be a direct C# question, but since I'll be using using C# for development, I thought I'd pose the question here. I'll soon be involved in the design of a new software product that will employ a software "Plug-In" architecture. Taking the plug-in route will give us a design that can adapt to, as yet, undefined future requirements (within the scope of the plug-in interface spec of course). In the past I've done this with...
2
1415
by: Sky Sigal | last post by:
Hello: I'm currently messing around, and need as much feedback/help as I can get, trying to find the most economical/graceful way to build usercontrols that rely on styling to look any good... It's the last part that has got me all frazzled (the 'rely on...to look good')... Let me explain -- and please bear with me as it's a bit longer than my usual questions:
0
1009
by: Julia Beresford | last post by:
Hi I have a VB .NET class library with classes and collection classes (classes that implement ICollection) that allow a client to read and write data to and from an SQL database. For example if I had a table of Things I would have a Things collection class with an Add method that would add a Thing to the collection but also add that as a new record in the database. Now I want to expose this functionality via a Web Service. However it...
2
1387
by: jack | last post by:
Hi all, im in a middle of an project and it is going pretty well. But one this that is always in back of my mind is the interface design. every day i look at it i tend to change either the position of label or textbox i have seen many application which have cool user interface. i want to know is there any good reference through which interface designing can be studied. I know that an application should be fully functional but, good...
6
1912
by: S. Lorétan | last post by:
Hi guys. I am preparing the rewriting of an huge existing application in VB6. This software is a mess of 10 years of patchs and new functionalities added randomly, and didn't have any logical or oriented-object background. This software is used by many differents persons, and there is a LOT of functionalities that exists only for one or two of them. So, I want to use a plugin-based system. That's it for the background.
0
1987
by: Memfis | last post by:
While I was looking through the group's archives I came across a discussion on doing properties (as known from Delphi/C#/etc) in C++. It inspired me to do some experimenting. Here's what I came up with. First we have an interface defining how a property can be used (read and written - for simplicity, I ignored read-only and write-only variants). template<typename Tclass Property { public:
1
10102
weaknessforcats
by: weaknessforcats | last post by:
Introduction Polymorphism is the official term for Object-Oriented Programming (OOP). Polymorphism is implemented in C++ by virtual functions. This article uses a simple example hierarchy which you may have seen many times in one form or another. An analysis of this example produces several problems that are not obvious but which will seriously limit your ability to use hierarchies like the example in a real program. Then, the article...
0
9629
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10298
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10069
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9923
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7475
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6723
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5370
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5500
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4033
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.