Upload
others
View
5
Download
0
Embed Size (px)
Citation preview
The ConstructorsLecture 5
Sections 4.4, 4.5, 6.3
Robb T. Koether
Hampden-Sydney College
Fri, Jan 22, 2010
Robb T. Koether (Hampden-Sydney College) The Constructors Fri, Jan 22, 2010 1 / 22
Outline
1 The Four Fundamental Member Functions
2 The Default ConstructorThe Automatic Default Constructor
3 The Copy ConstructorThe Automatic Copy Constructor
4 Constructors and the new Operator
5 Assignment
Robb T. Koether (Hampden-Sydney College) The Constructors Fri, Jan 22, 2010 2 / 22
Outline
1 The Four Fundamental Member Functions
2 The Default ConstructorThe Automatic Default Constructor
3 The Copy ConstructorThe Automatic Copy Constructor
4 Constructors and the new Operator
5 Assignment
Robb T. Koether (Hampden-Sydney College) The Constructors Fri, Jan 22, 2010 3 / 22
The Four Fundamental Member Functions
The four fundamental functionsI The default constructorI The copy constructorI The destructorI The assignment operator
These four member functions are essential to the functioning ofany class.
Robb T. Koether (Hampden-Sydney College) The Constructors Fri, Jan 22, 2010 4 / 22
Outline
1 The Four Fundamental Member Functions
2 The Default ConstructorThe Automatic Default Constructor
3 The Copy ConstructorThe Automatic Copy Constructor
4 Constructors and the new Operator
5 Assignment
Robb T. Koether (Hampden-Sydney College) The Constructors Fri, Jan 22, 2010 5 / 22
The Default Constructor
The Default ConstructorType::Type(); // PrototypeType Object; // Usage
The default constructor constructs an object for which no initialvalue is given.The default constructor should initialize the data members toneutral values that are appropriate for that type.
Robb T. Koether (Hampden-Sydney College) The Constructors Fri, Jan 22, 2010 6 / 22
Purposes of the Default Constructor
The Default ConstructorVectr v;Vectr* vptr = new Vectr[10];Vectr vptr2[10] = {Vectr(4, 123), Vectr(8, 567)};
The default constructor is used whenI An object is created with no initial value specified.I The new operator is used to create an array.I A static array is partially initialized.
Robb T. Koether (Hampden-Sydney College) The Constructors Fri, Jan 22, 2010 7 / 22
The Vectr Class
Example (The Vectr Class)The code.
I The header file vectr.h.I The implementation file vectr.cpp.I The test program Vectr Test.cpp.
The executable.
Robb T. Koether (Hampden-Sydney College) The Constructors Fri, Jan 22, 2010 8 / 22
Outline
1 The Four Fundamental Member Functions
2 The Default ConstructorThe Automatic Default Constructor
3 The Copy ConstructorThe Automatic Copy Constructor
4 Constructors and the new Operator
5 Assignment
Robb T. Koether (Hampden-Sydney College) The Constructors Fri, Jan 22, 2010 9 / 22
The Automatic Default Constructor
The automatic default constructor is provided automatically if wewrite no constructor.It
I Allocates memory for the data members.I Invokes each data member’s own default constructor.
It is not provided automatically if we write any constructor; wemust then write the default constructor, if we want one.
Robb T. Koether (Hampden-Sydney College) The Constructors Fri, Jan 22, 2010 10 / 22
Outline
1 The Four Fundamental Member Functions
2 The Default ConstructorThe Automatic Default Constructor
3 The Copy ConstructorThe Automatic Copy Constructor
4 Constructors and the new Operator
5 Assignment
Robb T. Koether (Hampden-Sydney College) The Constructors Fri, Jan 22, 2010 11 / 22
The Copy Constructor
The Copy ConstructorType::Type(const Type&); // PrototypeType ObjectA = ObjectB; // Usage 1Type ObjectA(ObjectB); // Usage 2
The copy constructor constructs an object which will be a copy ofan existing object.
Robb T. Koether (Hampden-Sydney College) The Constructors Fri, Jan 22, 2010 12 / 22
Purposes of the Copy Constructor
The Copy ConstructorVectr f(Vectr v);int main(){
Vectr v;Vectr u = v;Vectr w(u);u = f(v);
...}
The copy constructor is used whenI An object is created and initialized to the value of an existing object
of the same type.I A local copy of a value parameter is created during a function call.I A function returns a value.
Robb T. Koether (Hampden-Sydney College) The Constructors Fri, Jan 22, 2010 13 / 22
Point of Style
The Copy ConstructorVectr u = v; // Good styleVectr w; // Poor stylew = u;
The first uses the copy constructor.The second uses the default constructor followed by theassignment operator.
Robb T. Koether (Hampden-Sydney College) The Constructors Fri, Jan 22, 2010 14 / 22
Points of Style
The Copy ConstructorVectr(const Vectr& v){
makeCopy(v);return;
}
void makeCopy(const Vectr& v){// Make a copy}
A handy technique is to write a function makeCopy() and simplycall on it to do the work of the copy constructor.
Robb T. Koether (Hampden-Sydney College) The Constructors Fri, Jan 22, 2010 15 / 22
Outline
1 The Four Fundamental Member Functions
2 The Default ConstructorThe Automatic Default Constructor
3 The Copy ConstructorThe Automatic Copy Constructor
4 Constructors and the new Operator
5 Assignment
Robb T. Koether (Hampden-Sydney College) The Constructors Fri, Jan 22, 2010 16 / 22
The Automatic Copy Constructor
The automatic copy constructorI Allocates memory for the data members.I Invokes each data member’s copy constructor to copy values from
the existing object.
memAmemBmemC
memAmemBmemC
ObjectA ObjectB
copyas is
Robb T. Koether (Hampden-Sydney College) The Constructors Fri, Jan 22, 2010 17 / 22
The Automatic Copy Constructor
This is called a shallow copy.Pointers get copied with no change in value.Therefore, the pointer in the new object will point to the very samememory as the pointer in the old object.Generally, this is not good. Instead, we want a deep copy.What would happen in the Vectr class if we made a shallow copyof a vector?
Robb T. Koether (Hampden-Sydney College) The Constructors Fri, Jan 22, 2010 18 / 22
Outline
1 The Four Fundamental Member Functions
2 The Default ConstructorThe Automatic Default Constructor
3 The Copy ConstructorThe Automatic Copy Constructor
4 Constructors and the new Operator
5 Assignment
Robb T. Koether (Hampden-Sydney College) The Constructors Fri, Jan 22, 2010 19 / 22
Constructors and the new Operator
The new Operator and ConstructorsVectr* vptr;vptr = new Vectr;vptr = new Vectr(v);vptr = new Vectr(10);vptr = new Vectr(10, 123);vptr = new Vectr[10];
The new operator works in conjunction with the constructors.
Robb T. Koether (Hampden-Sydney College) The Constructors Fri, Jan 22, 2010 20 / 22
Outline
1 The Four Fundamental Member Functions
2 The Default ConstructorThe Automatic Default Constructor
3 The Copy ConstructorThe Automatic Copy Constructor
4 Constructors and the new Operator
5 Assignment
Robb T. Koether (Hampden-Sydney College) The Constructors Fri, Jan 22, 2010 21 / 22
Assignment
HomeworkRead Section 4.4, pages 158 - 165.Read Section 4.5, pages 165 - 169.Read Section 6.3, pages 269 - 286.
Robb T. Koether (Hampden-Sydney College) The Constructors Fri, Jan 22, 2010 22 / 22