#include <iostream>
#include <cmath>
#include <typeinfo>
#include <exception>
using namespace std;

using namespace std;

// Abstract base class.
class Shape {
 public:
  // Virtual functions
  virtual double area() { return 0; }
  virtual double perimeter() { return 0; }
  virtual void doubleArea() { /* do nothing */ }
  virtual Shape* clone() const = 0;
  virtual void copy(const Shape&r) = 0; 
};

// Derived class.
class Circle: public Shape {
 private:
  double radius;

 public:
  Circle (double r) : radius(r) {}
  double area() { return (M_PI*pow(radius,2)); }
  double perimeter() { return (M_PI*2*radius); }
  void doubleArea() { radius *= pow(2,0.5); }
  Circle* clone() const { return new Circle(*this); }
  void copy(const Shape&r) override {
  	if (dynamic_cast<const Circle*>(&r)) 
  	    *this = *dynamic_cast<const Circle*>(&r);
  	else throw (invalid_argument("ouch! circle copy mismatch"));
  }
  
};

void modShape(Shape &inShape) {

  // Make new Shape* from clone of inShape
  // and double its area.
  Shape* newShape = inShape.clone();
  newShape->doubleArea();
  cout << "newShape's area (after doubling): " << newShape->area() << endl;

  // Copy newShape to inShape.
  inShape.copy(*newShape);
  cout << "newShape copied to inShape (circ)." << endl;
  cout << "inShape's area in modShape: " << inShape.area() << endl;

};


int main() {

  Circle circ(2);
  cout << "circ's initial area (in main): " << circ.area() << endl;
  modShape(circ);
  cout << "circ's final area (in main): " << circ.area() << endl;

  return 0;
}