C++
                
              Shape (RTTI : Run Time Type Identification)
                호타리
                 2023. 9. 7. 15:40
              
                          
            <main.cpp>
#include <iostream>
#include "shape.h"
#include "rectangle.h"
#include "circle.h"
#include <typeinfo>
void printArea(const Shape *ps)
{
	std::cout << "area : " << ps->area() << std::endl;
}
void printShape(const Shape *ps)
{
	if (typeid(*ps) == typeid(Rectangle))          // RTTI : Run Time Type Identification
	{
		std::cout << "rectangle area : " << ps->area() << '\t';
		const Rectangle *p = (const Rectangle *)ps;
		std::cout << "diagonal : " << p->getDiagonal() << std::endl;
	}
	else if (typeid(*ps) == typeid(Circle))
	{
		std::cout << "circle area : " << ps->area() << '\t';
		const Circle *p = (const Circle *)ps;
		std::cout << "circumference : " << p->getCircumference() << 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)
	{
		printShape(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 결과>
