Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Constructors Initializing New Objects #include “fraction.h” int main() { float x; float y = 6.7; float z(7.2); Fraction."— Presentation transcript:

1

2 http://cs.mst.edu Constructors

3 http://cs.mst.edu 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 http://cs.mst.edu 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 http://cs.mst.edu 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 http://cs.mst.edu 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 http://cs.mst.edu Initializing New Objects #include “fraction.h” int main() { float x; float y = 6.7; float z(7.2); Fraction f; Fraction g(4, 5);...

8 http://cs.mst.edu 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...

9 http://cs.mst.edu Constructors  Constructors are always named the name of the class.

10 http://cs.mst.edu 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.

11 http://cs.mst.edu 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.

12 http://cs.mst.edu 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.

13 http://cs.mst.edu 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.

14 http://cs.mst.edu First Constructor class Fraction { public: Fraction(const int num, const int den); void readin(); void print();... };

15 http://cs.mst.edu 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; }

16 http://cs.mst.edu First Constructor class Fraction { public: int 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; } Take Note: No Return Statements or Types NO!

17 http://cs.mst.edu First Constructor 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; } Take Note: No Return Statements or Types NO!

18 http://cs.mst.edu 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; return; } Take Note: No Return Statements or Types NO!

19 http://cs.mst.edu 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; }

20 http://cs.mst.edu 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! }

21 http://cs.mst.edu 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 }

22 http://cs.mst.edu 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.

23 http://cs.mst.edu 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); } }

24 http://cs.mst.edu 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 http://cs.mst.edu 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 http://cs.mst.edu 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 http://cs.mst.edu Initializing New Objects #include “fraction.h” int main() { float x; float y = 6.7; float z(7.2); Fraction f; Fraction g(4, 5);...

28 http://cs.mst.edu 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.

29 http://cs.mst.edu 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

30 http://cs.mst.edu 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...

31 http://cs.mst.edu Copy Constructor // main.cpp... int main() { Fraction g(4, 5); Fraction h(g)... // fraction.h... class Fraction { Fraction(const Fraction & source);...

32 http://cs.mst.edu 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) {}

33 http://cs.mst.edu Initializing New Objects A 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.

34 http://cs.mst.edu End of Session


Download ppt "Constructors Initializing New Objects #include “fraction.h” int main() { float x; float y = 6.7; float z(7.2); Fraction."

Similar presentations


Ads by Google