Skip to content

Instantly share code, notes, and snippets.

@Jules-Baratoux
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jules-Baratoux/0e4c64a183468a1750bf to your computer and use it in GitHub Desktop.
Save Jules-Baratoux/0e4c64a183468a1750bf to your computer and use it in GitHub Desktop.
Homework #5 – Shape Hierarchy
#include <cstdlib>
#include "Shapes.h"
using JulesBaratoux::Circle;
using JulesBaratoux::Square;
using JulesBaratoux::Sphere;
using JulesBaratoux::Cube;
using JulesBaratoux::Shape;
void tests(void)
{
Circle::tests::constructor();
Circle::tests::getArea();
Circle::tests::display();
Square::tests::constructor();
Square::tests::getArea();
Square::tests::display();
Sphere::tests::constructor();
Sphere::tests::getSurfaceArea();
Sphere::tests::getVolume();
Sphere::tests::display();
Cube::tests::constructor();
Cube::tests::getSurfaceArea();
Cube::tests::getVolume();
Cube::tests::display();
}
int main(void)
{
Shape* shapes[] =
{
new Circle(42),
new Square(42),
new Sphere(42),
new Cube(42)
};
for (size_t i = 0; i < sizeof(shapes) / sizeof(*shapes); ++i)
{
shapes[i]->display();
delete shapes[i];
}
return EXIT_SUCCESS;
}
#include <cassert>
#include <cmath>
#include <string>
using std::string;
#include <iostream>
using std::ostream;
using std::endl;
#include <sstream>
using std::ostringstream;
#include "Shapes.h"
using JulesBaratoux::ShapeBase;
using JulesBaratoux::Circle;
using JulesBaratoux::Square;
using JulesBaratoux::Sphere;
using JulesBaratoux::Cube;
// ----------------------------------------------------------------------------
// ShapeBase
// ----------------------------------------------------------------------------
void ShapeBase::display(void) const
{
return display(std::cout);
}
// ----------------------------------------------------------------------------
// Circle
// ----------------------------------------------------------------------------
Circle::Circle(double radius)
: radius(radius)
{}
void Circle::tests::constructor(void)
{
assert( Circle(2).radius == 2 );
}
// ----------------------------------------------------------------------------
void Circle::display(ostream& out) const
{
out << "Circle with radius "<< radius << " has area " << getArea() << endl;
}
void Circle::tests::display(void)
{
ostringstream oss;
Circle(2).display(oss);
assert(oss.str() == "Circle with radius 2 has area 12.5664\n");
}
// ----------------------------------------------------------------------------
double Circle::getArea(void) const
{
return PI * pow(radius, 2.0);
}
void Circle::tests::getArea(void)
{
assert(Circle(2).getArea() == 12.566370614359172);
}
// ----------------------------------------------------------------------------
// Square
// ----------------------------------------------------------------------------
Square::Square(double lengthOfSide)
: lengthOfSide(lengthOfSide)
{}
void Square::tests::constructor(void)
{
assert( Square(2).lengthOfSide == 2.0 );
}
// ----------------------------------------------------------------------------
void Square::display(ostream& out) const
{
out << "Square with length of side "<< lengthOfSide << " has area " << getArea() << endl;
}
void Square::tests::display(void)
{
ostringstream oss;
Square(3).display(oss);
assert(oss.str() == "Square with length of side 3 has area 9\n");
}
// ----------------------------------------------------------------------------
double Square::getArea(void) const
{
return pow(lengthOfSide, 2.0);
}
void Square::tests::getArea(void)
{
assert(Square(3).getArea() == 9.0);
}
// ----------------------------------------------------------------------------
// Sphere
// ----------------------------------------------------------------------------
Sphere::Sphere(double radius)
: radius(radius)
{}
void Sphere::tests::constructor(void)
{
assert( Sphere(2).radius == 2.0 );
}
// ----------------------------------------------------------------------------
void Sphere::display(ostream& out) const
{
out << "Sphere with radius " << radius
<< " has surface area " << getSurfaceArea()
<< " and volume " << getVolume()
<< endl;
}
void Sphere::tests::display(void)
{
ostringstream oss;
Sphere(4).display(oss);
assert( oss.str() ==
"Sphere with radius 4 has surface area 201.062 and volume 268.083\n");
}
// ----------------------------------------------------------------------------
double Sphere::getSurfaceArea(void) const
{
return 4.0 * PI * pow(radius, 2.0);
}
void Sphere::tests::getSurfaceArea(void)
{
assert( Sphere(4).getSurfaceArea() == 201.06192982974676 );
}
// ----------------------------------------------------------------------------
double Sphere::getVolume(void) const
{
return (4.0 / 3.0) * PI * pow(radius, 3.0);
}
void Sphere::tests::getVolume(void)
{
assert( Sphere(4).getVolume() == 268.08257310632899 );
}
// ----------------------------------------------------------------------------
// Cube
// ----------------------------------------------------------------------------
Cube::Cube(double lengthOfSide)
: lengthOfSide(lengthOfSide)
{}
void Cube::tests::constructor(void)
{
assert( Cube(2).lengthOfSide == 2.0 );
}
// ----------------------------------------------------------------------------
void Cube::display(ostream& out) const
{
out << "Cube with length of side " << lengthOfSide
<< " has surface area " << getSurfaceArea()
<< " and volume " << getVolume()
<< endl;
}
void Cube::tests::display(void)
{
ostringstream oss;
Cube(5).display(oss);
assert( oss.str() ==
"Cube with length of side 5 has surface area 150 and volume 125\n" );
}
// ----------------------------------------------------------------------------
double Cube::getSurfaceArea(void) const
{
return 6.0 * pow(lengthOfSide, 2.0);
}
void Cube::tests::getSurfaceArea(void)
{
assert( Cube(5).getSurfaceArea() == 150.0 );
}
// ----------------------------------------------------------------------------
double Cube::getVolume(void) const
{
return pow(lengthOfSide, 3.0);
}
void Cube::tests::getVolume(void)
{
assert( Cube(5).getVolume() == 125.0 );
}
#pragma once
#include <ostream>
using std::ostream;
namespace JulesBaratoux
{
// The math library normally defines M_PI. But if strict ISO and/or POSIX
// compliance are requested this constant is not defined.
const double PI = 3.14159265358979323846;
struct Shape
{
virtual void display(void) const = 0;
virtual ~Shape(void) {}
};
struct ShapeBase : public Shape
{
void display(void) const;
virtual void display(ostream& out) const = 0;
virtual ~ShapeBase(void) {}
};
struct TwoDimensionalShape : public ShapeBase
{
virtual double getArea(void) const = 0;
virtual ~TwoDimensionalShape(void) {}
};
struct ThreeDimensionalShape : public ShapeBase
{
virtual double getSurfaceArea(void) const = 0;
virtual double getVolume(void) const = 0;
virtual ~ThreeDimensionalShape(void) {}
};
struct Circle : public TwoDimensionalShape
{
explicit Circle(double radius);
void display(ostream& out) const;
double getArea(void) const;
struct tests
{
static void constructor(void);
static void display(void);
static void getArea(void);
};
private:
double radius;
friend struct tests;
virtual ~Circle(void) {}
};
struct Square : public TwoDimensionalShape
{
Square(double lengthOfSide);
void display(ostream& out) const;
double getArea(void) const;
struct tests
{
static void constructor(void);
static void display(void);
static void getArea(void);
};
private:
double lengthOfSide;
friend struct tests;
virtual ~Square(void) {}
};
struct Sphere : public ThreeDimensionalShape
{
explicit Sphere(double radius);
void display(ostream& out) const;
double getSurfaceArea(void) const;
double getVolume(void) const;
struct tests
{
static void constructor(void);
static void display(void);
static void getSurfaceArea(void);
static void getVolume(void);
};
private:
double radius;
friend struct tests;
virtual ~Sphere(void) {}
};
struct Cube : public ThreeDimensionalShape
{
explicit Cube(double radius);
void display(ostream& out) const;
double getSurfaceArea(void) const;
double getVolume(void) const;
struct tests
{
static void constructor(void);
static void display(void);
static void getSurfaceArea(void);
static void getVolume(void);
};
private:
double lengthOfSide;
friend struct tests;
virtual ~Cube(void) {}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment