lecture27 Constructors and Destructors

lecture27 Constructors and Destructors - 1 Janice Regan,...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 Janice Regan, CMPT 128, February. 2007 CMPT 128: Introduction to Computing Science for Engineering Students Constructors and Destructors Janice Regan, CMPT 128, February 2007 2 Review of classes A class has members which are Attributes: variables whose values describe an object of the class Methods: functions that perform operations on the attributes of the class, or use the attributes of the class as inputs for more complicated algorithms A class is an abstract data type (ADT) Just like any other data type a class includes a set of possible objects and a set of operations (methods) that are legal on those objects The objects in a class are instances of the class Just like 1 is an object of the type int Janice Regan, CMPT 128, February 2007 3 Instances of a class A class is an abstract description of a group of objects. Each possible object in that group of objects can be an instance of the class An instance of a class will have particular values for each of the attributes of the class We can make as many instances as we need Different instances can have different values of the parameters Different instances can have the same values of parameters (like integer variables B1=1, B5=1) Janice Regan, CMPT 128, February 2007 4 Creating an instance of a class (1) So far we have declared instances of classes For example for class ifstream (input stream) we declared a variable ifstream inStream; inStream is then an object of type ifStream, and is a member of the class ifStream DayOfYear today; today is then an object of type DayOfYear and is a member of the class DayOfYear Janice Regan, CMPT 128, February 2007 5 Creating an instance of a class (2) When we created an instance of a class by declaring a variable to be an object of that class type what happens. A block of memory large enough to hold all the values of the attributes of the objects is set aside If any of the attributes are pointers, only the memory for the pointer is set aside, not the memory for the thing being pointed to The values of the attributes are not initialized!!! Janice Regan, CMPT 128, February 2007 6 Creating an instance of a class (3) How do we initialize the values of the attributes? Write mutator methods necessary but really meant to change attribute values after they have been initialized A better way is available, this is called a constructor Janice Regan, CMPT 128, February 2007 7 Creating an instance of a class (4) A constructor is a special type of member method A constructor has the same identifier (name) as the class A constructor has no return value or return type A constructor can be used to initialize attributes of objects A constructor can have any number of parameters, with any combination of types A constructor can be used to allocate deep memory (the memory pointed to by pointer attributes of the method) A constructor may be used to validate the data A constructor may be used to make a copy of an object A class may have more than one constructor (probably will have) Janice Regan, CMPT 128, February 2007...
View Full Document

Page1 / 36

lecture27 Constructors and Destructors - 1 Janice Regan,...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online