Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

What is the difference between keywords class and interface.

Posted on 2004-04-22
8
Medium Priority
?
929 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 1000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
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 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.

916 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