H-SC COMS 262 - Lecture 6 - The Constructors

Unformatted text preview:

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 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.01/13/19Four Fundamental Member Functions 3The 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 neutral values that are appropriate for that type.Type::Type();Type Object;01/13/19Four Fundamental Member Functions 4Purposes 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.01/13/19Four Fundamental Member Functions 5Example: The Vectr Classvectr.hvectr.cppVectrTest.cpp01/13/19Four Fundamental Member Functions 6The Automatic Default Constructor 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.01/13/19Four Fundamental Member Functions 7The 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);01/13/19Four Fundamental Member Functions 8Purposes 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 function returns a value.01/13/19Four Fundamental Member Functions 9Point 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;01/13/19Four Fundamental Member Functions 10memBmemAmemCmemBmemAmemCObjectA ObjectBThe Automatic Copy ConstructorThe automatic copy constructorAllocates 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 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?01/13/19Four Fundamental Member Functions 12Constructors and the new OperatorThe 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

H-SC COMS 262 - Lecture 6 - The Constructors

Download Lecture 6 - The Constructors
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 6 - The Constructors and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 6 - The Constructors 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?