Boolan Homework Week3

shape.h

#ifndef __SHAPE_H__
#define __SHAPE_H__ 

#include <iostream>
#include <iomanip>
using namespace std; 

extern const double PI;
//----------------------------------------------------------------
class Shape {
private:
	int no_;
public:
	Shape(int no = 0): no_(no) { }
	int getNo() const { return no_; }
	virtual int getArea() const = 0;
	virtual void print() const = 0;
	virtual Shape* copy() = 0;
	virtual ~Shape() { } 
};
//----------------------------------------------------------------
class Point {
private:
	int x_;
	int y_;
public:
	Point(int x = 0, int y = 0): x_(x), y_(y) {	}
};
//----------------------------------------------------------------
class Rectangle: public Shape {
private:
	int width_;
	int height_;
	Point leftUp_;
public:
	Rectangle(int no = 0, int width = 0, int height = 0, int x = 0, int y = 0): Shape(no), width_(width), height_(height), leftUp_(Point(x,y)) { }
	virtual int getArea() const { return width_ * height_; }
	virtual void print() const { cout << "Rect No." << setw(2) << setfill('0') << this->getNo() << "\'s area: " << this->getArea() << endl; }
	virtual Shape* copy() { return new Rectangle(*this); }
	virtual ~Rectangle() { };
};
//----------------------------------------------------------------
class Circle: public Shape {
private:
	Point center_;
	int radius_;
public:
	Circle(int no = 0, int radius = 0, int x = 0, int y = 0): Shape(no), radius_(radius), center_(Point(x, y)) { }
	virtual int getArea() const { return PI * radius_ * radius_; } 
	virtual void print() const { cout << "Cir  No." << setw(2) << setfill('0') << this->getNo() << "\'s area: " << this->getArea() << endl; }
	virtual Shape* copy() { return new Circle(*this); }
	virtual ~Circle() { } 
};

#endif  //__SHAPE_H__ 

 

shape_test.cpp

#include "shape.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std; 

const double PI = 3.141592;
Shape** createShape(int cnt);
Shape** filterShape(Shape* myshape[], int cnt, int &ncnt);
void printShape(Shape* myshape[], int cnt);
void deleteData(Shape* myshape[], int cnt);

int main(int argc, char** argv) {
	const int cnt = 20;
	Shape** myshape = createShape(cnt);
	cout << "---------- Before filter ----------" << endl;
	printShape(myshape, cnt);  
	cout << "---------- After filter ----------" << endl;
	int ncnt = 0;
	Shape** filtershape = filterShape(myshape, cnt, ncnt);   
	printShape(filtershape, ncnt);  	
	deleteData(myshape, cnt);
	deleteData(filtershape, ncnt);
	return 0;
}
//----------------------------------------------------------------
Shape** createShape(int cnt) {
	Shape** myshape = new Shape*[cnt];
	time_t t;
	srand((unsigned)time(&t));
	for (int i=0; i<cnt/2; ++i) {
		int w = rand()%10 + 1;
		int h = rand()%10 + 1;
		int x = rand()%10 + 1;
		int y = rand()%10 + 1;
		myshape[i] = new Rectangle(i+1, w, h, x, y);
	}
	for (int i=cnt/2; i<cnt; ++i) {
		int c = rand()%10 + 1;
		int x = rand()%10 + 1;
		int y = rand()%10 + 1;
		myshape[i] = new Circle(i+1, c, x, y);
	}
	return myshape;
}
//----------------------------------------------------------------
Shape** filterShape(Shape* myshape[], int cnt, int &ncnt) {
	Shape** filtershape = new Shape*[cnt];
	for (int i=0; i<cnt; ++i){
		if (myshape[i]->getArea()>=50) {
			filtershape[ncnt++] = myshape[i]->copy();
		} 
	}
	return filtershape;
}
//----------------------------------------------------------------
void printShape(Shape* myshape[], int cnt) {
	for (int i=0; i<cnt; ++i) {
		myshape[i]->print();
	}
}
//----------------------------------------------------------------
void deleteData(Shape* myshape[], int cnt) {
	for (int i=0; i<cnt; ++i) {
		delete myshape[i];
//		myshape[i] = nullptr;  // unnecessary
	}
	delete [] myshape;
	myshape = nullptr;
}

 

posted @ 2017-05-04 21:02  新未  阅读(126)  评论(0)    收藏  举报
回到顶部