리그캣의 개발놀이터

소프트웨어 설계 - Abstract class 예제 본문

프로그래밍 기본/소프트웨어 설계

소프트웨어 설계 - Abstract class 예제

리그캣 2018. 1. 25. 19:35

Abstract class

추상클래스 확장성이 뛰어나다. figure가 가지고 있는 공통 특징을 물려줄수 있다. 기본을 가지고 확장.

Abstract Operation

implemention


ex

// figure.cpp : 콘솔 응용 프로그램에 대한 진입점을 정의합니다.

//


#include "stdafx.h"

#include <iostream>


using namespace std;


class Figure {

int color;

public:

void setColor(int  c) { color = c; }

virtual void move(int dx, int dy) = 0; //abstract op

virtual void draw() = 0;

Figure(int c) {

color = c;

cout << "Figure::Figure(int)" << endl;

}

virtual ~Figure() { cout << "Figure::~Figure()\n "; }

};



class Point {

int x, y;

public:

void move(int dx, int dy) {

x += dx;

y += dy;

cout << "Point::move(int,int)\n";

}

Point(int xp, int yp) {

x = xp;

y = yp;

cout << "Point::Point(int, int)\n";

}

~Point() { cout << "Point::~Point()\n"; }

};


class Circle : public Figure {

Point center;

int radius;

public:

void move(int dx, int dy) {

center.move(dx, dy);

cout << "Circle::move(int,int)\n";

}

void draw() { cout << "Circle::draw()\n"; }

void setRadius(int r) { radius = r; }

Circle(int x, int y, int r, int c) : Figure(c), center(x, y) {

radius = r;

cout << "Circle::Circle(int,int,int,int)\n";

}

~Circle() { cout << "Circle::~Circle()\n"; }

};


class Rectangle : public Figure {

Point topleft;

int width, height;

public:

void move(int dx, int dy) {

topleft.move(dx, dy);

cout << "Rectangle::move(int,int)\n";

}

void draw() { cout << "Rectangle::draw()\n"; }

void setWidth(int w) { width = w;}

void setHeight(int h) { height = h; }

Rectangle(int x, int y, int w, int h, int c) : Figure(c), topleft(x, y) {

width = w;

height = h;

cout << "Rectangle::Rectangle(int,int,int,int,int)\n";

}

~Rectangle() { cout << "Rectangle::~Rectangle()\n"; }

};


int main()

{

Figure *f[10];

int num = 4;

f[0] = new Circle(100, 100, 50, 2);

f[1] = new Circle(200, 200, 30, 2);

f[2] = new Rectangle(10, 10, 50, 40, 3);

f[3] = new Rectangle(100, 100, 70, 30, 4);


int dx = 10, dy = 20, i;

for (i = 0; i < num; i++) f[i]->move(dx, dy);

for (i = 0; i < num; i++) f[i]->draw();

for (i = 0; i < num; i++) delete f[i];


   return 0;

}


Comments