lecture2CS32Nachenburg

lecture2CS32Nachenburg - Wednesday, J an 6th Col l ect A...

Info iconThis preview shows pages 1–6. 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 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 . Not what you’d want. -32 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 A constructor (that the programmer may add to a class) that automatically initializes every new variable you create of that 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
Constructors You can define the constructor in the class declaration (see above)… class CSNerd { public: CSNerd(int PCs, bool usesMac) { m_numPCs = PCs; m_macUser = usesMac; } ...
Background image of page 5

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

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

Page1 / 53

lecture2CS32Nachenburg - Wednesday, J an 6th Col l ect A...

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

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