Chapter 14Graph class design.ppt
《Chapter 14Graph class design.ppt》由会员分享,可在线阅读,更多相关《Chapter 14Graph class design.ppt(33页珍藏版)》请在麦多课文档分享上搜索。
1、Chapter 14 Graph class design,Bjarne S have discussed classes in previous lectures Here, we discuss design of classes Library design considerations Class hierarchies (object-oriented programming) Data hiding,2,Stroustrup/Programming,Ideals,Our ideal of program design is to represent the concepts of
2、the application domain directly in code. If you understand the application domain, you understand the code, and vice versa. For example: 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 whats
3、 common for all shapes in our Graph/GUI view of the world The last example, Shape, is different from the rest in that it is a generalization. You cant make an object thats “just a Shape”,3,Stroustrup/Programming,Logically identical operations have the same name,For every class, draw_lines() does the
4、 drawing move(dx,dy) does the moving s.add(x) adds some x (e.g., a point) to a shape s. For every property x of a Shape, x() gives its current value and set_x() gives it a new value e.g., Color c = s.color(); s.set_color(Color:blue);,4,Stroustrup/Programming,Logically different operations have diffe
5、rent names,Lines ln; Point p1(100,200); Point p2(200,300); ln.add(p1,p2); / add points to ln (make copies) win.attach(ln); / attach ln to windowWhy not win.add(ln)? add() copies information; attach() just creates a reference we can change a displayed object after attaching it, but not after adding i
6、t,5,(100,200),p1:,(200,300),p2:,attach(),add(),Stroustrup/Programming,Expose uniformly,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 data c.set_radius(12); / set radius to 12c.set_radius(c.radius()
7、*2); / double the radius (fine)c.set_radius(-9); / set_radius() could check for negative,/ but doesnt yet double r = c.radius(); / returns value of radiusc.radius = -9; / error: radius is a function (good!)c.r = -9; / error: radius is private (good!)Our functions can be private or public Public for
8、interface Private for functions used only internally to a class,6,Stroustrup/Programming,What does “private” buy us?,We can change our implementation after release We dont expose FLTK types used in representation to our users We could replace FLTK with another library without affecting user code We
9、could provide checking in access functions But we havent done so systematically (later?) Functional interfaces can be nicer to read and use E.g., s.add(x) rather than s.points.push_back(x) We enforce immutability of shape Only color and style change; not the relative position of points const member
10、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,7,Stroustrup/Programming,“Regular” interfaces,Line ln(Point(100,200),Point(300,400); Mark m(Point(100,200), x); / display a s
11、ingle point as an x Circle c(Point(200,200),250);/ Alternative (not supported): Line ln2(x1, y1, x2, y2); / from (x1,y1) to (x2,y2)/ How about? (not supported): Rectangle s1(Point(100,200),200,300); / width=200 height=300 Rectangle s2(Point(100,200),Point(200,300); / width=100 height=100Rectangle s3
12、(100,200,200,300);/ is 200,300 a point or a width plus a height?,8,Stroustrup/Programming,A library,A collection of classes and functions meant to be used together As building blocks for applications To build more such “building blocks”A good library models some aspect of a domain It doesnt try to d
13、o everything Our library aims at simplicity and small size for graphing data and for very simple GUI We cant define each library class and function in isolation A good library exhibits a uniform style (“regularity”),9,Stroustrup/Programming,Class Shape,All our shapes are “based on” the Shape class E
14、.g., a Polygon is a kind of Shape,10,Rectangle,Image,Stroustrup/Programming,Class Shape is abstract,You cant make a “plain” Shape protected:Shape(); / protected to make class Shape abstract For exampleShape ss; / error: cannot construct Shape Protected means “can only be used from this class or from
15、 a derived class” Instead, we use Shape as a base class struct Circle : Shape / “a Circle is a Shape”/ ;,11,Stroustrup/Programming,Class Shape,Shape ties our graphics objects to “the screen” Window “knows about” Shapes All our graphics objects are kinds of Shapes Shape is the class that deals with c
16、olor and style It has Color and Line_style members Shape can hold Points Shape has a basic notion of how to draw lines It just connects its Points,12,Stroustrup/Programming,Class Shape,Shape deals with color and style It keeps its data private and provides access functionsvoid set_color(Color col);C
17、olor color() const;void set_style(Line_style sty);Line_style style() const;/ private:/ Color line_color;Line_style ls;,13,Stroustrup/Programming,Class Shape,Shape stores Points It keeps its data private and provides access functionsPoint point(int i) const; / read-only access to points int number_of
18、_points() const; / protected:void add(Point p); / add p to points/ private: vector points; / not used by all shapes,14,Stroustrup/Programming,Class Shape,Shape itself can access points directly:void Shape:draw_lines() const / draw connecting lines if (color().visible() ,15,Stroustrup/Programming,Cla
19、ss Shape (basic idea of drawing),void Shape:draw() const/ The real heart of class Shape (and of our graphics interface system)/ called by Window (only) / save old color and style / set color and style for this shape/ draw what is specific for this particular shape / Note: this varies dramatically de
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- CHAPTER14GRAPHCLASSDESIGNPPT
