for Robot Artificial Inteligence

17. Polymorphic Pointer

|

Exercise (Polymorphic Pointer)

Main.cpp

#include "Rectangle.h"
#include "Circle.h"
#include <iostream>

using namespace std;

void introduceShape(Shape*);
double calculateShapeArea(Shape*);
double calculateShapeCircumferece(Shape*);

int main()
{

    Rectangle rectangle1(5,2);
    Circle circle1(3);

    introduceShape(&rectangle1);
    cout << "My area is: " << calculateShapeArea(&rectangle1) << ", my circumference is: " << calculateShapeCircumferece(&rectangle1) << endl << endl;

    introduceShape(&circle1);
    cout << "My area is: " << calculateShapeArea(&circle1) << ", my circumference is: " << calculateShapeCircumferece(&circle1) << endl;

    return 0;
}

void introduceShape(Shape* shapeToIntroduce)
{
    return shapeToIntroduce->introduce();
}

double calculateShapeArea(Shape* shapeToCalculate)
{
    return shapeToCalculate->calculateArea();
}

double calculateShapeCircumferece(Shape* shapeToCalculate)
{
    return shapeToCalculate->calculateCircumference();
}

Rectangle.h

#ifndef RECTANGLE_H_INCLUDED
#define RECTANGLE_H_INCLUDED
#include "Shape.h"

class Rectangle: public Shape
{
    double width;
    double height;
public:
    Rectangle(double, double);
    void introduce();
    double calculateArea();
    double calculateCircumference();

};


#endif // RECTANGLE_H_INCLUDED

circle.h

#ifndef CIRCLE_H_INCLUDED
#define CIRCLE_H_INCLUDED
#include "Shape.h"

class Circle: public Shape
{
    double radius;
public:
    Circle(double);
    void introduce();
    double calculateArea();
    double calculateCircumference();
};


#endif // CIRCLE_H_INCLUDED

Rectangle.cpp

#include "Rectangle.h"
#include <iostream>

Rectangle::Rectangle(double width, double height)
{
    this->width = width;
    this->height = height;
}
void Rectangle::introduce()
{
    std::cout << "I AM A RECTANGLE !" << std::endl;
}
double Rectangle::calculateArea()
{
    return width*height;
}
double Rectangle::calculateCircumference()
{
    return 2*(width+height);
}

Circle.cpp

#include "Circle.h"
#include <iostream>
#define PI 3.14

Circle::Circle(double radius)
{
    this->radius = radius;
}
void Circle::introduce()
{
    std::cout << "I AM A CIRCLE !" << std::endl;
}
double Circle::calculateArea()
{
    return PI*radius*radius;
}
double Circle::calculateCircumference()
{
    return 2*PI*radius;
}

Exercise 2 (Virtual destructor)

  • 동적 함수로 직사각형과, 원을 Instance 한다.
  • 그렇게 되면 rectangle과 circlel은 Shape 클래스에 포인터 함수가 되여 동적으로 사용 할 수 있다.
  • 사용 후 Delete 될때 shape.h에서 가상 destructor도 종료가 되면서 모든 값이 초기화 된다.

main.cpp

#include "Rectangle.h"
#include "Circle.h"
#include <iostream>

using namespace std;

void introduceShape(Shape*);
double calculateShapeArea(Shape*);
double calculateShapeCircumferece(Shape*);

int main()
{
    Shape* rectangle1 = new Rectangle(5,2);
    Shape* circle1 = new Circle(3);

    introduceShape(rectangle1);
    cout << "My area is: " << calculateShapeArea(rectangle1) << ", my circumference is: " << calculateShapeCircumferece(rectangle1) << endl << endl;

    introduceShape(circle1);
    cout << "My area is: " << calculateShapeArea(circle1) << ", my circumference is: " << calculateShapeCircumferece(circle1) << endl << endl;

    delete rectangle1;
    delete circle1;
    return 0;
}

void introduceShape(Shape* shapeToIntroduce)
{
    return shapeToIntroduce->introduce();
}

double calculateShapeArea(Shape* shapeToCalculate)
{
    return shapeToCalculate->calculateArea();
}

double calculateShapeCircumferece(Shape* shapeToCalculate)
{
    return shapeToCalculate->calculateCircumference();
}

shape.h

#ifndef SHAPE_H_INCLUDED
#define SHAPE_H_INCLUDED
#include <iostream>

class Shape
{
    public:
        virtual ~Shape() {std::cout << "Shape destructor !" << std::endl;};
        virtual void introduce() = 0;
        virtual double calculateArea() = 0;
        virtual double calculateCircumference() = 0;
};

#endif // SHAPE_H_INCLUDED

rectangle.h

#ifndef RECTANGLE_H_INCLUDED
#define RECTANGLE_H_INCLUDED
#include "Shape.h"

class Rectangle: public Shape
{
    double width;
    double height;
public:
    Rectangle(double, double);
    ~Rectangle();

    void introduce();
    double calculateArea();
    double calculateCircumference();
};


#endif // RECTANGLE_H_INCLUDED

circle.h

#ifndef CIRCLE_H_INCLUDED
#define CIRCLE_H_INCLUDED
#include "Shape.h"

class Circle: public Shape
{
    double radius;
public:
    Circle(double);
    ~Circle();

    void introduce();
    double calculateArea();
    double calculateCircumference();
};


#endif // CIRCLE_H_INCLUDED

circle.cpp

#include "Circle.h"
#include <iostream>
#define PI 3.14

Circle::Circle(double radius)
{
    this->radius = radius;
}

Circle::~Circle()
{
    std::cout << "Circle destructor !" << std::endl;
}

void Circle::introduce()
{
    std::cout << "I AM A CIRCLE !" << std::endl;
}

double Circle::calculateArea()
{
    return PI*radius*radius;
}

double Circle::calculateCircumference()
{
    return 2*PI*radius;
}

rectangle.cpp

#include "Rectangle.h"
#include <iostream>

Rectangle::Rectangle(double width, double height)
{
    this->width = width;
    this->height = height;
}

Rectangle::~Rectangle()
{
    std::cout << "Rectangle destructor !" << std::endl;
}

void Rectangle::introduce()
{
    std::cout << "I AM A RECTANGLE !" << std::endl;
}
double Rectangle::calculateArea()
{
    return width*height;
}
double Rectangle::calculateCircumference()
{
    return 2*(width+height);
}

결과값

Comments