//shape.h
#ifndef SHAPE__H
#define SHAPE__H
#include
#include
static const float PI=3.14159;
class Shape{
public:
Shape():name_("Shape"),width_(0),height_(0),originx_(0),originy_(0){};
~Shape(){
std::cout<<"~Shape() called\n";
}
public:
private:
};
class Circle: public Shape{
public:
Circle(float r): Shape(2*r,2*r,0,0,"Circle"), radius(r){
std::cout<<" Circle() called\n";
}
~Circle(){
std::cout<<" ~Circle() called\n";
}
float getarea(){
return PI*radius*radius;
}
float getperimeter(){
return 2*PI*radius;
}
private:
};
class Rectangle : public Shape{
public:
~Rectangle(){
std::cout<<" ~Rectangle() called\n";
}
};
class Square: public Rectangle{
public:
Square(float w): Rectangle(w,w,"Square") {
std::cout<<" Square() called\n";
}
~Square(){
std::cout<<" ~Square() called\n";
}
};
#endif
//shape.cpp
#include "shape.h"
#include
#include
#include
using namespace std;
void test_1(){
cout<<"test_1 begin\n";
Shape* papers[5];
Circle michael(15);
Rectangle andrew(34.0,2.0);
Circle ann(12.5);
Square tim(9.99);
Rectangle jonathan(12.95,11.25);
papers[0] = &michael;
papers[1] = &andrew;
papers[2] = &ann;
papers[3] = &tim;
papers[4] = &jonathan;
for(int i=0;i<5;i++) {
cout << "Shape " << setw(10) << papers[i]->getname()<<" area:" << setw(10) << papers[i]->getarea() << " perimeter:"<< setw(10)<< papers[i]->getperimeter()<< endl;
}
cout<<"test_1 end\n";
}
/* test_1 output
test_1 begin
Shape() called
Circle() called
Shape() called
Rectangle() called
Shape() called
Circle() called
Shape() called
Rectangle() called
Square() called
Shape() called
Rectangle() called
Shape Circle area: 706.858 perimeter: 94.2477
Shape Rectangle area: 68 perimeter: 72
Shape Circle area: 490.873 perimeter: 78.5397
Shape Square area: 99.8001 perimeter: 39.96
Shape Rectangle area: 145.688 perimeter: 48.4
test_1 end
~Rectangle() called
~Shape() called
~Square() called
~Rectangle() called
~Shape() called
~Circle() called
~Shape() called
~Rectangle() called
~Shape() called
~Circle() called
~Shape() called
*/
void test_2(){
cout<<"test_2 begin\n";
Shape* papers[5];
papers[0] = new Circle(15.0f);
papers[1] = new Rectangle(34.0f,2.0f);
papers[2] = new Circle(12.5f);
papers[3] = new Square(9.99f);
papers[4] = new Rectangle(12.95f,11.25f);
for(int i=0;i<5;i++) {
cout << "Shape " << setw(10) << papers[i]->getname()<<" area:" << setw(10) << papers[i]->getarea() << " perimeter:"<< setw(10)<< papers[i]->getperimeter()<< endl;
}
for(int i=0;i<5;i++)
delete papers[i];
cout<<"test_2 end\n";
}
/* test_2: output
test_2 begin
Shape() called
Circle() called
Shape() called
Rectangle() called
Shape() called
Circle() called
Shape() called
Rectangle() called
Square() called
Shape() called
Rectangle() called
Shape Circle area: 706.858 perimeter: 94.2477
Shape Rectangle area: 68 perimeter: 72
Shape Circle area: 490.873 perimeter: 78.5397
Shape Square area: 99.8001 perimeter: 39.96
Shape Rectangle area: 145.688 perimeter: 48.4
~Shape() called
~Shape() called
~Shape() called
~Shape() called
~Shape() called
test_2 end
*/
int main(){
test_1();
test_2();
return 0;
}