lecture14slides

lecture14slides - Wednesday, November 28th, 2001 Topics 1....

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

View Full Document Right Arrow Icon
Wednesday, November 28 th , 2001 Topics 1. One more composition example 2. Using #define, #ifndef and #endif 3. Constant class instances and const member functions 4. Static member variables and functions 5. More pointers to classes 6. Friend functions
Background image of page 1

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

View Full DocumentRight Arrow Icon
A final composition example: version #1 class Heart { public: Heart() { m_size = 10; } int GetMyLove() { return m_size; } private: int m_size; }; class Brain { public: Brain(int IQ) { m_IQ = IQ; } int GetMyIQ() { return m_IQ; } private: int m_IQ; }; class StrawMan { public: StrawMan(int IQ) : m_brain(IQ) { } int MyCourage(void) { return (m_heart.GetMyLove() + m_brain.GetMyIQ()); } private: Heart m_heart; Brain m_brain; }; main() { StrawMan vladamir(100); cout << vladamir.MyCourage(); }
Background image of page 2
class Heart { // version #2 public: Heart(int size) { m_size = size; } int GetMyLove() { return m_size; } private: int m_size; }; class Brain { public: Brain(int IQ) { m_IQ = IQ; } int GetMyIQ() { return m_IQ; } private: int m_IQ; }; class StrawMan { public: StrawMan(int heartsize, int IQ) : m_heart(heartsize), m_brain(IQ) { } int MyCourage(void) { return (m_heart.GetMyLove() + m_brain.GetMyIQ()); } private: Heart m_heart; Brain m_brain; }; main() { StrawMan vladamir(5,100); cout << vladamir.MyCourage();
Background image of page 3

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

View Full DocumentRight Arrow Icon
#define and #ifndef Sometimes you include a header file more than once in a cpp file. This often causes problems! gastank.h class GasTank { ... }; porsche.h #include “gastank.h” class Porsche { ... GasTank m_tank; }; main.cpp #include “gastank.h” #include “porsche.h” // gastank is // included twice! ...
Background image of page 4
We can ensure that a header file is included only once by using this logic: gastank.h #ifndef _GASTANK_H_ # define _GASTANK_H_ // place header contents here class GasTank { ... }; #endif // matches #ifndef _GASTANK_H_
Background image of page 5

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

View Full DocumentRight Arrow Icon
Explanation: “#define SYMBOL” is almost like declaring a variable called SYMBOL. During compilation, SYMBOL is
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/08/2011 for the course CS 31 taught by Professor Melkanoff during the Fall '00 term at UCLA.

Page1 / 20

lecture14slides - Wednesday, November 28th, 2001 Topics 1....

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

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