lecture2

lecture2 - Wednesday,April5th CollectAcademicIntegrityForms...

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

View Full Document Right Arrow Icon
    Wednesday, April 5 th   Collect  Academic Integrity Forms Constructors Destructors Class Composition Composition with Initializer Lists
Background image of page 1

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

View Full DocumentRight 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, 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 2
    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 3

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

View Full DocumentRight Arrow Icon
    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 4
      constructor  is a  special function in your class  that initializes a new variable  when its first created. The constructor function  MUST  have the  SAME NAME AS THE CLASS !   The constructor has  NO RETURN TYPE . The constructor function  CANNOT RETURN ANY VALUES .   Like all other class member functions, the constructor logic can be defined inside  or outside the class declaration.  Constructors
Background image of page 5

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

View Full DocumentRight Arrow Icon
    Constructors class Circ { 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); } The constructor has no return  type… Not void, not anything! The constructor has the same  name as the class. The constructor runs the moment  a new variable is created. You can’t call a constructor  explicitly. It’s called  automatically   when you create a  new class variable .
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); } If a constructor requires  parameters: You  must
Background image of page 7

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

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

This note was uploaded on 10/20/2009 for the course CS 32 taught by Professor Davidsmallberg during the Winter '08 term at UCLA.

Page1 / 36

lecture2 - Wednesday,April5th CollectAcademicIntegrityForms...

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

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