Commit 67164f05 authored by Robert Bradshaw's avatar Robert Bradshaw

More cpp test, don't need cpp file.

parent f21013a5
__doc__ = u""" __doc__ = u"""
>>> test_new_del() >>> test_new_del()
>>> test_rect_area(3, 4) >>> test_rect_area(3, 4)
12 12.0
>>> test_square_area(15) >>> test_square_area(15)
225 (225.0, 225.0)
""" """
cdef extern from "shapes.cpp" namespace shapes: cdef extern from "shapes.h" namespace shapes:
cdef cppclass Shape: cdef cppclass Shape:
float area() float area()
cdef cppclass Circle(Shape):
int radius
__init__(int)
cdef cppclass Rectangle(Shape): cdef cppclass Rectangle(Shape):
int width int width
int height int height
__init__(int, int) __init__(int, int)
cdef cppclass Square(Shape): cdef cppclass Square(Rectangle):
int side int side
__init__(int) # __init__(int) # need function overloading
def test_new_del(): def test_new_del():
cdef Rectangle *rect = new Rectangle(10, 20) cdef Rectangle *rect = new Rectangle(10, 20)
cdef Square *sqr = new Square(15) cdef Circle *circ = new Circle(15)
del rect, sqr del rect, circ
def test_rect_area(w, h): def test_rect_area(w, h):
cdef Rectangle *rect = new Rectangle(w, h) cdef Rectangle *rect = new Rectangle(w, h)
...@@ -33,7 +37,7 @@ def test_rect_area(w, h): ...@@ -33,7 +37,7 @@ def test_rect_area(w, h):
del rect del rect
def test_square_area(w): def test_square_area(w):
cdef Square *sqr = new Square(w) cdef Square *sqr = new Square(w, w)
cdef Rectangle *rect = sqr cdef Rectangle *rect = sqr
try: try:
return rect.area(), sqr.area() return rect.area(), sqr.area()
......
#include "shapes.h"
using namespace shapes;
Rectangle::Rectangle(int width, int height)
{
this->width = width;
this->height = height;
}
Square::Square(int side)
{
this->side = side;
}
...@@ -2,30 +2,43 @@ ...@@ -2,30 +2,43 @@
#define SHAPES_H #define SHAPES_H
namespace shapes { namespace shapes {
class Shape class Shape
{ {
public: public:
virtual float area() = 0; virtual float area() = 0;
virtual ~Shape() { } virtual ~Shape() { }
}; };
class Rectangle : public Shape class Rectangle : public Shape
{ {
public: public:
Rectangle(int width, int height); Rectangle(int width, int height)
{
this->width = width;
this->height = height;
}
float area() { return width * height; } float area() { return width * height; }
int width; int width;
int height; int height;
}; };
class Square : public Shape class Square : public Rectangle
{ {
public: public:
Square(int side); Square(int side) : Rectangle(side, side) { this->side = side; }
float area() { return side * side; } /* need until function overloading in Cython */
Square(int side, int ignored) : Rectangle(side, side) { this->side = side; }
int side; int side;
}; };
class Circle : public Shape {
public:
Circle(int radius) { this->radius = radius; }
float area() { return 3.1415926535897931f * radius; }
int radius;
};
} }
#endif #endif
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment