200 likes | 278 Views
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
E N D
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 • Determine the price of various configurations of a computer • Expansions : CD-ROM drive, Floppy drive, Network card • Monitor : CRT, LCD
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(); };
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(); };
Abstraction : Design of a Class • Class inheritance hierarchies Monitor Card Network CDRom Floppy CRT LCD
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(); };
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(); };
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; };
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; };
Abstraction : Design of a Class • Class inheritance hierarchies Component Monitor Card Network CDRom Floppy CRT LCD
Abstraction : Design of a Class • What are the differences between classes? (only within this design) Component Monitor Card Network CDRom Floppy CRT LCD
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
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
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
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_;} };
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_;} };
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
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) {} };
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
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.