{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture2 - Wednesday,April5th Constructors Destructors...

Info icon This 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
Image of page 1

Info icon This 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, 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
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);
Image of page 3

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

View Full Document Right 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
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
Image of page 5

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

View Full Document Right 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); } 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 .
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 
Image of page 7

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern