330 likes | 474 Views
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;
E N D
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; float z(7.2); Fraction f; Fraction g(4, 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); ...
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; float z(7.2); Fraction f; Fraction g(4, 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); // no constructor to accept two ints ...
Constructors Constructors are always named the name of the class.
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 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 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.
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.
First Constructor class Fraction { public: Fraction(const int num, const int den); void readin(); void print(); ... };
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; }
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; }
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; }
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; }
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; }
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! }
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 }
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.
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); } }
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; float z(7.2); Fraction f; Fraction g(4, 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); ...
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; 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.
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
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 ...
Copy Constructor // main.cpp ... int main() { Fraction g(4, 5); Fraction h(g) ... // fraction.h ... class Fraction { Fraction(const Fraction & source); ...
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) {}
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.