printf("ho_tari\n");

Shape (Shape, Rectangle, Circle 상속) 본문

C++

Shape (Shape, Rectangle, Circle 상속)

호타리 2023. 9. 7. 15:19

<main.cpp>

#include <iostream>

#include "shape.h"

#include "rectangle.h"

#include "circle.h"



void printArea(const Shape *ps)

{

	std::cout << "area : " << ps->area() << std::endl;

}



int main()

{

	//Shape s(100, 100);

	Shape *ps;



	Shape *pShapes[5];	

	pShapes[0] = new Rectangle(10, 10, 20, 5);

	pShapes[1] = new Circle(50, 50, 5);

	pShapes[2] = new Rectangle(0, 0, 100, 20);

	pShapes[3] = new Rectangle(100, 100, 100, 100);

	pShapes[4] = new Circle(100, 100, 50);

	

	for (int i = 0; i < 5; ++i)

	{

		printArea(pShapes[i]);

	}

	

	for (int i = 0; i < 5; ++i)

	{

		delete pShapes[i];

	}

	

	return 0;

}

<shape.h>

#ifndef SHAPE_H

#define SHAPE_H



class Shape {          // Abstract Base Class (ABC)

protected:

	int x_;

	int y_;

	

	Shape(const Shape& rhs);

	Shape& operator=(const Shape& rhs);



public:

	Shape(int x, int y);

	virtual ~Shape() {}

	

	void move(int offsetX, int offsetY);

	virtual double area() const = 0;             // pure virtual function



};



#endif

<shape.cpp>

#include "shape.h"



Shape::Shape(int x, int y)

: x_(x), y_(y)

{



}



void Shape::move(int offsetX, int offsetY)

{

	x_ = x_ + offsetX;

	y_ = y_ + offsetY;

}



/*

double Shape::area() const

{

	// ??? can't be implemented!!!

}

*/

<rectangle.h>

#ifndef RECTANGLE_H

#define RECTANGLE_H

#include "shape.h"



class Rectangle : public Shape {

private:

	int width_;

	int height_;

	

	Rectangle(const Rectangle& );

	Rectangle& operator=(const Rectangle& );



public:

	Rectangle(int x, int y, int width, int height);

	virtual ~Rectangle() { }

	

	virtual double area() const;

	double getDiagonal() const;



};





#endif

<rectangle.cpp>

#include "rectangle.h"

#include <cmath>



Rectangle::Rectangle(int x, int y, int width, int height)

: Shape(x, y), width_(width), height_(height)

{



}



double Rectangle::area() const

{

	return width_ * height_;

}



double Rectangle::getDiagonal() const

{

	return sqrt(width_ * width_ + height_ * height_);

}

<circle.h>

#ifndef CIRCLE_H

#define CIRCLE_H

#include "shape.h"



class Circle : public Shape {

private:

	int radius_;

	

	Circle(const Circle& );                     // not use

	Circle& operator=(const Circle& );          // not use



public:

	Circle(int x, int y, int radius);

	virtual ~Circle() { }

	

	virtual double area() const;

	

	double getCircumference() const;



};



#endif

<circle.cpp>

#include "circle.h"



Circle::Circle(int x, int y, int radius)

: Shape(x, y), radius_(radius)

{



}



double Circle::area() const 

{

	return 3.141592 * radius_ * radius_;

}



double Circle::getCircumference() const

{

	return 2 * 3.141592 * radius_;         // return 3.141592 * (radius_ + radius_);

}

 

<compile 결과>

'C++' 카테고리의 다른 글

BoundArray (index 지정)  (0) 2023.09.08
Shape (RTTI : Run Time Type Identification)  (0) 2023.09.07
SafeArray2 (상속)  (0) 2023.09.07
SafeArray  (0) 2023.09.07
Queue3 (array 클래스와 has-a 관계)  (0) 2023.09.07