Further cleaning up of class examples
This commit is contained in:
parent
7f40ae3570
commit
0de4cf13a8
29 changed files with 150 additions and 216 deletions
|
|
@ -1,46 +1,28 @@
|
|||
/* File : example.c */
|
||||
/* File : example.cxx */
|
||||
|
||||
#include "example.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#define M_PI 3.14159265358979323846
|
||||
|
||||
// Static member initializer
|
||||
int Shape::nshapes = 0;
|
||||
|
||||
// Constructor
|
||||
Shape::Shape() {
|
||||
nshapes++;
|
||||
}
|
||||
|
||||
// Move the shape to a new location
|
||||
/* Move the shape to a new location */
|
||||
void Shape::move(double dx, double dy) {
|
||||
x += dx;
|
||||
y += dy;
|
||||
}
|
||||
|
||||
// Destructor
|
||||
Shape::~Shape() {
|
||||
nshapes--;
|
||||
}
|
||||
int Shape::nshapes = 0;
|
||||
|
||||
// Circle area
|
||||
double Circle::area() const {
|
||||
double Circle::area() {
|
||||
return M_PI*radius*radius;
|
||||
}
|
||||
|
||||
// Circle perimeter
|
||||
double Circle::perimeter() const {
|
||||
double Circle::perimeter() {
|
||||
return 2*M_PI*radius;
|
||||
}
|
||||
|
||||
// Square area
|
||||
double Square::area() const {
|
||||
double Square::area() {
|
||||
return width*width;
|
||||
}
|
||||
|
||||
// Square perimeter
|
||||
double Square::perimeter() const {
|
||||
double Square::perimeter() {
|
||||
return 4*width;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,12 +2,16 @@
|
|||
|
||||
class Shape {
|
||||
public:
|
||||
Shape();
|
||||
virtual ~Shape();
|
||||
double x, y;
|
||||
Shape() {
|
||||
nshapes++;
|
||||
}
|
||||
virtual ~Shape() {
|
||||
nshapes--;
|
||||
}
|
||||
double x, y;
|
||||
void move(double dx, double dy);
|
||||
virtual double area() const = 0;
|
||||
virtual double perimeter() const = 0;
|
||||
virtual double area() = 0;
|
||||
virtual double perimeter() = 0;
|
||||
static int nshapes;
|
||||
};
|
||||
|
||||
|
|
@ -15,21 +19,16 @@ class Circle : public Shape {
|
|||
private:
|
||||
double radius;
|
||||
public:
|
||||
Circle(double r) : radius(r) { };
|
||||
virtual double area() const;
|
||||
virtual double perimeter() const;
|
||||
Circle(double r) : radius(r) { }
|
||||
virtual double area();
|
||||
virtual double perimeter();
|
||||
};
|
||||
|
||||
class Square : public Shape {
|
||||
private:
|
||||
double width;
|
||||
public:
|
||||
Square(double w) : width(w) { };
|
||||
virtual double area() const;
|
||||
virtual double perimeter() const;
|
||||
Square(double w) : width(w) { }
|
||||
virtual double area();
|
||||
virtual double perimeter();
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -7,4 +7,3 @@
|
|||
|
||||
/* Let's just grab the original header file here */
|
||||
%include "example.h"
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue