9.9. Default Memberwise Assignment

The assignment operator (=) can be used to assign an object to another object of the same class. By default, such assignment is performed by memberwise assignment (also called copy assignment)—each data member of the object on the right of the assignment operator is assigned individually to the same data member in the object on the left of the assignment operator. Figures 9.139.14 define a Date class. Line 18 of Fig. 9.15 uses default memberwise assignment to assign the data members of Date object date1 to the corresponding data members of Date object date2. In this case, the month member of date1 is assigned to the month member of date2, the day member of date1 is assigned to the day member of date2 and the year member of date1 is assigned to the year member of date2. [Caution: Memberwise assignment can cause serious problems when used with a class whose data members contain pointers to dynamically allocated memory; we discuss these problems in Chapter 10 and show how to deal with them.]


 1   // Fig. 9.13: Date.h
 2   // Date class declaration.  Member functions are defined in Date.cpp.
 3
 4   // prevent multiple inclusions of header
 5   #ifndef DATE_H
 6   #define DATE_H
 7
 8   // class Date definition
 9   class Date
10   {
11   public:
12      explicit Date( int = 1, int = 1, int = 2000 ); // default constructor
13      void print();
14   private:
15      unsigned int month;
16      unsigned int day;
17      unsigned int year;
18   }; // end class Date
19
20   #endif


Fig. 9.13. Date class declaration.


 1   // Fig. 9.14: Date.cpp
 2   // Date class member-function definitions.
 3   #include <iostream>
 4   #include "Date.h" // include definition of class Date from Date.h
 5   using namespace std;
 6
 7   // Date constructor (should do range checking)
 8   Date::Date( int m, int d, int y )
 9      : month( m ), day( d ), year( y )
10   {
11   } // end constructor Date
12
13   // print Date in the format mm/dd/yyyy
14   void Date::print()
15   {
16      cout << month << '/' << day << '/' << year;
17   } // end function print


Fig. 9.14. Date class member-function definitions.


 1   // Fig. 9.15: fig09_15.cpp
 2   // Demonstrating that class objects can be assigned
 3   // to each other using default memberwise assignment.
 4   #include <iostream>
 5   #include "Date.h" // include definition of class Date from Date.h
 6   using namespace std;
 7
 8   int main()
 9   {
10      Date date1( 7, 4, 2004 );
11      Date date2; // date2 defaults to 1/1/2000
12
13      cout << "date1 = ";
14      date1.print();
15      cout << " date2 = ";
16      date2.print();
17
18      date2 = date1; // default memberwise assignment
19
20      cout << " After default memberwise assignment, date2 = ";
21      date2.print();
22      cout << endl;
23   } // end main


date1 = 7/4/2004
date2 = 1/1/2000

After default memberwise assignment, date2 = 7/4/2004


Fig. 9.15. Class objects can be assigned to each other using default memberwise assignment.

Objects may be passed as function arguments and may be returned from functions. Such passing and returning is performed using pass-by-value by default—a copy of the object is passed or returned. In such cases, C++ creates a new object and uses a copy constructor to copy the original object’s values into the new object. For each class, the compiler provides a default copy constructor that copies each member of the original object into the corresponding member of the new object. Like memberwise assignment, copy constructors can cause serious problems when used with a class whose data members contain pointers to dynamically allocated memory. Chapter 10 discusses how to define customized copy constructors that properly copy objects containing pointers to dynamically allocated memory.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.1.97