# #define t_rectangle 2

Λογισμικό & κατασκευή λογ/κού

18 Νοε 2013 (πριν από 4 χρόνια και 6 μήνες)

90 εμφανίσεις

Class 15
-

Object Oriented Programming Using C

#define t_circle 1

#define t_rectangle 2

struct circle_shape

{

short type;

double x,y;

};

struct rectangle_shape

{

short type;

double x1, y1;

double x2, y2;

};

typedef union SHAPE

{

circle_shape circle;

rectangle_shape rectangle;

}SHAPE;

double compute_area(SHAPE *p_shape);

Class 15
-

Object Oriented Programming Using C

int main( )

{

int i;

SHAPE s[2];

s[0] .type=t_rectangle;

s[0] .rectangle.x1=80.0;

s[0] .rectangle.y1=30.0;

s[0] .rectangle.x2=120.0;

s[0] .rectangle.y2=50.0;

s[1] .type=t_circle;

s[1] .circle.x=200.0;

s[1] .circle.y=100.0;

for (i=0;i<2;i++)

cout << “area of shape”

<< i << “=”

<< compute_area(& s [i]);

return 0;

}

Class 15
-

Object Oriented Programming Using C

double compute_area(SHAPE *p_shape)

{

double area = 0;

switch (p_shape
-
>type)

{

case t_circle: area=3.14 *p_shape
-

*p_shape
-

break;

case t_rectangle: area=(p_shape
-
>rectangle.x2
-

p_shape

-
>rectangle .x1)*

(p_shape
-
>rectangle.y2
-

p_shape

-
>rectangle.y1;

break;

default: cout << “unknown shape”;

}

return (area);

}

Class 15
-

Object Oriented Programming Using C

c
lass shape

{ public:

virtual double
compute_area
(void) const =0;

}

class
circle_shape:public

shape

{

private:

double
x,y
;

public:

circle_shape
(double x, double y, double radius);

virtual double
compute_area
(void) const;

};

Class 15
-

Object Oriented Programming Using C

class
rectangle_shape
: public shape

{

private:

double x1, y1;

double x2, y2;

public:

rectangle_shape

(double x1, double y1, double 2x, double y2);

double
compute_area
(void) const;

};

Class 15
-

Object Oriented Programming Using C

circle_shape::circle_shape (double xc, double yc, double r)

{

x=xc;

y=yc;

}

double circle_shape::compute_area(void) const

{

}

rectangle_shape::rectangle_shape(double xul, double yul,

double xlr, double ylr)

{

x1=xul;

y1= yul;

x2=xlr;

y2=ylr;

}

double rectangle_shape::compute_area(void) const

{

return ((x1
-
x2) * (y1
-
y2));

}

Class 15
-

Object Oriented Programming Using C

int main(void)

{

int i;

shape *shapes[3];

shapes[0] = new circle_shape (200., 100., 50.);

shapes[1]=new rectangle
-
shape(80.0, 30.0, 120.0, 50.0)

for (i=0; i < 2; i++)

cout << “area of shape” << i

<< shapes [i]
-
> compute_area( );

delete shapes[0];

delete shapes[1];

return[0];

}