欢迎来到麦多课文档分享! | 帮助中心 海量文档,免费浏览,给你所需,享你所想!
麦多课文档分享
全部分类
  • 标准规范>
  • 教学课件>
  • 考试资料>
  • 办公文档>
  • 学术论文>
  • 行业资料>
  • 易语言源码>
  • ImageVerifierCode 换一换
    首页 麦多课文档分享 > 资源分类 > PPT文档下载
    分享到微信 分享到微博 分享到QQ空间

    Chapter 14Graph class design.ppt

    • 资源ID:379590       资源大小:626KB        全文页数:33页
    • 资源格式: PPT        下载积分:2000积分
    快捷下载 游客一键下载
    账号登录下载
    微信登录下载
    二维码
    微信扫一扫登录
    下载资源需要2000积分(如需开发票,请勿充值!)
    邮箱/手机:
    温馨提示:
    如需开发票,请勿充值!快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
    如需开发票,请勿充值!如填写123,账号就是123,密码也是123。
    支付方式: 支付宝扫码支付    微信扫码支付   
    验证码:   换一换

    加入VIP,交流精品资源
     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    Chapter 14Graph class design.ppt

    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

    20、pending on the type of shape / e.g. Text, Circle, Closed_polyline/ reset the color and style to their old values ,16,Stroustrup/Programming,Class Shape (implementation of drawing),void Shape:draw() const/ The real heart of class Shape (and of our graphics interface system)/ called by Window (only) F

    21、l_Color oldc = fl_color(); / save old color/ there is no good portable way of retrieving the current style (sigh!)fl_color(line_color.as_int(); / set color and stylefl_line_style(ls.style(),ls.width();draw_lines(); / call the appropriate draw_lines() / a “virtual call”/ here is what is specific for

    22、a “derived class” is donefl_color(oldc); / reset color to previous fl_line_style(0); / (re)set style to default ,17,Note!,Stroustrup/Programming,Class shape,In class Shape virtual void draw_lines() const; / draw the appropriate lines In class Circle void draw_lines() const /* draw the Circle */ In c

    23、lass Text void draw_lines() const /* draw the Text */ Circle, Text, and other classes “Derive from” Shape May “override” draw_lines(),18,Stroustrup/Programming,class Shape / deals with color and style, and holds a sequence of lines public:void draw() const; / deal with color and call draw_lines()vir

    24、tual void move(int dx, int dy); / move the shape +=dx and +=dyvoid set_color(Color col); / color accessint color() const;/ style and fill_color access functions Point point(int i) const; / (read-only) access to pointsint number_of_points() const; protected:Shape(); / protected to make class Shape ab

    25、stractvoid add(Point p); / add p to points virtual void draw_lines() const; / simply draw the appropriate lines private:vector points; / not used by all shapesColor lcolor; / line colorLine_style ls; / line styleColor fcolor; / fill color/ prevent copying ;,19,Stroustrup/Programming,Display model co

    26、mpleted,20,attach(),Stroustrup/Programming,Language mechanisms,Most popular definition of object-oriented programming: OOP = inheritance + polymorphism + encapsulationBase and derived classes / inheritance struct Circle : Shape ; Also called “inheritance”Virtual functions / polymorphism virtual void

    27、 draw_lines() const; Also called “run-time polymorphism” or “dynamic dispatch”Private and protected / encapsulation protected: Shape(); private: vector points;,21,Stroustrup/Programming,A simple class hierarchy,We chose to use a simple (and mostly shallow) class hierarchy Based on Shape,22,Rectangle

    28、,Image,Stroustrup/Programming,Object layout,The data members of a derived class are simply added at the end of its base class (a Circle is a Shape with a radius),23,Stroustrup/Programming,Benefits of inheritance,Interface inheritance A function expecting a shape (a Shape&) can accept any object of a

    29、 class derived from Shape. Simplifies use sometimes dramatically We can add classes derived from Shape to a program without rewriting user code Adding without touching old code is one of the “holy grails” of programming Implementation inheritance Simplifies implementation of derived classes Common f

    30、unctionality can be provided in one place Changes can be done in one place and have universal effect Another “holy grail”,24,Stroustrup/Programming,Access model,A member (data, function, or type member) or a base can be Private, protected, or public,25,Stroustrup/Programming,Pure virtual functions,O

    31、ften, a function in an interface cant be implemented E.g. the data needed is “hidden” in the derived class We must ensure that a derived class implements that function Make it a “pure virtual function” (=0) This is how we define truly abstract interfaces (“pure interfaces”)struct Engine / interface

    32、to electric motors/ no data/ (usually) no constructorvirtual double increase(int i) =0; / must be defined in a derived class/ virtual Engine(); / (usually) a virtual destructor ; Engine eee; / error: Collection is an abstract class,Stroustrup/Programming,26,Pure virtual functions,A pure interface ca

    33、n then be used as a base class Constructors and destructors will be describe d in detail in chapters 17-19Class M123 : public Engine / engine model M123/ representation public:M123(); / construtor: initialization, acquire resourcesdouble increase(int i) /* */ / overrides Engine :increase/ M123(); /

    34、destructor: cleanup, release resources ;M123 window3_control; / OK,Stroustrup/Programming,27,Technicality: Copying,If you dont know how to copy an object, prevent copying Abstract classes typically should not be copiedclass Shape / Shape(const Shape / error: no Shape “copy assignment” (its deleted)

    35、,Stroustrup/Programming,28,Prevent copying C+98 style,If you dont know how to copy an object, prevent copying Abstract classes typically should not be copiedclass Shape / private:Shape(const Shape / error: no Shape “copy assignment” (its private) ,Stroustrup/Programming,29,Technicality: Overriding,T

    36、o override a virtual function, you need A virtual function Exactly the same name Exactly the same typestruct B void f1(); / not virtualvirtual void f2(char);virtual void f3(char) const;virtual void f4(int); ;,Stroustrup/Programming,30,struct D : B void f1(); / doesnt overridevoid f2(int); / doesnt o

    37、verridevoid f3(char); / doesnt overridevoid f4(int); / overrides ;,Technicality: Overriding,To override a virtual function, you need A virtual function Exactly the same name Exactly the same typestruct B void f1(); / not virtualvirtual void f2(char);virtual void f3(char) const;virtual void f4(int);

    38、;,Stroustrup/Programming,31,struct D : B void f1() override; / errorvoid f2(int) override; / errorvoid f3(char) override; / errorvoid f4(int) override; / OK ;,Technicality: Overriding,To invoke a virtual function, you need A reference, or A pointerD d1; B / calls D:f4(2) on d1 since bptr points to a D,Stroustrup/Programming,32,Next lecture,Graphing functions and data,33,Stroustrup/Programming,


    注意事项

    本文(Chapter 14Graph class design.ppt)为本站会员(appealoxygen216)主动上传,麦多课文档分享仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知麦多课文档分享(点击联系客服),我们立即给予删除!




    关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

    copyright@ 2008-2019 麦多课文库(www.mydoc123.com)网站版权所有
    备案/许可证编号:苏ICP备17064731号-1 

    收起
    展开