Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Constructors Lecture 7 Fri, Feb 2, 2007.

Similar presentations


Presentation on theme: "The Constructors Lecture 7 Fri, Feb 2, 2007."— Presentation transcript:

1 The Constructors Lecture 7 Fri, Feb 2, 2007

2 The Four Fundamental Member Functions
The functions The default constructor The copy constructor The destructor The assignment operator These four member functions are essential to the functioning of any class. 5/22/2019 Four Fundamental Member Functions

3 The Default Constructor
The default constructor constructs an object for which no initial value is given. Prototype: Usage: The default constructor should initialize the data members to values that are appropriate for that type. Type::Type(); Type Object; 5/22/2019 Four Fundamental Member Functions

4 Purposes of the Default Constructor
The default constructor is used when An object is created with no initial value specified. The members of an array are initialized automatically. When the new operator is used. When a static array is partially initialized. 5/22/2019 Four Fundamental Member Functions

5 Example: The Vectr Class
vectr.h vectr.cpp VectrTest.cpp 5/22/2019 Four Fundamental Member Functions

6 The Automatic Default Constructor
Is provided automatically if we write no constructor. Is not provided if we write any constructor; we must then write the default constructor, if we want one. Allocates memory for the data members. Invokes each data member’s own default constructor. 5/22/2019 Four Fundamental Member Functions

7 Four Fundamental Member Functions
The Copy Constructor The copy constructor constructs an object which will be a copy of an existing object. Prototype: Usage: Type::Type(const Type&); Type ObjectA = ObjectB; Type ObjectA(ObjectB); 5/22/2019 Four Fundamental Member Functions

8 Purposes of the Copy Constructor
The copy constructor is used when An object is created and initialized to the value of an existing object of the same type. A local copy of a value parameter is created during a function call. A copy of the return value is created when a function returns. 5/22/2019 Four Fundamental Member Functions

9 Four Fundamental Member Functions
Point of Style Good style Use the copy constructor. Poor style Use the default constructor followed by the assignment operator. Rational r = s; Rational r; r = s; 5/22/2019 Four Fundamental Member Functions

10 The Automatic Copy Constructor
Allocates memory for the data members. Invokes each data member’s copy constructor to copy values from the existing object. memB memA memC ObjectA ObjectB 5/22/2019 Four Fundamental Member Functions

11 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 same memory 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 copy of a vector? 5/22/2019 Four Fundamental Member Functions


Download ppt "The Constructors Lecture 7 Fri, Feb 2, 2007."

Similar presentations


Ads by Google