{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture2

lecture2 - Wednesday,Jan6 th Constructors Destructors...

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

View Full Document Right Arrow Icon
Wednesday, Jan 6 th   Collect  Academic Integrity Forms Constructors Destructors Class Composition Composition with Initializer Lists Two final topics required for Project #1 Adjust cyclical dependency presentation with one CSNerd  having a reference to an EENerd but the EENerd havinga full  CSNerd variable (so we do one forward declaration and one  not) Add a few examples of invalid default parameters (a=5,b,c),  etc.
Background image of page 1

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

View Full Document Right Arrow Icon
class CSNerd { public: void Init(int PCs, bool usesMac) { m_numPCs = PCs; // # of PCs owned m_macUser = usesMac; } int getNerdScore(void) { if(m_macUser == true) return(0); //not nerdy; “artistic” return(10 * m_numPCs); } private: int m_numPCs; bool m_macUser; }; main() { CSNerd david; david.Init(2,false); // geeky cout << david.getNerdScore(); } Constructors: Class Initialization Every class should have an  initialization function  that can be  used to reset new variables before  they’re used. void Init (int PCs, bool usesMac) { m_numPCs = PCs; m_macUser = usesMac; } int getNerdScore(void) { if(m_macUser == true) return(0); return(10 * m_numPCs); } m_numPCs m_macUser david           2    false   2 false 10*2 = 20 But there’s  one problem  with  such an Init function…  What is  it?   Here’s a hint! Right! Our programmer might  forget to  call the Init  function before using the  variable… What’ll happen? Well, remember, all simple variables (e.g.,  ints bools , etc.) in C++  start out  with  random values   unless they’re explicitly initialized! So if you forget to call the Init function, your  CSNerd’s  member variables  will have  random  values . Not what you’d want. -32              false Ack! How can someone  have -32 computers? 
Background image of page 2
class CSNerd { public: void Init(int PCs, bool usesMac) { m_numPCs = PCs; m_macUser = usesMac; } int getNerdScore(void) { if(m_macUser == true) return(0); return(10 * m_numPCs); } private: int m_numPCs; bool m_macUser; }; Constructors Wouldn’t it be great if  C++  would  guarantee  that every time we  create  a new class  variable , it’ll be  auto-initialized ? Well, as it turns out, that’s exactly what the  C++ constructor  does! main() { CSNerd david; cout << david.getNerdScore(); }
Background image of page 3

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

View Full Document Right Arrow Icon
class CSNerd { public: void Init (int PCs, bool usesMac) { m_numPCs = PCs; m_macUser = usesMac; } int getNerdScore(void) { if(m_macUser == true) return(0); return(10 * m_numPCs); } private: int m_numPCs; bool m_macUser; }; Constructors main() { CSNerd chen; chen.Init(3,true); cout << chen.getNerdScore(); } CSNerd constructor  is a special member function  (that the programmer may add to a class) that  automatically initializes  every new  variable you create of that class.
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 53

lecture2 - Wednesday,Jan6 th Constructors Destructors...

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

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