draw(); Rectangle* pr = (Rectangle*)ps; ((Rectangle*)ps)->draw(); // 하향 형변환 Shape* pc = new Circle(); pc->draw(); Circle* pd = (Circle*)pc; pd->draw(); ((Circle*)pc)->draw(); delete ps; // delete pr; }"> draw(); Rectangle* pr = (Rectangle*)ps; ((Rectangle*)ps)->draw(); // 하향 형변환 Shape* pc = new Circle(); pc->draw(); Circle* pd = (Circle*)pc; pd->draw(); ((Circle*)pc)->draw(); delete ps; // delete pr; }"> draw(); Rectangle* pr = (Rectangle*)ps; ((Rectangle*)ps)->draw(); // 하향 형변환 Shape* pc = new Circle(); pc->draw(); Circle* pd = (Circle*)pc; pd->draw(); ((Circle*)pc)->draw(); delete ps; // delete pr; }">
#include <iostream>
using namespace std;
class Shape {
protected:
int x, y;
public:
void setOrigin(int x, int y) {
this->x = x;
this->y = y;
}
void draw() {
cout << "Shape Draw" << endl;
}
};
class Rectangle : public Shape {
private:
int width, height;
public:
void setWidth(int w, int h) {
width = w;
height = h;
}
void draw() {
cout << "Rectangle Draw" << endl;
}
};
class Circle : public Shape {
private:
int radius;
public:
void setRadius(int r) {
radius = r;
}
};
int main()
{
Shape* ps = new Rectangle(); // 상향 형변환
ps->draw();
Rectangle* pr = (Rectangle*)ps;
((Rectangle*)ps)->draw(); // 하향 형변환
Shape* pc = new Circle();
pc->draw();
Circle* pd = (Circle*)pc;
pd->draw();
((Circle*)pc)->draw();
delete ps;
// delete pr;
}
#include <iostream>
using namespace std;
class Shape {
protected:
int x, y;
public:
Shape(){
cout << "Shape 생성자" << endl;
}
virtual ~Shape() {
cout << "Shape 소멸자" << endl;
}
void setOrigin(int x, int y) {
this->x = x;
this->y = y;
}
virtual void draw() = 0; // 순수 가상 함수
};
class Rectangle : public Shape {
private:
int width, height;
public:
Rectangle() {
cout << "Rectangle 생성자" << endl;
}
~Rectangle() {
cout << "Rectangle 소멸자" << endl;
}
void setWidth(int w, int h) {
width = w;
height = h;
}
void draw() {
cout << "Rectangle Draw" << endl;
}
};
class Circle : public Shape {
private:
int radius;
public:
void setRadius(int r) {
radius = r;
}
};
int main()
{
Rectangle* r1 = new Rectangle();
delete r1;
}