Presentation is loading. Please wait.

Presentation is loading. Please wait.

Class 15 - Overhead 11Object Oriented Programming Using C #define t_circle 1 #define t_rectangle 2 struct circle_shape {short type; double x,y; double.

Similar presentations


Presentation on theme: "Class 15 - Overhead 11Object Oriented Programming Using C #define t_circle 1 #define t_rectangle 2 struct circle_shape {short type; double x,y; double."— Presentation transcript:

1 Class 15 - Overhead 11Object Oriented Programming Using C #define t_circle 1 #define t_rectangle 2 struct circle_shape {short type; double x,y; double radius; }; 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);

2 Class 15 - Overhead 12Object 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; s[1].circle.radius=50.9; for (i=0;i<2;i++) cout << “area of shape” << i << “=” << compute_area(& s [i]); return 0; }

3 Class 15 - Overhead 13Object 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->circle.radius *p_shape->circle.radius: 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); }

4 Class 15 - Overhead 14Object Oriented Programming Using C class shape { public: virtual double compute_area(void) const =0; } class circle_shape:public shape {private: double x,y; double radius; public: circle_shape(double x, double y, double radius); virtual double compute_area(void) const; };

5 Class 15 - Overhead 14Object 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; };

6 Class 15 - Overhead 15Object Oriented Programming Using C circle_shape::circle_shape (double xc, double yc, double r) {x=xc; y=yc; radius = r; } double circle_shape::compute_area(void) const {return (3.14 * radius * radius); } 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));}

7 Class 15 - Overhead 16Object 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 compute_area( ); delete shapes[0]; delete shapes[1]; return[0]; }


Download ppt "Class 15 - Overhead 11Object Oriented Programming Using C #define t_circle 1 #define t_rectangle 2 struct circle_shape {short type; double x,y; double."

Similar presentations


Ads by Google