1 / 33

Constructors

Constructors. Initializing New Objects. #include “ fraction.h ” int main() { float x; float y = 6.7; float z(7.2); Fraction f; Fraction g(4, 5); . Initializing New Objects. #include “ fraction.h ” int main() { float x; float y = 6.7;

niabi
Download Presentation

Constructors

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Constructors

  2. Initializing New Objects #include “fraction.h” int main() { float x; float y = 6.7; float z(7.2); Fraction f; Fraction g(4, 5); ...

  3. Initializing New Objects #include “fraction.h” int main() { float x; float y = 6.7; float z(7.2); Fraction f; Fraction g(4, 5); ...

  4. Initializing New Objects #include “fraction.h” int main() { float x; float y = 6.7; float z(7.2); Fraction f; Fraction g(4, 5); ...

  5. Initializing New Objects #include “fraction.h” int main() { float x; float y = 6.7; float z(7.2); Fraction f; Fraction g(4, 5); ...

  6. Initializing New Objects #include “fraction.h” int main() { float x; float y = 6.7; float z(7.2); Fraction f; Fraction g(4, 5); ...

  7. Initializing New Objects #include “fraction.h” int main() { float x; float y = 6.7; float z(7.2); Fraction f; Fraction g(4, 5); // no constructor to accept two ints ...

  8. Constructors Constructors are always named the name of the class.

  9. Constructors Constructors are always named the name of the class. Constructors have no return type (this is not void) and hence have no return statement.

  10. Constructors Constructors are always named the name of the class. Constructors have no return type (this is not void) and hence have no return statement. Constructors can be overloaded like any other function, and you will most likely do so.

  11. Constructors Constructors are always named the name of the class. Constructors have no return type (this is not void) and hence have no return statement. Constructors can be overloaded like any other function, and you will most likely do so. Constructors are called by the compiler automatically; they are rarely called explicitly by the programmer.

  12. Constructors Constructors are always named the name of the class. Constructors have no return type (this is not void) and hence have no return statement. Constructors can be overloaded like any other function, and you will most likely do so. Constructors are called by the compiler automatically; they are rarely called explicitly by the programmer. If you write no constructor, the compiler will provide your class with a default constructor. The mechanism is suppressed once you write any constructor.

  13. First Constructor class Fraction {   public:     Fraction(const int num, const int den);     void readin();     void print(); ... }; 

  14. First Constructor class Fraction {   public:     Fraction(const int num, const int den);     void readin();     void print(); ... }; Fraction::Fraction(const int num, const int den)  { m_Numerator = num; m_Denominator = den; }   

  15. First Constructor NO! Take Note: No Return Statements or Types class Fraction {   public: intFraction(const int num, const int den);     void readin();     void print(); ... }; Fraction::Fraction(const int num, const int den)  { m_Numerator = num; m_Denominator = den; }   

  16. First Constructor NO! Take Note: No Return Statements or Types class Fraction {   public:     Fraction(const int num, const int den);     void readin();     void print(); ... }; void Fraction::Fraction(const int num, const int den)  { m_Numerator = num; m_Denominator = den; }   

  17. NO! First Constructor Take Note: No Return Statements or Types class Fraction {   public:     Fraction(const int num, const int den);     void readin();     void print(); ... }; Fraction::Fraction(const int num, const int den)  { m_Numerator = num; m_Denominator = den; return; }   

  18. What if… // main.cpp #include “fraction.h” int main() { float x; float y = 6.7; float z(7.2); Fraction f; Fraction g(4, 0); ... // fraction.cpp Fraction::Fraction(const int num, const int den)  { m_Numerator = num; m_Denominator = den; }   

  19. What if… // main.cpp #include “fraction.h” int main() { float x; float y = 6.7; float z(7.2); Fraction f; DENOMINATOR ZERO !! Fraction g(4, 0); ... // fraction.cpp Fraction::Fraction(const int num, const int den)  { m_Numerator = num; m_Denominator = den; // allows ALL integer values, even zero! }

  20. What if fix… // main.cpp #include “fraction.h” int main() { float x; float y = 6.7; float z(7.2); Fraction f; Fraction g(4, 0); ... // fraction.cpp Fraction::Fraction(const int num, const int den)  { setNum(num); setDen(den); // let mutator function vet the argument passed }

  21. An Initialization List // main.cpp #include “fraction.h” int main() { float x; float y = 6.7; float z(7.2); Fraction f; Fraction g(4, 0); ... // fraction.cpp Fraction::Fraction(const int num, const int den) : m_Numerator(num),m_Denominator(den){} // note: you must have a function body in an initialization list // even if it is empty.

  22. What if fix… // main.cpp #include “fraction.h” int main() { float x; float y = 6.7; float z(7.2); Fraction f; Fraction g(4, 0); ... // fraction.cpp Fraction::Fraction(const int num, const int den) : m_Numerator(num),m_Denominator(den) { if(m_Denominator == 0) { cout << “error: 0 passed in as denominator” << endl; exit(1); } }

  23. Initializing New Objects #include “fraction.h” int main() { float x; float y = 6.7; float z(7.2); Fraction f; Fraction g(4, 5); ...

  24. Initializing New Objects #include “fraction.h” int main() { float x; float y = 6.7; float z(7.2); Fraction f; Fraction g(4, 5); ...

  25. Initializing New Objects #include “fraction.h” int main() { float x; float y = 6.7; float z(7.2); Fraction f; Fraction g(4, 5); ...

  26. Initializing New Objects #include “fraction.h” int main() { float x; float y = 6.7; float z(7.2); Fraction f; Fraction g(4, 5); ...

  27. Initializing New Objects #include “fraction.h” int main() { float x; float y = 6.7; float z(7.2); Fraction f; Fraction g(4, 5); ... Because we have provided one constructor, the compiler no longer automatically provides a default constructor – one that takes no arguments.

  28. Default Constructor // main.cpp ... int main() { Fraction f; ... // fraction.h ... class Fraction { Fraction(); // default constructor ... // fraction.cpp Fraction::Fraction() : m_Numerator(0), m_Denominator(1) {} // defaults to 0/1

  29. Copy Constructor #include “fraction.h” int main() { float x; float y = 6.7; float z(7.2); Fraction f; Fraction g(4, 5); Fraction h(g); // create a fraction that is a copy of another ...

  30. Copy Constructor // main.cpp ... int main() { Fraction g(4, 5); Fraction h(g) ... // fraction.h ... class Fraction { Fraction(const Fraction & source); ...

  31. Copy Constructor // main.cpp ... int main() { Fraction g(4, 5); Fraction h(g) ... // fraction.h ... class Fraction { Fraction(const Fraction & source); ... // fraction.cpp Fraction::Fraction(const Fraction & source) : m_Numerator(source.m_Numerator), m_Denominator(source.m_Denominator) {}

  32. Initializing New ObjectsA Warning! #include “fraction.h” int main() { fraction f(); // NEVER do this. The compiler thinks you are ... // declaring a function named “f” that has no // parameters but returns a fraction.

  33. End of Session

More Related