1 / 7

Class 15 - Overhead 11

#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;

early
Download Presentation

Class 15 - Overhead 11

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. #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); Object Oriented Programming Using C Class 15 - Overhead 11

  2. 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; } Object Oriented Programming Using C Class 15 - Overhead 12

  3. 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); } Object Oriented Programming Using C Class 15 - Overhead 13

  4. 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; }; Object Oriented Programming Using C Class 15 - Overhead 14

  5. 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; }; Object Oriented Programming Using C Class 15 - Overhead 14

  6. 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)); } Object Oriented Programming Using C Class 15 - Overhead 15

  7. 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]; } Object Oriented Programming Using C Class 15 - Overhead 16

More Related