2

first posting so please be gentle!

I have an interface that's like this

public interface I_Hospital{
    public void registerObserver(I_Person p);
    public void removeObserver(I_Person p);
    public void notifyObservers();
    public void addWard(Ward ward);
}

Now, if I want to recreate this in C++, is it correct to do the following:

IHospital.h

Class IHospital{
    public:
    virtual void registerObserver(IPerson p) = 0;
    etc...
}

Is this the correct implementation on an Interface in C++??

Thanks, Patrick

2
  • 3
    Yes, virtual _signature_ = 0; gives it the same semantics as an interface in Java.. That said, you may want to change it to virtual void registerObserver(IPerson &p) = 0; using a reference to p, rather than just passing it in. Since C++ defaults to call by value, what you've written will not work the same as Java, converting to a reference produces about the same results as your Java code. Commented Sep 15, 2014 at 15:16
  • 1
    +1 for having spent the time to research it and try it on your own, rather than just asking how Commented Sep 15, 2014 at 15:30

3 Answers 3

5

Yes, you'd define an interface as an abstract class containing pure virtual functions (with the purity indicated by = 0), just like that. Like Java's interfaces, abstract classes can't be instantiated directly, but must be derived from by concrete classes which override and implement the pure virtual functions.

There are a couple of issues:

  • the keyword to introduce a class is class not Class
  • you'll want to take the IPerson parameter by reference, IPerson &, not by value; abstract classes can't be passed by value. The same probably applies to the Ward argument; even if it's not abstract, you probably want this class to refer to a ward (as the Java version would), not copy one. Unlike Java, passing a class object to a function will pass a copy of the object, unless you specifically request that it's passed by reference.
Sign up to request clarification or add additional context in comments.

Comments

0

A Java interface provides methods that must be overridden by classes that inherit from that interface. This is akin to having an abstract C++ base class that pure virtual methods, like the one you've shown. So yes, that is the correct implementation. Also note that in base classes should have virtual destructors so that deleting a base class pointer to a derived instance correctly calls the derived and base class destructors.

1 Comment

So then, in the Hospital class I can use: code class Hospital : IHospital code I've only done Java in college so trying to keep myself busy now by rewriting some assignments in C++ and then maybe Ruby
0

Yes, this is as close as you can get to Java's concept of interfaces.

Note that C++ doesn't actually have a concept of an interface as a separate thing from a class. However, it is customary for C++ developers to refer to classes that have no data members and whose methods are all pure virtual as interfaces -- this is a distinction made by programmers, not the language itself.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.