Solved

What is the difference between keywords class and interface.

Posted on 2004-04-22
8
918 Views
Last Modified: 2013-12-14
Hello,

I have been looking for information regarding the keywords class and interface when using C++. I have inherited a dll that has declared classes with the keyword interface, but cannot find the reason why. Any info would be appreciated.

Thanks in advance, Brad
0
Comment
Question by:bradj_eng
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 10895757
If you are referring to MS compilers, 'struct' and 'interface' are interchangeable in the way they handle regular C++. And, from a C++ point of view, a 'struct' is a 'class' with all members defaulting to 'public'.
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10896067
With respect to pure C++

AFAIK, there is no keyword as Interface
There is only a keyword called class

class/struct is a mechanism to define new user defined data types (ADT)
The public member functions of the class comprise the Interface of the class


Interface is a word mostly used with respect to COM/DCOM related technologies
Again, it specifies the public member functions of the class

Amit

0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10896149
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 1

Expert Comment

by:ravenscr98
ID: 10896291

Interfaces are a feature of Java, VB .NET and C#.  As the name implies, they define interfaces to functions, but not implementations.  They are typically just a set of method prototypes, though Java allows symbolic constants.  Classes are then able to implement the interface, which means they define all methods specified in the interface definition.

For example, consider the interface (not language specific)

interface ISearch{
  boolean search(int i);
}

and the class

class WidgetList implements ISearch{

// ....

  boolean search(int i){
    // define the method
  }
}

which implements the interface.  Notice that the interface only lists the prototype.  The implementation is defined in the class.  Now, an object of any class that implements the interface can be accessed by a reference to the interface.

WidgetList w = new WidgetList();

// use w

ISearch s = w;     // s is a reference to an object that implements ISearch

if (s.search(13))  {
  // ....
}

This code is able to search the object referenced by s without knowing the actual class of s, as long as the object provides the service specified in the interface ISearch.  You can also implement functions whose arguments are references to ISearch and they will accept a reference to any object that implements ISearch.

Java and C# both allow classes to implement mulltiple interfaces and allow new interfaces to be built from existing interfaces.

Unfortunately, C++ does not support interfaces directly.  However, the same result can be achieved using abstract classes that contain only pure virtual functions.

class ISearch{
  public:
    virtual boolean search(int i) = 0;
};

class WidgetList: public ISearch{

  // ....

  public:
    boolean search(int i){
      // define it
   }
};

In this case, you can use an ISearch * to point to a WidgetList object and access the search method.  Because COM/ActiveX and .NET rely heavily on interfaces, Microsoft has provided extensions to C++ to use them.  The keyword __interface is used to define an interface.  You specify the prototypes and the compiler treats them as public, pure virtual functions.  You can then inherit from them.

In the MSDN, search for "__interface keyword" or "__interface" .  Or, look for the topics

Visual Studio .NET : Developing with Visual Studio .NET : Reference : Language References : Language Equivalents : Keywords Compared in Different Languages

and

Visual Studio .NET : Visual C++ : Visual C++ Reference : C/C++ Languages : C++ Language Reference : Derived Classes : Explicit Overides : __interface   .

On the web, go to

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vsintro7/html/vxoriLanguageEquivalentsKeywords.asp

or

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclang/html/vcrefinterface.asp .

What does this all mean for your C++ program and the interface in the dll.  Assuming that you are working in .NET, you can inherit from it as long as you define all methods defined in the interface.  For COM, you'll need more expertise than I can provide.
0
 
LVL 1

Expert Comment

by:ravenscr98
ID: 10896300
Sys_Prog:

Sorry for repeating some of what you said.  You submitted while I was typing mine up and I did not refresh before I submitted.
0
 
LVL 12

Accepted Solution

by:
stefan73 earned 250 total points
ID: 10897456
Hi bradj_eng,
> keyword interface
In connection to DLLs, this is used to declare a DLL function which is visible from outside (that is, callable).

Older versions of gcc also support the #pragma interface for class definitions to avoid multiple creation of the same code for inline members, but that's probably not what you mean.

Cheers,
Stefan
0
 
LVL 86

Expert Comment

by:jkr
ID: 10899247
>>Interfaces are a feature of Java, VB .NET and C#.  

That is obviously wrong. Interfaces are a programming concept.
0
 
LVL 1

Expert Comment

by:ravenscr98
ID: 10901569
Sorry, it is not wrong, just a matter of semantics.  Interfaces are a programming concept and in particular are a concept of Object-Oriented programming languages.  In terms of actual Object-Oriented languages, Java, VB .NET, and C# all provide an interface mechanism and use the "interface" keyword to implement it.

COM also uses the concept of an interface to specify a set of services provided by a binary object, but not to specify the implementation of those services.  This idea comes directly from the Object-Oriented programming model.  However, since many COM aware languages do not have explicit interface mechanisms, they have to rely on other techniques to implement the interface.

Since the original question was about the use of the keyword "interface" in C++, my answer is correct that Java, VB .NET, and C# all provide interfaces with a language defined construct.  On the other hand, the C++ definition does not include an interface construct or keyword, but they can be implemented using pure virtual functions.  Microsoft does provide the __interface keyword as an extension to simplify their use in C++ with COM and .NET.
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question