1 / 20

Introduction to Effective C++ Programming

Introduction to Effective C++ Programming. Kwanghee Ko Design Laboratory Department of Ocean Engineering Massachusetts Institute of Technology Day 2. Abstraction : Design of a Class. Show how abstraction is made in a class design with an example. Example

maire
Télécharger la présentation

Introduction to Effective C++ Programming

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. Introduction to Effective C++ Programming Kwanghee Ko Design Laboratory Department of Ocean Engineering Massachusetts Institute of Technology Day 2

  2. Abstraction : Design of a Class • Show how abstraction is made in a class design with an example. • Example • Determine the price of various configurations of a computer • Expansions : CD-ROM drive, Floppy drive, Network card • Monitor : CRT, LCD

  3. Abstraction : Design of a Class class Card { public: virtual int price() = 0; virtual char *name() = 0; virtual int rebate(); }; • First Design class Network : public Card { public: int price(); char *name(); }; class CDRom : public Card { public: int price(); char *name(); int rebate(); }; class Floppy : public Card { public: int price(); char *name(); };

  4. Abstraction : Design of a Class class Monitor { public: virtual int price() = 0; virtual char *name() = 0; }; • First Design class CRT : public Monitor { public: int price(); char *name(); }; class LCD : public Monitor { public: int price(); char *name(); };

  5. Abstraction : Design of a Class • Class inheritance hierarchies Monitor Card Network CDRom Floppy CRT LCD

  6. Abstraction : Design of a Class class Card { public: virtual int price() = 0; virtual char *name() = 0; virtual int rebate(); }; • Find common abstractions and concentrate them in a base class. class Network : public Card { public: int price(); char *name(); }; class CDRom : public Card { public: int price(); char *name(); int rebate(); }; class Floppy : public Card { public: int price(); char *name(); };

  7. Abstraction : Design of a Class class Monitor { public: virtual int price() = 0; virtual char *name() = 0; }; • Find common abstractions and concentrate them in a base class. class CRT : public Monitor { public: int price(); char *name(); }; class LCD : public Monitor { public: int price(); char *name(); };

  8. Abstraction : Design of a Class class Card { public: virtual int price() = 0; virtual char *name() = 0; virtual int rebate(); }; class Monitor { public: virtual int price() = 0; virtual char *name() = 0; };

  9. Abstraction : Design of a Class class Component { public: virtual int price() = 0; virtual char *name() = 0; virtual int rebate(); int netPrice(); }; int Component::rebate() { return 0; } class Card { public: virtual int price() = 0; virtual char *name() = 0; virtual int rebate(); }; class Monitor { public: virtual int price() = 0; virtual char *name() = 0; };

  10. Abstraction : Design of a Class • Class inheritance hierarchies Component Monitor Card Network CDRom Floppy CRT LCD

  11. Abstraction : Design of a Class • What are the differences between classes? (only within this design) Component Monitor Card Network CDRom Floppy CRT LCD

  12. Abstraction : Design of a Class • What are the differences between classes? (only within this design) Price Name Amount of rebate Component Monitor Card Network CDRom Floppy CRT LCD

  13. Abstraction : Design of a Class • What are the differences between classes? (only within this design) Price Name Amount of rebate Component Variation in Value? Variation in Behavior? Monitor Card Network CDRom Floppy CRT LCD

  14. Abstraction : Design of a Class • What are the differences between classes? (only within this design) Variation in Value Use data members Component Variation in behavior Use virtual functions Monitor Card Network CDRom Floppy CRT LCD

  15. Abstraction : Design of a Class • Careful observation reveals that each component can be represented by different values of data members (price, name, rebate). class Component { int price_; char *name_; int rebate_; public: Component(int p,char *n, int r=0); int netPrice() int price() {return price_;} char *name() {return name_;} int rebate() {return rebate_;} };

  16. Abstraction : Design of a Class • Careful observation reveals that each component can be represented by different values of data members (price, name, rebate). class Component { int price_; char *name_; int rebate_; public: Component(int p,char *n, int r=0); int netPrice() int price() {return price_;} char *name() {return name_;} int rebate() {return rebate_;} };

  17. Abstraction : Design of a Class • Treatment of Nonzero Rebates • We need to reintroduce the classes Card and Monitor to provide constructors with appropriate rebate defaults other than zero. Component Monitor Card

  18. Abstraction : Design of a Class • A public derived class should be a specialization of its base class class Card : public Component { public: Card(int p, char *n, int r=45):Component(p,n,r) {} }; Different default rebates class Monitor : public Component { public: Monitor(int p, char *n, int r=0):Component(p,n,r) {} };

  19. General Guidelines • Design class interfaces that are complete and minimal. • Decide smartly between member, non-member and friend functions. • Avoid data members in the public interface. • Use ‘const’ as much as you can. • Prefer pass-by-reference to pass-by-value

  20. General Guidelines • Don’t try to return a reference when you must return an object. • Function overloading vs. parameter defaulting. • Explicitly disallow use of implicitly generated member functions you don’t want. • Use namespaces.

More Related