lecture15

lecture15 - Monday, November 29 th • Class Challenge •...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

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

Unformatted text preview: Monday, November 29 th • Class Challenge • Class Constructors • Class Destructors • Using new and delete with classes Class Challenge Consider a class called Track that has following methods : 1. An Init method that initializes the class 1. A Submit method that allows the user to submit a new integer between 0 and 999. 1. A 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. Class Challenge Part B: Write a main function that: 1. Declares a new object (variable) of your Track class. 1. Calls the Init method to initialize your object. 1. Prompts the user for 5 numbers and uses the Submit function to submit these numbers to the object. 1. 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. Class Challenge Part C: Write your Track class methods: 1. Init 1. Submit 1. Find Now watch what we can do… 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 . 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); 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 A constructor is a special function in your class...
View Full Document

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

Page1 / 32

lecture15 - Monday, November 29 th • Class Challenge •...

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

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