Lecture 2 - Construction, Destruction, Initializer Lists, Class Composition

Lecture 2 - Construction, Destruction, Initializer Lists, Class Composition

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

View Full Document Right Arrow Icon
Wednesday, Jan 11 th   Collect  Academic Integrity Forms Constructors Destructors Class Composition Composition with Initializer Lists A few final topics required for Project #1 Learning how to use the VC debugger (time  permitting)
Background image of page 1

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

View Full DocumentRight 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 DocumentRight 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  automatically initializes  every new  variable you create of that class. The constructor is called  automatically  every time you  create a new instance of your  class. (3,true); (int PCs, bool usesMac) CSNerd(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 chen           3 true true 3 Instead of being called  Init , the  constructor  function has the  same  name as the class ! (Confusing, huh?) Since the constructor is  called  automatically  any time you define a  new variable…  there’s no chance of a new variable  being uninitialized accidentally.
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.

This note was uploaded on 04/02/2012 for the course COMPUTER S 32 taught by Professor Smallberg during the Winter '12 term at UCLA.

Page1 / 66

Lecture 2 - Construction, Destruction, Initializer Lists, Class Composition

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

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