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

More cpp test, don't need cpp file.

parent f21013a5
__doc__ = u"""
>>> test_new_del()
>>> test_rect_area(3, 4)
12
12.0
>>> 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:
float area()
cdef cppclass Circle(Shape):
int radius
__init__(int)
cdef cppclass Rectangle(Shape):
int width
int height
__init__(int, int)
cdef cppclass Square(Shape):
cdef cppclass Square(Rectangle):
int side
__init__(int)
# __init__(int) # need function overloading
def test_new_del():
cdef Rectangle *rect = new Rectangle(10, 20)
cdef Square *sqr = new Square(15)
del rect, sqr
cdef Circle *circ = new Circle(15)
del rect, circ
def test_rect_area(w, h):
cdef Rectangle *rect = new Rectangle(w, h)
......@@ -33,7 +37,7 @@ def test_rect_area(w, h):
del rect
def test_square_area(w):
cdef Square *sqr = new Square(w)
cdef Square *sqr = new Square(w, w)
cdef Rectangle *rect = sqr
try:
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 @@
#define SHAPES_H
namespace shapes {
class Shape
{
public:
virtual float area() = 0;
virtual ~Shape() { }
};
class Rectangle : public Shape
{
public:
Rectangle(int width, int height);
Rectangle(int width, int height)
{
this->width = width;
this->height = height;
}
float area() { return width * height; }
int width;
int height;
};
class Square : public Shape
class Square : public Rectangle
{
public:
Square(int side);
float area() { return side * side; }
Square(int side) : Rectangle(side, side) { this->side = side; }
/* need until function overloading in Cython */
Square(int side, int ignored) : Rectangle(side, side) { this->side = side; }
int side;
};
class Circle : public Shape {
public:
Circle(int radius) { this->radius = radius; }
float area() { return 3.1415926535897931f * radius; }
int radius;
};
}
#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