Chapter 14 - Chapter 14 Chapter Graph class design Bjarne...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Chapter 14 Chapter Graph class design Bjarne Stroustrup Abstract Abstract We have discussed classes in previous lectures Here, we discuss design of classes Library design considerations Class hierarchies (object-oriented programming) Data hiding Data Stroustrup/Programming 2 Ideals Ideals Our ideal of program design is to represent the Our concepts of the application domain directly in code. If you understand the application domain, you understand the code, and vice versa. For example: vice Window – a window as presented by the operating system Line – a line as you see it on the screen Point – a coordinate point Color – as you see it on the screen Shape – what’s common for all shapes in our Graph/GUI view of the world of The last example, Shape, is different from the rest The Shape is in that it is a generalization. in You can’t make an object that’s “just a Shape” Stroustrup/Programming 3 Logically identical operations have the same name same For every class, draw_lines() does the drawing move(dx,dy) does the moving s.add(x) adds some x (e.g., a point) to a shape s. point) For every property x of a Shape, For x() gives its current value and set_x() gives it a new value e.g., Color c = s.color(); s.set_color(Color::blue); Stroustrup/Programming 4 Logically different operations have Logically different names Lines ln; Point p1(100,200); Point p2(200,300); ln.add(p1,p2); win.attach(ln); Why not win.add(ln)? Why win.add(ln) p1: p2: // add points to ln (make copies) // add // attach ln to window // attach add() copies information; attach() just creates a reference attach() we can change a displayed object after attaching it, but not after adding it attach() (100,200) win: ln: &ln (200,300) add() (100,200) (200,300) Stroustrup/Programming 5 Expose uniformly Expose Data should be private Data hiding – so it will not be changed inadvertently Use private data, and pairs of public access functions to get and set the Use data data c.set_radius(12); // set radius to 12 // set c.set_radius(c.radius()*2); // double the radius (fine) // double c.set_radius(-9); // set_radius() could check for negative, // set_radius() // but doesn’t yet double r = c.radius(); // returns value of radius double // returns c.radius = -9; // error: radius is a function (good!) // error: c.r = -9; // error: radius is private (good!) // error: Our functions can be private or public Public for interface Private for functions used only internally to a class Stroustrup/Programming 6 What does private/protected buy us? What We can change our implementation after release We don’t expose FLTK types used in representation to our users We could provide checking in access functions E.g., s.add(x) rather than s.points.push_back(x) E.g., s.add(x) s.points.push_back(x) We enforce immutability of shape But we haven’t done so systematically (later?) Functional interfaces can be nicer to read and use We could replace FLTK with another library without affecting user code Only color and style change; not the relative position of points const member functions The value of this “encapsulation” varies with application domains Is often most valuable Is the ideal i.e., hide representation unless you have a good reason not to Stroustrup/Programming 7 “Regular” interfaces Line ln(Point(100,200),Point(300,400)); Mark m(Point(100,200), 'x'); // display a single point as an 'x' // display Circle c(Point(200,200),250); // Alternative (not supported): Alternative Line ln2(x1, y1, x2, y2); // from (x1,y1) to (x2,y2) // from // How about? (not supported): How Square s1(Point(100,200),200,300); // width==200 height==300 width==200 Square s2(Point(100,200),Point(200,300)); // width==100 height==100 // width==100 Square s3(100,200,200,300); // is 200,300 a point or a width plus a height? Square is Stroustrup/Programming 8 A library library A collection of classes and functions meant to be used collection together together A good library models some aspect of a domain good As building blocks for applications To build more such “building blocks” It doesn’t try to do everything Our library aims at simplicity and small size for graphing data and for Our very simple GUI We can’t define each library class and function in isolation A good library exhibits a uniform style (“regularity”) Stroustrup/Programming 9 Class Shape Class All our shapes are “based on” the Shape class E.g., a Polygon is a kind of Shape E.g., Polygon Shape Shape Circle Text Ellipse Line Open_polyline Image Axis Lines Marked_polyline Closed_polyline Function Rectangle Marks Polygon Mark Stroustrup/Programming 10 Class Shape – is abstract is You can’t make a “plain” Shape protected: Shape(); // protected to make class Shape abstract // protected For example Shape ss; // error: cannot construct Shape error: Protected means “can only be used from a derived class” Instead, we use Shape as a base class struct Circle : Shape { // … // }; }; // “a Circle is a Shape” // “a Stroustrup/Programming 11 11 Class Shape Class Shape ties our graphics objects to “the screen” Shape is the class that deals with color and style Window “knows about” Shapes Shape All our graphics objects are kinds of Shapes All Shape It has Color and Line_style members It Color Line_style Shape can hold Points Point Shape has a basic notion of how to draw lines It just connects its Points It Point Stroustrup/Programming 12 Class Shape Class Shape deals with color and style It keeps its data private and provides access functions void set_color(Color col); int color() const; void set_style(Line_style sty); Line_style style() const; // … // private: // … // Color line_color; Line_style ls; Stroustrup/Programming 13 Class Shape Class Shape stores Points Point It keeps its data private and provides access functions Point point(int i) const; // read-only access to points // read-only int number_of_points() const; // … // protected: void add(Point p); // add p to points void // add // … private: vector<Point> points; // not used by all shapes // not Stroustrup/Programming 14 Class Shape Class Shape itself can access points directly: void Shape::draw_lines() const // draw connecting lines // draw { if (color().visible() && 1<points.size()) for (int i=1; i<points.size(); ++i) fl_line(points[i-1].x,points[i-1].y,points[i].x,points[i].y); } Others (incl. derived classes) use point() and number_of_points() point() number_of_points() Others why? void Lines::draw_lines() const // draw a line for each pair of points // draw { for (int i=1; i<number_of_points(); i+=2) fl_line(point(i-1).x, point(i-1).y, point(i).x, point(i).y); } Stroustrup/Programming 15 Class Shape (basic idea of drawing) (basic void Shape::draw() const // The real heart of class Shape (and of our graphics interface system) The // called by Window (only) called { // … save old color and style … // save // … set color and style for this shape… // set // … draw what is specific for this particular shape … // … Note: this varies dramatically depending on the type of shape … // … e.g. Text, Circle, Closed_polyline // … reset the color and style to their old values … // reset } Stroustrup/Programming 16 Class Shape (implementation of drawing) (implementation void Shape::draw() const // The real heart of class Shape (and of our graphics interface system) The // called by Window (only) called { Fl_Color oldc = fl_color(); // save old color // save // there is no good portable way of retrieving the current style (sigh!) // there fl_color(line_color.as_int()); // set color and style // set fl_line_style(,ls.width()); Note! draw_lines(); // call the appropriate draw_lines() // // a “virtual call” // “virtual // here is what is specific for a “derived class” is done fl_color(oldc); fl_line_style(0); fl_line_style(0); // reset color to previous // // (re)set style to default // (re)set } Stroustrup/Programming 17 Class shape Class In class Shape In Shape virtual void draw_lines() const; virtual // draw the appropriate lines draw In class Circle In Circle void draw_lines() const { /* draw the Circle */ } void draw In class Text In Text void draw_lines() const { /* draw the Text */ } void */ Circle, Text, and other classes Text “Derive from” Shape Derive Shape May “override” draw_lines() May draw_lines() Stroustrup/Programming 18 class Shape { // deals with color and style, and holds a sequence of lines class // deals public: public: void draw() const; // deal with color and call draw_lines() // deal virtual void move(int dx, int dy); // move the shape +=dx and +=dy virtual move void set_color(Color col); // color access // color int color() const; // … style and fill_color access functions … // style Point point(int i) const; // (read-only) access to points // access int number_of_points() const; protected: Shape(); // protected to make class Shape abstract // protected void add(Point p); // add p to points // add virtual void draw_lines() const; // simply draw the appropriate lines simply private: vector<Point> points; // not used by all shapes // not Color lcolor; // line color // line Line_style ls; // line style // line Color fcolor; // fill color // fill // … prevent copying … }; Stroustrup/Programming 19 Display model completed Display draw_lines() Shape draw() Circle draw_lines() draw() Display Engine Window draw_lines() Shape draw() attach() wait_for_button() Text draw_lines() our code make objects Stroustrup/Programming 20 Language mechanisms Language Most popular definition of object-oriented programming: Most OOP == inheritance + polymorphism + encapsulation OOP Base and derived classes struct Circle : Shape { … }; Also called “inheritance” Virtual functions // polymorphism // polymorphism virtual void draw_lines() const; Also called “run-time polymorphism” or “dynamic dispatch” Private and protected // inheritance // inheritance // encapsulation // encapsulation protected: Shape(); private: vector<Point> points; Stroustrup/Programming 21 A simple class hierarchy simple We chose to use a simple (and mostly shallow) class hierarchy Based on Shape Shape Circle Text Ellipse Line Open_polyline Image Axis Lines Marked_polyline Closed_polyline Function Rectangle Marks Polygon Mark Stroustrup/Programming 22 Object layout Object The data members of a derived class are simply added at The the end of its base class (a Circle is a Shape with a radius) the Shape: Circle: points line_color ls points line_color ls ---------------------r Stroustrup/Programming Stroustrup/Programming 23 Benefits of inheritance Benefits Interface inheritance A function expecting a shape (a Shape&) can accept function Shape& can any object of a class derived from Shape. any Simplifies use We can add classes derived from Shape to a program We without rewriting user code without sometimes dramatically Adding without touching old code is one of the “holy grails” Adding of programming of Implementation inheritance Simplifies implementation of derived classes Common functionality can be provided in one place Changes can be done in one place and have universal effect Another “holy grail” Stroustrup/Programming 24 Access model Access A member (data, function, or type member) or a base can be Private, protected, or public Stroustrup/Programming 25 Next lecture Next Graphing functions and data Stroustrup/Programming 26 ...
View Full Document

Ask a homework question - tutors are online