4

look at this code

#include<iostream>

using namespace std;

    //Shape is an Interface Class. No data and everything pure virtual
    class Shape {
    public:
      virtual void Area(int length, int breadth) = 0;
      virtual void Perimeter(int length, int breadth) = 0;
      //Note, no data
    };

    //Derived class - Inherits Shape as Public
    class Rectangle : public Shape {
    public:
      void Area(int length, int breadth);
      void Perimeter(int length, int breadth);
    private:
      int someData;
    };

    //Derived class - Inherits Shape as Public
    class Triangle : public Shape {
    public:
      void Area(int length, int breadth);
      void Perimeter(int length, int breadth);
    private:
      int someData;
    };

    int main()
    {
      Rectangle r;
      Triangle t;

      cout<<"\n\n";
      r.Area(3,4);
      r.Perimeter(3,4);

      t.Area(3,4);
      t.Perimeter(3,4);

      cout<<"\n\n";
      return 0;
    }

    void Rectangle::Area(int length, int breadth)
    {
      cout<<"\nThe Area of Rectangle for length = "<<length<<" and\
      breadth = "<<breadth<<" is "<<(length * breadth)<<endl;
    }

    void Rectangle::Perimeter(int length, int breadth)
    {
      cout<<"\nThe Perimeter of Rectangle for length = "<<length<<" and\
      breadth = "<<breadth<<" is "<<2 * (length + breadth)<<endl;
    }

    void Triangle::Area(int length, int breadth)
    {
      cout<<"\nThe Area of Triangle for length = "<<length<<" and\
      breadth = "<<breadth<<" is "<<(length * breadth)/2<<endl;
    }

    void Triangle::Perimeter(int length, int breadth)
    {
      cout<<"\nThe Perimeter of Triangle for length = "<<length<<" and\
      breadth = "<<breadth<<" is "<<(length * breadth)/3<<endl;
    }

I use interface in the code , but my question is what i should use it and what is the benefits from it , no performance , no real needed it , why i should i use it ( the interfaces ) . what is the point to use it , an you explain it please .

and thank you !

user943998
  • 127
  • 3
  • 10
  • 1
    http://stackoverflow.com/questions/761194/interface-vs-abstract-class-general-oo read this... – Ahmed Masud Dec 10 '11 at 10:54
  • 1
    Aside: `Shape` should have a destructor that is either `virtual` or declared `protected`. Its use is prone to errors otherwise. – CB Bailey Dec 10 '11 at 10:54

5 Answers5

5

Abstract interfaces separate the interface from the implementation. Just as pimpl idiom it

  • decreases compilation time, and
  • lets you change the implementation without breaking the ABI.

Both are important advantages in large programs.

Yakov Galka
  • 61,035
  • 13
  • 128
  • 192
1

An interface could be used to have, say, a vector of different Shape objects. Otherwise you couldn't have a collection that mixes triangles and rectangles for example. Or another class could have a Shape member, which could then either be a triangle or rectangle. These are just some examples.

Edit:

Let me give a concrete example. Say you have an interface called Car. Imagine you want to have a class Garage that has room for a single Car. You've implemented different types of cars, that all use the Car interface. Then the Garage class could be something like:

class Garage {
public:
    Car getCar(); // returns _theCar
private:
    Car _theCar:
}
tobier
  • 584
  • 1
  • 4
  • 18
1

A common mistake when programming C++ (and other object oriented languages) is to use inheritance too much. Interfaces is inheritance done right. The reason is that the strength of interfaces is to be able to handle objects of different type in another system as if they were the same type. Triangle and Circle can both be Shapes for instance and can be passed to a graphics engine for drawing on the screen.

The reason interfaces are 'better' than inheritance that also includes inherited functionality is that it quickly becomes very hard to understand what a class really does, to debug it and verify that the internal state of the objects cannot be destroyed by using the external methods.

The need for this type of structure where you use interfaces more than sporadically is hard to motivate in a small example, but becomes obvious when a projects grows big. Besides making things like I describes above possible they are also good to make it easier to test the program since you can then replace the implementation of part of your program (lets say the database access for instace) with a stubbed implementation and by doing that enable you to write automatic tests that verifies other parts of the program (processing the data for instance)

There are no performance reasons to choose interface over direct access to members, rather the opposite since you will call methods that are virtual. This is however a very minor performance penalty in the majority of cases.

Meros
  • 1,088
  • 10
  • 10
0

Have a look here for more on C++ MI - Why should I avoid multiple inheritance in C++?.

Building up on the "3 Interfaces" section and ybungalobill's answer, consider the typical Observer pattern:

class MyClass : public IScreenListener
{
public:
    MyClass()
    {
        PowerManager::RegisterScreenListener(*this);
    }

    // Overriding from IScreenListener
    void OnScreenOn()
    {
       // do as you like
    }

    void OnScreenOff()
    {
       // do as you like
    }
}

Here, the IScreenListener interface provides the two pure virtual methods OnScreenOff and OnScreenOn which are to be implemented in your code. This example is based on Bada's screen listener: it allows you to get notified by such events.

Of course, there are other benefits. Like hiding a code library implementation details from its users, and more.

Community
  • 1
  • 1
kellogs
  • 2,645
  • 2
  • 33
  • 46
0

Interfaces (Pure Abstract classes) provide general functionality. In your example, the class Shape is generic. Which means you cannot have a actual instance (or object) from the class Shape. Where as you can say a Rectangle is a Shape or a Triangle is a Shape. You cannot calculate Area or Perimeter of a Shape unless you know what Shape it is.

Interfaces (Pure Abstract classes) enforce a protocol that a class that is derived from it must implement all of its methods. Otherwise, it also becomes an interface. Interfaces pointers can be sent to functions or other classes and from there you can call the actual derived classes functionality.

For example, if there is a class called Animal from where you derive all animals like dogs, snakes, humans etc, you can send the array of Animal pointers (which are actually instances of it's derived classes) and then call the functionality like Run(), Walk(), Eat() etc. Animal in this case is generic class.

Murali Krishna
  • 301
  • 1
  • 7