The ConstructorsThe Four Fundamental Member FunctionsThe Default ConstructorPurposes of the Default ConstructorExample: The Vectr ClassThe Automatic Default ConstructorThe Copy ConstructorPurposes of the Copy ConstructorPoint of StyleThe Automatic Copy ConstructorSlide 11Constructors and the new OperatorThe ConstructorsLecture 6Wed, Jan 30, 200801/13/19Four Fundamental Member Functions 2The Four Fundamental Member FunctionsThe functionsThe default constructorThe copy constructorThe destructorThe assignment operatorThese four member functions are essential to the functioning of any class.01/13/19Four Fundamental Member Functions 3The Default ConstructorThe default constructor constructs an object for which no initial value is given.Prototype:Usage:The default constructor should initialize the data members to neutral values that are appropriate for that type.Type::Type();Type Object;01/13/19Four Fundamental Member Functions 4Purposes of the Default ConstructorThe default constructor is used whenAn 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.01/13/19Four Fundamental Member Functions 5Example: The Vectr Classvectr.hvectr.cppVectrTest.cpp01/13/19Four Fundamental Member Functions 6The Automatic Default Constructor The automatic default constructorIs 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.01/13/19Four Fundamental Member Functions 7The Copy ConstructorThe 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);01/13/19Four Fundamental Member Functions 8Purposes of the Copy ConstructorThe copy constructor is used whenAn 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 function returns a value.01/13/19Four Fundamental Member Functions 9Point of StyleGood styleUse the copy constructor.Poor styleUse the default constructor followed by the assignment operator.Rational r = s;Rational r;r = s;01/13/19Four Fundamental Member Functions 10memBmemAmemCmemBmemAmemCObjectA ObjectBThe Automatic Copy ConstructorThe automatic copy constructorAllocates memory for the data members.Invokes each data member’s copy constructor to copy values from the existing object.copy01/13/19Four Fundamental Member Functions 11The Automatic Copy ConstructorThis 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?01/13/19Four Fundamental Member Functions 12Constructors and the new OperatorThe new operator is used in conjunction with the constructors.int* pi = new int(123);int* pai = new int[123];string* ps = new string("Hello");Rational* pr = new Rational(2, 3);Date* pd = new Date("Jan", 23, 2002);Point2D* ppt = new Point;Point2D* papt = new
View Full Document