473,325 Members | 2,792 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,325 software developers and data experts.

extending classes and implementing interfaces C++ style

I have some code in Java that I need to translate into C++. My Java
code defines a class hierarchy as follows:

// interface IA
public interface IA
{
public abstract void doA();
}

// interface IB
public interface IB extends interface IA
{
public abstract void doB();
}

// concrete class JA conforming to interface IA
public class JA implements IA
{
public void doA() { ... }
}

// concrete class JB extending from JA (so that JB knows about method
doA() ) and
// conforms to interface IB (which is derived from interface IA)
public class JB extends JB implements IB
{
public void doB() { ...}
}

How do I translate these classes into C++ (specially class JB),
without duplicating code in C++ equivalent classes of JA and JB and
while making sure that JA and JB conform to their respecting
interfaces IA and IB.

Thanks

-- Albert
Webmaster, InterEC.NET

Feb 1 '07 #1
13 2087
Here is what I have so far:
// interface IA
class IA
{
public:
IA() {}
virtual ~IA() {}

void doA() throw() = 0;
}

// interface IB
class IB: public IA
{
public:
IB() {}
virtual ~IB() {}

void doB() throw() = 0;
}

// concrete class JA conforming to interface IA
class JA: public IA
{
public:
JA() {}
virtual ~JA()

void doA throw() { ... }
}

// concrete class JB conforming to interface IB
class JA: public JA, public IB
{
public JB() {}
virtual ~JB()

void doB throw() { ... }
}

When I compile this, I get an error message saying that doA is
abstract in class JA????

Feb 1 '07 #2

in*****@interec.net napsal:
Here is what I have so far:
// interface IA
class IA
{
public:
IA() {}
virtual ~IA() {}

void doA() throw() = 0;
}

// interface IB
class IB: public IA
{
public:
IB() {}
virtual ~IB() {}

void doB() throw() = 0;
}

// concrete class JA conforming to interface IA
class JA: public IA
{
public:
JA() {}
virtual ~JA()

void doA throw() { ... }
}

// concrete class JB conforming to interface IB
class JA: public JA, public IB
{
public JB() {}
virtual ~JB()

void doB throw() { ... }
}

When I compile this, I get an error message saying that doA is
abstract in class JA????
class JA: public JA, public IB is nonsense (I think it is typo) -
class cannot inherit from itself.

doA is abstract, because it is declared as void doA() throw() = 0;,
but I miss there key word virtual (in C++ are not all methods virtual
as in some other languages).

Feb 1 '07 #3
in*****@interec.net wrote:
I have some code in Java that I need to translate into C++. My Java
code defines a class hierarchy as follows:

// interface IA
public interface IA
{
public abstract void doA();
}

// interface IB
public interface IB extends interface IA
{
public abstract void doB();
}

// concrete class JA conforming to interface IA
public class JA implements IA
{
public void doA() { ... }
}

// concrete class JB extending from JA (so that JB knows about method
doA() ) and
// conforms to interface IB (which is derived from interface IA)
public class JB extends JB implements IB
{
public void doB() { ...}
}

How do I translate these classes into C++ (specially class JB),
without duplicating code in C++ equivalent classes of JA and JB and
while making sure that JA and JB conform to their respecting
interfaces IA and IB.

Thanks

-- Albert
Webmaster, InterEC.NET
Assuming that last class was supposed to be:
public class JB extends JA implements IB

I think this is what you want.

class IA
{
public:
virtual ~IA() {}
virtual void doA() = 0 ;
} ;

class IB : virtual public IA
{
public:
virtual void doB() = 0 ;
} ;

class JA : virtual public IA
{
public:
virtual void doA()
{
std::cout << "JA::doA()" << std::endl ;
}
} ;

class JB : public JA, public IB
{
public:
virtual void doB()
{
std::cout << "JB::doB()" << std::endl ;
}
} ;

Some notes:
The virtual keyword is not required everywhere I used it. Once you make
a function virtual in a base class, it remains virtual in derived
classes whether you declare it as such or not.

The "virtual" in virtual inheritance doesn't mean the same thing as the
"virtual" in virtual member functions. If you don't already understand
virtual inheritance, I suggest looking it up rather than confusing
yourself by making assumptions about what it does. Also, see Dave
Rahardja's thread from earlier today "Virtual Inheritance and
interfaces" for some debate about how it applies to your situation.

The virtual destructor in IA isn't required, but it is a Good Idea.
Also, there are some good arguments for why you should have at least one
virtual function in a class that is not defined inline. Since the
destructor of IA is the only function with a definition, it might make
sense not to define it inline as I did.

--
Alan Johnson
Feb 1 '07 #4
On Feb 1, 6:58 am, inte...@interec.net wrote:
Here is what I have so far:
[snip]
When I compile this, I get an error message saying that doA is
abstract in class JA????
Not in JB? I had to make a few changes to get your code through the
compiler but I never got that error, what I did get was an ambiguity
in JB if I tried to call doA(), since there's a doA() in both JA and
in IA (which JB inherits from indirectly through IB). This can be
solved with a using statement, see code below. This could probably be
solved in some other way, I'm not a big fan of large inheritance
hierarchies myself so I've never had this problem. There was a thread
just the other day about virtual public inheritance which might be
worth checking out.

// interface IA
class IA
{
public:
virtual void doA() throw() = 0;
};

// interface IB
class IB: public IA
{
public:
virtual void doB() throw() = 0;
};

// concrete class JA conforming to interface IA
class JA: public IA
{
public:
JA() { }
virtual ~JA();
void doA() throw() { }
};

// concrete class JB conforming to interface IB
class JB: public JA, public IB
{
public:
using JA::doA;

JB() { }
virtual ~JB();
void doB() throw() {doA();}
};

--
Erik Wikström

Feb 1 '07 #5
Alan Johnson wrote:
>
in*****@interec.net wrote:
>>
// concrete class JA conforming to interface IA
public class JA implements IA
Wrong way in C++. Do not use private inheritance in C++ without concrete
request, because it is nearly always much better to use composition here:

template< class IA>
class JA
{
private:
IA ia;
}
>public class JB extends JB implements IB
Here the same:

template< class IB>
class JB: public interface_JB
{
private:
IB ib;
}
>How do I translate these classes into C++
--
Maksim A Polyanin
Feb 1 '07 #6

Maybe i was wrong with Java directives, but i hope my reasons are clear -
use composition instead of C++ multiple inheritance:

Erik Wikstrom wrote:
>
// interface IA
class IA
{
public:
virtual void doA() throw() = 0;
};

// interface IB
class IB: public IA
{
public:
virtual void doB() throw() = 0;
};

// concrete class JA conforming to interface IA
class JA: public IA
{
public:
JA() { }
virtual ~JA();
void doA() throw() { }
};

// concrete class JB conforming to interface IB
class JB: public JA, public IB
template<class T=JA>
class JB: public IB
{
T ja;
{
public:
using JA::doA;
//forwarding here
void doA() throw() {ja.doA();}
JB() { }
virtual ~JB();
void doB() throw() {doA();}
//implement here
void doB() throw();
};

--
Maksim A Polyanin
Feb 1 '07 #7
Grizlyk wrote:
Alan Johnson wrote:
>in*****@interec.net wrote:
>>// concrete class JA conforming to interface IA
public class JA implements IA

Wrong way in C++. Do not use private inheritance in C++ without concrete
request, because it is nearly always much better to use composition here:

template< class IA>
class JA
{
private:
IA ia;
}
Where did anybody use private inheritance?
>
>>public class JB extends JB implements IB

Here the same:

template< class IB>
class JB: public interface_JB
{
private:
IB ib;
}
>>How do I translate these classes into C++
In your example, what is interface_JB? And what is IB? I can't really
tell what point you are trying to make. Runtime polymorphism can't be
achieved with templates and composition in C++.

--
Alan Johnson
Feb 1 '07 #8
Alan Johnson wrote:
in*****@interec.net wrote:

class JB : public JA, public IB
{
public:
virtual void doB()
{
std::cout << "JB::doB()" << std::endl ;
}
} ;
This works for this particular example, but in general, C++ classes that
are intended to emulate Java's (misguided) interfaces should be used as
virtual bases (as in the definition of IB). So:

class JB : public JA, public virtual IB

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Feb 1 '07 #9
Grizlyk wrote:
>
template< class IB>
class JB: public interface_JB
{
private:
IB ib;
}
The goal is to be able to pass objects of the derived type to a function
like this:

void f(const IB&);

Using IB to define a member doesn't support that.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Feb 1 '07 #10
Alan Johnson wrote:
>>>// concrete class JA conforming to interface IA
public class JA implements IA

Wrong way in C++. Do not use private inheritance in C++ without concrete
request, because it is nearly always much better to use composition here:

template< class IA>
class JA
{
private:
IA ia;
}

Where did anybody use private inheritance?
Yes, i forget, that Java directives "class JA implements IA" does not mean
"private inheritance". Look my message dated "12:45":
>Grizlyk wrote:
Maybe i was wrong with Java directives, but i hope my reasons are clear -
use composition instead of C++ multiple inheritance
It means, that independent from Java directives, the Java classes can be
implemented without multiple inheritance.
Runtime polymorphism can't be achieved with templates and composition in
C++.
What do you mean as "runtime polymorphism"? The following expression
>public class JB extends JB implements IB
can not be treated as "runtime polymorphism" (for C++ at least), because
"class JB " will be created at compile time or am I wrong?
>>>How do I translate these classes into C++
As I could understand, the problem is appearence of multiple inheritence for
direct Java -C++ casting and OP wants to avoid it (take in account, I can
not see whole thread), if yes:
1. In all cases any kind (ordinary or multiple) of "inheritance of
implementation" _can_ be replaced by composition (static or dynamic).
2. In most cases any kind (ordinary or multiple) of "inheritance of
implementation" _must_ be replaced by composition (static or dynamic),
because composition (instead of inheritance) increase ability of "reusage"
of classes.

Somebody implements the Java classes on C++ like this:
>>
// interface IA
class IA
{
public:
virtual void doA() throw() = 0;
};

// interface IB
class IB: public IA
{
public:
virtual void doB() throw() = 0;
};

// concrete class JA conforming to interface IA
class JA: public IA
{
public:
JA() { }
virtual ~JA();
void doA() throw() { }
};

// concrete class JB conforming to interface IB
class JB: public JA, public IB
Here he has used multiple inheritance.
>In your example, what is interface_JB?
In the example "interface_JB" is "IB".

This is my example of static implementation with the help of composition

template<class IA=JA>
class JB: public IB
{
IA ia;
public:
//forwarding
void doA() throw() {ia.doA();}
//to implement
void doB() throw();//{ia.doA();}
};

This is my example of dynamic implementation with the help of composition

template< class IA>
class JB: public IB
{
private:
IA *ia;

public:
//forwarding
void doA() throw() {ia->doA();}
//to implement
void doB() throw();//{ia->doA();}

public:
JB( IA *p):ia(p){}
};

Note, that explicit throw specification (as "throw(expr)") is not safe in
current C++ implementation, in the following example:

void doA() throw() { throw 0; }
try{ doA(); }catch(...){}

"throw 0;" will terminate your program.

--
Maksim A Polyanin
Feb 1 '07 #11
Pete Becker wrote:
Grizlyk wrote:
>>
template< class IB>
class JB: public interface_JB
{
private:
IB ib;
}

The goal is to be able to pass objects of the derived type to a function
like this:

void f(const IB&);

Using IB to define a member doesn't support that.

No, I was assuming "IB" is derived from "interface_JB". Look here
news:ep**********@aioe.org (message dated "16:03")

--
Maksim A Polyanin
Feb 1 '07 #12
Alan Johnson wrote:
>
Where did anybody use private inheritance?
Here
>// concrete class JB conforming to interface IB
class JB: public JA, public IB
"public JA" is really "inheritance of implementation".

--
Maksim A Polyanin
Feb 1 '07 #13
On 31 Jan 2007 21:51:01 -0800, in*****@interec.net wrote:
>I have some code in Java that I need to translate into C++. My Java
code defines a class hierarchy as follows:
In general, Java's interface definitions can be implemented in C++ as a class
with nothing but pure virtual functions (and a public virtual destructor that
does nothing).

Java:

public interface I
{
public abstract void fn();
}

C++:

class I // Interface
{
public:
virtual void fn() = 0;
virtual ~I() {}
};

Implementing a Java interface is analogous to virtual public inheritance in
C++.

Java:

public class A implements I
{
public void fn() { /* ... */ }
}

C++:

class A: public virtual I
{
public:
virtual void fn() { /* ... */ }
};
I started a thread about the pros and cons of performing this analogous
exercise in C++ about a week ago. Do a search for my name and the topic
"Virtual inheritance and interfaces".

-dr
Feb 2 '07 #14

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

Similar topics

4
by: Robert Zurer | last post by:
I notice that Microsoft puts their interfaces and classes in the same assembly. For example System.Data contains OleDbConnection and IDbConnection etc. I have found it useful to keep my...
8
by: Vishal Gandhi | last post by:
Hi , Please help me by advising an real life scenario where Abstract Classes should be used over Interfaces or vice versa . Whats the basic difference between Abstract Class and interface other...
4
by: Carl Youngblood | last post by:
I imagine this subject has probably been brought up numerous times. Forgive me for bringing it up again. I was googling through old posts on this newsgroup about it and found a good suggestion on...
3
by: Flip | last post by:
I'm looking at the O'Reilly Programming C# book and I have a question about extending and combining interfaces syntax. It just looks a bit odd to me, the two syntaxes look identical, but how does...
7
by: tshad | last post by:
I am trying to figure out when it is best to use Interfaces and when to use classes. The only reason I can figure out to use interface is if you need to implement multiple interfaces. The...
9
by: Kiran | last post by:
Hi, As a convention we always create interfaces before creating classes for class libraries. we then implement this interface in a class. Can someone point out the reasons\advantages for...
173
by: Zytan | last post by:
I've read the docs on this, but one thing was left unclear. It seems as though a Module does not have to be fully qualified. Is this the case? I have source that apparently shows this. Are...
7
by: Maximus Decimus | last post by:
HI all, I am using python v2.5 and I am an amateur working on python. I am extending python for my research work and would like some help and guidance w.r.t this matter from you experienced...
5
by: Wolfgang Hanke | last post by:
Hello, I want to extend multiple Controls like TextBox, Label, ComboBox etc. with the same new featureset (for Example all need a Method getSomething()) Because I cant alter their Base-Class I...
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
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...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
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

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.