Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Effective C++ Programming Kwanghee Ko Design Laboratory Department of Ocean Engineering Massachusetts Institute of Technology Day 2.

Similar presentations


Presentation on theme: "Introduction to Effective C++ Programming Kwanghee Ko Design Laboratory Department of Ocean Engineering Massachusetts Institute of Technology Day 2."— 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 First Design class Card { public: virtual int price() = 0; virtual char *name() = 0; virtual int rebate(); }; 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 First Design class Monitor { public: virtual int price() = 0; virtual char *name() = 0; }; 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 Card NetworkCDRomFloppy Monitor CRTLCD

6 Abstraction : Design of a Class Find common abstractions and concentrate them in a base class. class Card { public: virtual int price() = 0; virtual char *name() = 0; virtual int rebate(); }; 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; }; class CRT : public Monitor { public: int price(); char *name(); }; class LCD : public Monitor { public: int price(); char *name(); }; Find common abstractions and concentrate them in a base class.

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 Card { public: virtual int price() = 0; virtual char *name() = 0; virtual int rebate(); }; class Monitor { public: virtual int price() = 0; virtual char *name() = 0; }; class Component { public: virtual int price() = 0; virtual char *name() = 0; virtual int rebate(); int netPrice(); }; int Component::rebate() { return 0; }

10 Abstraction : Design of a Class Class inheritance hierarchies Card NetworkCDRomFloppy Monitor CRTLCD Component

11 Abstraction : Design of a Class What are the differences between classes? (only within this design) Card NetworkCDRomFloppy Monitor CRTLCD Component

12 Abstraction : Design of a Class What are the differences between classes? (only within this design) Card NetworkCDRomFloppy Monitor CRTLCD ComponentPrice Name Amount of rebate

13 Abstraction : Design of a Class What are the differences between classes? (only within this design) Card NetworkCDRomFloppy Monitor CRTLCD ComponentPrice Name Amount of rebate Variation in Value? Variation in Behavior?

14 Abstraction : Design of a Class What are the differences between classes? (only within this design) Card NetworkCDRomFloppy Monitor CRTLCD ComponentVariation in Value Use data members Variation in behavior Use virtual functions

15 Abstraction : Design of a Class 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_;} }; Careful observation reveals that each component can be represented by different values of data members (price, name, rebate).

16 Abstraction : Design of a Class 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_;} }; Careful observation reveals that each component can be represented by different values of data members (price, name, 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. Card Monitor Component

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

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.


Download ppt "Introduction to Effective C++ Programming Kwanghee Ko Design Laboratory Department of Ocean Engineering Massachusetts Institute of Technology Day 2."

Similar presentations


Ads by Google