lecture4 - MoreC+Topics const Constructors Destructors...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
CSE 335 Dr. Charles B. Owen Object-Oriented Programming 1 More C++ Topics const Constructors Destructors Assignment operators
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CSE 335 Dr. Charles B. Owen Object-Oriented Programming 2 const const is a compiler option you can use to guarantee something will not be  changed. // Function will not modify object std::wstring GetText() const {return mText;} // Returned string cannot be modified. const std::wstring &GetText() const {return mText;} // Parameter string cannot be modified void SetText(const std::wstring &str); // I can’t do this, the compiler will complain: std::wstring &GetText() const {return mText;}    // Error – Why?
Background image of page 2
CSE 335 Dr. Charles B. Owen Object-Oriented Programming 3 const  is all or nothing! Once you start using it, you have to keep using it and use it  everywhere! Most modern libraries use const very well. You can’t use “iterator” on a const object.  Use “const_iterator”,  instead.         for(list<CRecording *>:: const_iterator   r=mRecordings.begin();                r != mRecordings.end();  r++)         {              const  CRecording *recording = *r;             CRecording *copyRecording = new  CRecording(*recording);             mRecordings.push_back(copyRecording);         }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CSE 335 Dr. Charles B. Owen Object-Oriented Programming 4 An Example Set of Classes We can draw lines and display text. All drawables have an x,y  location.  We can group multiple drawables together.   Text
Background image of page 4
CSE 335 Dr. Charles B. Owen Object-Oriented Programming 5 The idea Line Text Group Line Group Group Line Text Text Text Group Line Line Line Line We can group  lines, text, and  other groups  together. Powerpoint works  this way.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CSE 335 Dr. Charles B. Owen Object-Oriented Programming 6 An Example Set of Classes We can draw lines and display text. All drawables have an x,y  location.  We can group multiple drawables together.   Text 3,4
Background image of page 6
CSE 335 Dr. Charles B. Owen Object-Oriented Programming 7 CDrawable class CDrawable { public:     CDrawable(void);     virtual ~CDrawable(void);     virtual void Draw(DC &dc, int x, int y) = 0;     int GetX() const {return mX;}     int GetY() const {return mY;}      void SetLocation(int x, int y) {mX = x; mY = y;} private:     int mX;     int mY; }; Pure virtual function Note virtual destructor
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CSE 335 Dr. Charles B. Owen Object-Oriented Programming 8 CGroup class CGroup { public:     CGroup(void);     virtual ~CGroup(void);     virtual void Draw(DC &dc, int x, int y);     void AddChild(CDrawable *child) {mChildren.push_back(child);} private:     std::list<CDrawable *> mChildren; }; Grouper 5
Background image of page 8
CSE 335 Dr. Charles B. Owen
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/12/2010 for the course CSE CSE 335 taught by Professor Owen during the Fall '10 term at Michigan State University.

Page1 / 39

lecture4 - MoreC+Topics const Constructors Destructors...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online