{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture15%2Dpost

lecture15%2Dpost - Monday November 29 ClassChallenge...

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

View Full Document Right Arrow Icon
Monday, November 29 th Class Challenge Class Constructors Class Destructors Using new and delete with classes
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 Challenge Consider a class called  Track  that has following  methods : 1. An  Init  method that initializes the class 2. Submit  method that allows the user to submit a new integer between 0  and 999. 3. Find  method that accepts an integer and returns  true  if the user  previously submitted that number,  false  otherwise. Part A:   Write a class  declaration  for the  Track  class on the chalk  board.
Background image of page 2
Class Challenge Part B:   Write a  main  function that: 1. Declares a new object (variable) of your  Track  class. 2. Calls the  Init  method to initialize your object. 3. Prompts the user for 5 numbers and uses the  Submit  function to submit  these numbers to the object. 4. Uses the  Find  function to determine if the number 20 was submitted. Notice that we’re able to use our  Track  class without knowing how it actually  works internally.
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 Challenge Part C:   Write your Track class methods: 1. Init 2. Submit 3. Find Now watch what we can do…
Background image of page 4
Constructors class Circ { public: void Init (float x, float y, float r) { m_x = x; m_y = y; m_rad = r; } float GetArea(void) { return(3.14159*m_rad*m_rad); } private: float m_x, m_y, m_rad; }; main() { Circ a, b; a.Init(3,3,10); cout << a.GetArea(); b.Init(1,1,33); cout << b.GetArea(); } In our  Circ  class example, the  Init  member function is used to  initialize each instance of the  class with specific values. C++ actually has an  official  way to do this:  a constructor
Background image of page 5

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

View Full Document Right Arrow Icon
Constructors class Circ { public: void Init (float x, float y, float r) { m_x = x; m_y = y; m_rad = r; } float GetArea(void) { return(3.14159*m_rad*m_rad); } private: float m_x, m_y, m_rad; }; main() { Circ a; a.Init(3,3,10); cout << a.GetArea(); } Circ A constructor is a special member function that has the  same name  as the class  itself. It is responsible for initializing an instance’s data when the variable is  created. The constructor is called  automatically  every time you  create a new instance of your  class. (3,3,10);
Background image of page 6
Constructors class Circ { public: Circ(float x, float y, float r); float GetArea(void) const; private: float m_x, m_y, m_rad; }; // Circ::Circ is a constructor Circ::Circ(float x, float y, float r) { m_x = x; m_y = y; m_rad = r; } float Circ::GetArea(void) const { return(3.14159*m_rad*m_rad); } main() { Circ a(1,2,4); cout << a.GetArea(); } You can define the constructor in  the  class declaration  (like the  previous example), or in the usual  way, shown here: 1            2           4  a .m_x     .m_y     .m_rad         1 2 4 50.26
Background image of page 7

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

View Full Document Right Arrow Icon
  constructor  is a  special function in your class  that initializes a new variable  when its first created.
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}