473,320 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to program to an interface

Hi

I've been wondering about how "programming to an interface, not an
implementation" works in C++.

As I understand it, I start with an abstract base class as the interface:

class TestInterface {
public:
...
virtual int foo() = 0;
...
};

- and use this for the implementation:

class TestImp : public TestInterface {
public:
...
virtual int foo();
...
private:
...
};
In an client class I may then do something like:

Client::UseFoo(TestInterface *test) {
test->foo();
}

But that's just polymorphism - so "Interface = Polymorphism"?

And the test object still needs to be created somewhere, like:

Client::UseFoo() {
TestImp *test = new TestImp;
test->foo();
delete test;
}

So the client still needs to know about the implementation. So why use an
interface if polymorphism is not needed? What if the private part of TestImp
contained some classes from an external lib, that the client should not know
about?

Obviously I'm missing something :)

Mar 2 '06 #1
5 1765
Mikael wrote:

Obviously I'm missing something :)


Factories.

Your header file should contain only the interface.

Your IMP.cpp file can contain the whole definition of TestImp.

You can add a factory method e.g.

TestInterface * MkTest(); // in header

- now you have complete separation from implementation and interface.

You can also use generic factories ....

http://austria.sourceforge.net/dox/h...Factories.html
(the doc is useless - see the test case).

Mar 2 '06 #2
"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:88******************************@speakeasy.ne t...
Factories.

Your header file should contain only the interface.

Your IMP.cpp file can contain the whole definition of TestImp.

You can add a factory method e.g.

TestInterface * MkTest(); // in header

- now you have complete separation from implementation and interface.

You can also use generic factories ....

http://austria.sourceforge.net/dox/h...Factories.html
(the doc is useless - see the test case).

So - to stick with my example - the following is the way to do it (changes
marked with a "|") ?

class TestInterface {
public:
| TestInterface *Create();
...
virtual int foo() = 0;
...
};

and in the cpp file for TestInterface:

| #include "TestInterface.h"
| #include "TestImp.h"
| TestInterface::Create() {
| return new TestImp();
| }

Should Create be static? virtual?

One nitpick, TestInterface is no longer a pure abstract base class, because
of Create. Is that a problem?

Mar 2 '06 #3
One nitpick, TestInterface is no longer a pure abstract base class,
because of Create. Is that a problem?


It still has a virtual function that it doesn't define... sounds abstract
to me.
-Tomás
Mar 2 '06 #4
>But that's just polymorphism - so "Interface = Polymorphism"?
It seems to me that current idea of interfaces in C++ came from Java,
where there is a huge difference between interface and class.
Originally in C++ interface is .h file (class declaration) and
implementation is cpp file. Nevertheless I can say that "C++ interface
class" is a class where exist only following:
1) Pure virtual functions.
2) Virtual destructor.
3) Enumerators and/or const static _data_ members.

So no data members (even static) & no implemented functions.

Mar 2 '06 #5
"Mikael" <none> wrote:
So - to stick with my example - the following is the way to do it (changes
marked with a "|") ?

class TestInterface {
public:
| TestInterface *Create();
...
virtual int foo() = 0;
...
};

and in the cpp file for TestInterface:

| #include "TestInterface.h"
| #include "TestImp.h"
| TestInterface::Create() {
| return new TestImp();
| }

Should Create be static?
Yes.
virtual?
No.
One nitpick, TestInterface is no longer a pure abstract base class,
because of Create.
It's still abstract.
Is that a problem?


No. Why would it be?

Mar 2 '06 #6

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

Similar topics

0
by: Christian Schuhegger | last post by:
Hi, I remember that I've seen some time ago (perhaps a year or so) a project on freshmeat where a guy hacked a postgres c-interface library (i guess it was libpq / or perhaps he just used the...
8
by: Zheng Da | last post by:
I don't know where should I ask the question, so send the email to this group. I choose this group, because I want to write the program with c++ :) I want to write a program which support...
34
by: kevin.watters | last post by:
Hi all, I have a need for a short program: Given a drive letter, it would recursively search through all directories, "generating" each filename that it encounters (need to pass each filename...
2
by: Graham Menhennitt | last post by:
I have a large Python 2.5 program that I want my users to be able to "extend" using a Python script. However, I want their script to run in a sandbox within the overall program so that they only...
20
by: Francine.Neary | last post by:
I am learning C, having fun with strings & pointers at the moment! The following program is my solution to an exercise to take an input, strip the first word, and output the rest. It works fine...
1
by: Jim Langston | last post by:
Windows. Situation: Using a Python program called OpenRPG. I have a program that displays form data (a character sheet) in C++. I am able in the C++ program to build a string and copy it into the...
1
by: Hypnotik | last post by:
Well on to my next program. The program tally's votes on 3 issues. I have a class for the interface (asks questions) and a class for adding the tallies. In main there is a counter of Tally data...
1
by: Kayvine | last post by:
Hi guys, this is a question I have for an assignment, it is pretty long, but I am not asking for the code(well if someone wants to write I'll be really happy, lol), but I just want to know how to...
1
by: AngreGanon | last post by:
Hi all~ I've written a network program in C. This program catputures ARP request packet and reply wrong ARP packet. Hi. I've written a small program to learn to write in C. But unfortunately...
38
by: abasili | last post by:
Hi everyone, I'm trying to compile a C++ function and then call it from a C program. Since Google is my friend I've ended up to this link which seems very clear: ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.