Solved

What is the difference between keywords class and interface.

Posted on 2004-04-22
8
916 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Hide carret in RichEdit 4 57
How to debug Angular JS in Visual Studio 2013 2 105
C++ :Change value from  DisableCMD registry 4 44
Issues with C++ Class 19 78
Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
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 viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

932 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

11 Experts available now in Live!

Get 1:1 Help Now