What Is a CPP Interface?

Object-oriented programming languages such as C++ ("C Plus Plus" or CPP) follow a paradigm of programming geared towards reusing code and building structural hierarchies in that code. Developers create classes to encapsulate data in functionality into logical pieces. They also do this because certain techniques in OOP, such as inheritance, allow the building of more complex classes from simpler ones. Developers can also control how classes are implemented by using design template classes called "interfaces" or "abstract classes" that determine how other programmers use those classes.

  1. Objects and Inheritance

    • When creating classes and objects for a program in C++, the programmer often discovers that different classes will overlap or share similar functionality. She then has two choices: write the functional code for both classes, or create a "base" class with all the shared traits and features of those classes. Then, the two classes can "inherent" those traits while adding their own. For example, if the programmer writes "Associate" and "Manager" classes for a business app, she might create a single "Employee" class that contains the common functionality and between associates and managers.

    Virtual Functions

    • Base class for any other class can contain definitions about what classes can do. and what information they contain. So, the Employee class from the example might contain a function to calculate salary that contains a specific formula. The Manager and Associate classes inherent that function, and thus the formula. However, managers and associates might require different formula to calculate salary. In this case, the base class would contain a "virtual" function. The base class would define a default behavior, but the child classes could then create their own formulas and functions if need be.

    Abstract Classes

    • Alongside virtual functions, there are "pure" virtual functions. Pure virtual functions do not contain a function definition. Rather, they simply define a function name. So, for example, the salary calculation function in the base class Employee would be declared as virtual without a definition. Then, the Manager and Associate classes would define their own functions to calculate salary. When a class contains one or more purely virtual functions, it is called an abstract class. This is because classes with pure virtual functions cannot become objects.

    C++ Interfaces

    • Since abstract classes cannot become objects, they are called "interfaces." Interfaces are abstract classes, often containing only pure virtual functions that serve as a template for other common classes. So if the Employee example contains only pure virtual functions, then the programmer designs the Manager and Associate classes to inherit from Employee as an interface, meaning they must implement the pure virtual functions of the Employee class. While the ISO C++ standard does not contain a specific "interface" keyword, Microsoft's implementation of C++ does (Source 3). Here is an example to illustrate this relationship.
      class Employee{

      public:
      virtual int calcSalary() = 0; //pure virtual function

      private:
      int ID;
      };

      class Associate : public Employee{

      public:
      int calcSalary(){ //Associate *must* implement calcSalary or get an error
      /*code*/
      }
      };

Related Searches:

References

Comments

Related Ads

Featured