Gain & Share Knowlege

Tuesday, May 26, 2009

Interface in C++

"Interface: Interface is a group of related methods with empty bodies. As interfaces are implicitly abstract, they cannot be directly instantiated except when instantiated by a class which implements the said interface. The class must implement all of the methods described in the interface, or be an abstract class."

An interface is a contract, a specification that concreteclasses MUST follow. It defines method signatures but cannot have any implementations; the latter must be provided by the classes that implement the interface.

C#,Java differs from C++ in this regard because C++ lacks native language support for interfaces. As a C++ programmers if you want to make interface in C++ you have to create an abstract class with all its method as pure virtual function.

Example of Interface in C++:
=========================
class Student
{
public:

void display(){}=0; //Pure Virtual Function
void setdata(){}=0; //Pure Virtual Function

};

-------------------------------------------

class StudentImplement:public Student
{
public:

void display()
{

cout<<"Display Logic";

}
void setdata()
{
cout<<"Setdata Logic";
}
};

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]



<< Home