gtest

https://avneetkhasla.wordpress.com/2013/04/12/google-c-testing-frameworkgoogle-test-gtest/

http://www.eriksmistad.no/getting-started-with-google-test-on-ubuntu/

http://www.kaizou.org/2014/11/gtest-cmake/

http://alexott.net/en/cpp/CppTestingIntro.html

https://github.com/google/googletest/blob/master/googlemock/README.md

https://github.com/google/googletest/blob/master/googlemock/docs/CheatSheet.md

Click to access post-try-google-unit-test-framework.pdf

Click to access Lab4.pdf

Interface

I assume that with interface you mean a C++ class with only pure virtual methods (i.e. without any code), instead with abstract class you mean a C++ class with virtual methods that can be overridden, and some code, but at least one pure virtual method that makes the class not instantiable. e.g.:

class MyInterface
{
public:
  // Empty virtual destructor for proper cleanup
  virtual ~MyInterface() {}

  virtual void Method1() = 0;
  virtual void Method2() = 0;
};


class MyAbstractClass
{
public:
  virtual ~MyAbstractClass();

  virtual void Method1();
  virtual void Method2();
  void Method3();

  virtual void Method4() = 0; // make MyAbstractClass not instantiable
};

I’d use an interface if I want to define a set of rules using which a component can be programmed, without specifying a concrete particular behavior. Classes that implement this interface will provide some concrete behavior themselves.

Instead, I’d use an abstract class when I want to provide some default infrastructure code and behavior, and make it possible to client code to derive from this abstract class, overriding the pure virtual methods with some custom code, and complete this behavior with custom code.


 

    1. Declare a virtual destructor in your interface or make a protected non-virtual one to avoid undefined behaviours if someone tries to delete an object of type IDemo.

 

  • Use virtual inheritance to avoid problems whith multiple inheritance. (There is more often multiple inheritance when we use interfaces.)

 

 

And like other answers:

    • Make a class with pure virtual methods.

 

  • Use the interface by creating another class that overrides those virtual methods.
    class IDemo
    {
        public:
            virtual void OverrideMe() = 0;
            virtual ~IDemo() {}
    }

    Or

    class IDemo
    {
        public:
            virtual void OverrideMe() = 0;
        protected:
            ~IDemo() {}
    }

    And

    class Child : virtual public IDemo
    {
        public:
            virtual void OverrideMe()
            {
                //do stuff
            }
    }

 

 

Abstract class
An abstract class is, conceptually, a class that cannot be instantiated and is usually implemented as a class that has one or more pure virtual (abstract) functions.

A pure virtual function is one which must be overridden by any concrete (i.e., non-abstract) derived class. This is indicated in the declaration with the syntax ” = 0″ in the member function’s declaration.

1
2
3
4
5
6
7
8
class AbstractClass {
public:
  virtual void AbstractMemberFunction() = 0; // Pure virtual function makes
                                             // this class Abstract class.
  virtual void NonAbstractMemberFunction1(); // Virtual function.
 
  void NonAbstractMemberFunction2();
};

In general an abstract class is used to define an implementation and is intended to be inherited from by concrete classes. It’s a way of forcing a contract between the class designer and the users of that class

Interface
An interface has no implementation.
An interface class contains only a virtual destructor and pure virtual functions.
An interface class is a class that specifies the polymorphic interface i.e. pure virtual function declarations into a base class. The programmer using a class hierarchy can then do so via a base class that communicates only the interface of classes in the hierarchy.

1
2
3
4
5
6
7
8
9
class shape   // An interface class
{
  public:
    virtual ~shape();
    virtual void move_x(int x) = 0;
    virtual void move_y(int y) = 0;
    virtual void draw() = 0;
//...
};

Every interface class should have a virtual destructor. Virtual destructor makes sure that when a shape is deleted polymorphically, correct destructor of the derived class is invoked.

Differences
1 – interfaces can have no state or implementation
2 – a class that implements an interface must provide an implementation of all the method of that interface
3 – abstract classes may contain state (data members) and/or implementation (methods)
4 – abstract classes can be inherited without implementing the abstract methods (though such a derived class is abstract itself)
5 -interfaces may be multiple-inherited, abstract classes may not (this is probably the key concrete reason for interfaces to exist separately from abtract classes – they permit an implementation of multiple inheritance that removes many of the problems of general MI).
6- If you anticipate creating multiple versions of your component, create an abstract class. Abstract classes provide a simple and easy way to version your components. By updating the base class, all inheriting classes are automatically updated with the change. Interfaces, on the other hand, cannot be changed once created. If a new version of an interface is required, you must create a whole new interface.
7-If the functionality you are creating will be useful across a wide range of disparate objects, use an interface. Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing common functionality to unrelated classes.

Multithreaded Work Queue in C++

Multithreaded Work Queue in C++