include fractionh int main float x float y 67 float z72 Fraction f Fraction g4 5 Initializing New Objects include fractionh ID: 710550
Download Presentation The PPT/PDF document "Constructors Initializing New Objects" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
ConstructorsSlide2
Initializing New Objects
#include “
fraction.h
”
int
main()
{
float x;
float y = 6.7;
float z(7.2);
Fraction f;
Fraction g(4, 5);
...
Slide3
Initializing New Objects
#include “
fraction.h
”
int main(){ float x; float y = 6.7; float z(7.2); Fraction f; Fraction g(4, 5); ... Slide4
Initializing New Objects
#include “
fraction.h
”
int main(){ float x; float y = 6.7; float z(7.2); Fraction f; Fraction g(4, 5); ... Slide5
Initializing New Objects
#include “
fraction.h
”
int main(){ float x; float y = 6.7; float z(7.2); Fraction f; Fraction g(4, 5); ... Slide6
Initializing New Objects
#include “
fraction.h
”
int main(){ float x; float y = 6.7; float z(7.2); Fraction f; Fraction g(4, 5); ... Slide7
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 ... Slide8
Constructors
Constructors are always named the name of the class.Slide9
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.Slide10
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.Slide11
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.Slide12
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.Slide13
First Constructor
class Fraction
{
public:
Fraction(const int num, const int den); void readin(); void print(); ...}; Slide14
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;} Slide15
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 TypesNO!Slide16
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 TypesNO!Slide17
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 TypesNO!Slide18
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.cppFraction::Fraction(const int
num, const int den) {
m_Numerator
= num;
m_Denominator
= den;
}
Slide19
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!
}
Slide20
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.cppFraction::Fraction(const int
num, const int den) {
setNum
(num);
setDen
(den); // let
mutator
function vet the argument passed
}
Slide21
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.cppFraction::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.
Slide22
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.cppFraction::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);
}
}
Slide23
Initializing New Objects
#include “
fraction.h
”
int main(){ float x; float y = 6.7; float z(7.2); Fraction f; Fraction g(4, 5); ... Slide24
Initializing New Objects
#include “
fraction.h
”
int main(){ float x; float y = 6.7; float z(7.2); Fraction f; Fraction g(4, 5); ... Slide25
Initializing New Objects
#include “
fraction.h
”
int main(){ float x; float y = 6.7; float z(7.2); Fraction f; Fraction g(4, 5); ... Slide26
Initializing New Objects
#include “
fraction.h
”
int main(){ float x; float y = 6.7; float z(7.2); Fraction f; Fraction g(4, 5); ... Slide27
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.
Slide28
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/1Slide29
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 ... Slide30
Copy Constructor
// main.cpp
...
int
main(){ Fraction g(4, 5); Fraction h(g) ... // fraction.h...class Fraction{ Fraction(const Fraction & source);
...Slide31
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
) {}Slide32
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. Slide33
End of Session