Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > extending classes and implementing interfaces C++ style

Reply
Thread Tools

extending classes and implementing interfaces C++ style

 
 
interec@interec.net
Guest
Posts: n/a
 
      02-01-2007
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

 
Reply With Quote
 
 
 
 
interec@interec.net
Guest
Posts: n/a
 
      02-01-2007
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????

 
Reply With Quote
 
 
 
 
Ondra Holub
Guest
Posts: n/a
 
      02-01-2007

http://www.velocityreviews.com/forums/(E-Mail Removed) 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).

 
Reply With Quote
 
Alan Johnson
Guest
Posts: n/a
 
      02-01-2007
(E-Mail Removed) 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
 
Reply With Quote
 
=?iso-8859-1?q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      02-01-2007
On Feb 1, 6:58 am, (E-Mail Removed) 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

 
Reply With Quote
 
Grizlyk
Guest
Posts: n/a
 
      02-01-2007
Alan Johnson wrote:
>
> (E-Mail Removed) 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


 
Reply With Quote
 
Grizlyk
Guest
Posts: n/a
 
      02-01-2007

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


 
Reply With Quote
 
Alan Johnson
Guest
Posts: n/a
 
      02-01-2007
Grizlyk wrote:
> Alan Johnson wrote:
>> (E-Mail Removed) 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
 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      02-01-2007
Alan Johnson wrote:
> (E-Mail Removed) 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)
 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      02-01-2007
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)
 
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
Declaring and implementing exceptions inside interfaces? josh Java 6 12-19-2006 08:29 AM
Style question on abstract classes that implement interfaces c_kernel@hotmail.com Java 2 10-11-2006 04:16 AM
new style and classic style classes ? ankit Python 1 12-22-2005 08:34 AM
Implementing Interfaces and Type Safety (OOP Newbie in Perl) Veli-Pekka Tätilä Perl Misc 7 08-20-2005 10:02 AM
Conditionally implementing __iter__ in new style classes Thomas Heller Python 13 07-08-2005 06:16 AM



Advertisments