Skip to content

Instantly share code, notes, and snippets.

@gcjyzdd
Last active October 17, 2019 07:39
Show Gist options
  • Select an option

  • Save gcjyzdd/7facddc327baeb3ca89c13bcc0182cda to your computer and use it in GitHub Desktop.

Select an option

Save gcjyzdd/7facddc327baeb3ca89c13bcc0182cda to your computer and use it in GitHub Desktop.
#include "Rectangle.h"
#include "cwrapper.h"
#ifdef __cplusplus
extern "C"{
#endif
WRectangle* Rectangle_create(int l, int w){
//calling the constructor and casting the pointer to WRectangle
return reinterpret_cast<WRectangle*>( new Rectangle(l,w) );
}
void Rectangle_destroy(WRectangle* self){
//calling the destructor
delete reinterpret_cast<Rectangle*>(self);
}
int Rectangle_area(WRectangle* self){
return reinterpret_cast<Rectangle*>(self)->area();
}
int Rectangle_perimeter(WRectangle* self){
return reinterpret_cast<Rectangle*>(self)->perimeter();
}
#ifdef __cplusplus
}
#endif
#ifdef __cplusplus
extern "C"{
#endif
/* Wrapper struct to hold a pointer to
Rectangle object in C */
struct WRectangle;
typedef struct WRectangle WRectangle;
/* Wrapper for the constructor */
WRectangle* Rectangle_create(int l, int w);
/* Wrapper for destructor */
void Rectangle_destroy(WRectangle* self);
/* Wrapper for method area */
int Rectangle_area(WRectangle* self);
/* Wrapper for method perimeter */
int Rectangle_perimeter(WRectangle* self);
#ifdef __cplusplus
}
#endif
class Rectangle{
private:
int length;
int width;
public:
Rectangle(int l, int w): length(l), width(w){};
int area(){ return length*width;};
perimeter() { return 2*(lendth+width);};
~Rectangle(){};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment