Solved

What is the difference between keywords class and interface.

Posted on 2004-04-22
8
913 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
Comment Utility
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
Comment Utility
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
Comment Utility
0
 
LVL 1

Expert Comment

by:ravenscr98
Comment Utility

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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 1

Expert Comment

by:ravenscr98
Comment Utility
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
Comment Utility
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
Comment Utility
>>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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
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…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

772 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now