objects and classes part ii cis 61. constructors we saw before two ways that member functions can be...

28
Objects and Classes Part II CIS 61

Upload: landon-ohara

Post on 26-Mar-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Objects and Classes Part II CIS 61. Constructors We saw before two ways that member functions can be used to give values to the data items in an object

Objects and Classes Part II

CIS 61

Page 2: Objects and Classes Part II CIS 61. Constructors We saw before two ways that member functions can be used to give values to the data items in an object

Constructors

We saw before two ways that member functions can be used to give values to the data items in an object.

But sometimes it is convenient if an object can initialize itself when it’s first created, without requiring a separate call to a member function.

Page 3: Objects and Classes Part II CIS 61. Constructors We saw before two ways that member functions can be used to give values to the data items in an object

Constructors

Automatic initialization is carried out using a special member function called a constructor.

A constructor is a member function that is executed automatically whenever an object is created.

Page 4: Objects and Classes Part II CIS 61. Constructors We saw before two ways that member functions can be used to give values to the data items in an object

A Counter Example

For example we will create a class of objects that might be useful as a general-purpose programming element.

A counter is a variable that counts things.

It might count keystrokes, number of lines, number of times in a loop.

Page 5: Objects and Classes Part II CIS 61. Constructors We saw before two ways that member functions can be used to give values to the data items in an object

A Counter Example

Each time one of these events take place, the counter is incremented (1 is added to it).

Let’s assume that this counter needs to be accessed by many different functions.

In a procedural language like C, a counter would probably be implemented as a global variable.

Page 6: Objects and Classes Part II CIS 61. Constructors We saw before two ways that member functions can be used to give values to the data items in an object

A Counter Example

But remember that global variables should be avoided in most cases and can complicate a program’s designed and can be accessed accidentally.

Let’s look at the example “counter.cpp”.

Page 7: Objects and Classes Part II CIS 61. Constructors We saw before two ways that member functions can be used to give values to the data items in an object

Automatic Initialization

When an object of type counter is first created, we want its count to be initialized to 0, since this is what most counts do.

We could provide a set_count() function to do this and call it with an argument of 0, or we could provide a zero_count() function, which would always set the count to 0.

Page 8: Objects and Classes Part II CIS 61. Constructors We saw before two ways that member functions can be used to give values to the data items in an object

Automatic Initialization

The only problem with writing our own functions like that would be that we would need to execute 2 lines of code for each object created.

counter c1; //every time we must do this

c1.zero_count(); //we must do this too

Page 9: Objects and Classes Part II CIS 61. Constructors We saw before two ways that member functions can be used to give values to the data items in an object

Automatic Initialization

Having to initialize variables in this way each time is mistake prone, because the programmer might forget to initialize the object after creating it.

It is more reliable and convenient to cause each object to initialize itself when it is created.

Page 10: Objects and Classes Part II CIS 61. Constructors We saw before two ways that member functions can be used to give values to the data items in an object

Automatic Initialization

In the counter class, the constructor Counter() does this.

This function is called automatically whenever a new object of type Counter is created.

counter c1, c2;

This creates two objects of type Counter.

Page 11: Objects and Classes Part II CIS 61. Constructors We saw before two ways that member functions can be used to give values to the data items in an object

Automatic Initialization

As each object is created, its constructor, Counter(), is executed.This function sets the count variable to 0.So the effect of the single statement “Counter c1, c2;” is to not only create two objects, but also to initialize their count variables to 0.

Page 12: Objects and Classes Part II CIS 61. Constructors We saw before two ways that member functions can be used to give values to the data items in an object

Same Name as the Class

There are some unusual aspects of constructor functions.

First, it is no accident that they have exactly the same name (Counter in our example) as the class of which they are members. This is one way the compiler knows that it is a constructor.

Page 13: Objects and Classes Part II CIS 61. Constructors We saw before two ways that member functions can be used to give values to the data items in an object

Same Name as the Class

Second, there is no return type used for constructors.

This is because the constructor is called automatically by the system, there’s no program for it to return anything to.

This is the second way the compiler knows that it is a constructor.

Page 14: Objects and Classes Part II CIS 61. Constructors We saw before two ways that member functions can be used to give values to the data items in an object

Initializer List

One of the most common tasks a constructor carries out is initializing data members.

In the Counter class the constructor must initialize the count member to 0.

Page 15: Objects and Classes Part II CIS 61. Constructors We saw before two ways that member functions can be used to give values to the data items in an object

Initializer List

One way to initialize a count would be:

count()

{count = 0;}

But this is not ideal even though it works.

This is how you should do it:

count() : count(0)

{ }

Page 16: Objects and Classes Part II CIS 61. Constructors We saw before two ways that member functions can be used to give values to the data items in an object

Initializer List

If multiple members must be initialized, they’re separated by commas.

Here is what that would look like:

someClass() : m1(7), m2(33), m3(4)

{ }

Page 17: Objects and Classes Part II CIS 61. Constructors We saw before two ways that member functions can be used to give values to the data items in an object

Destructors

We have seen that a special member function, the constructor, is called automatically when an object is first created.

There is also another function that is called automatically when an object is destroyed.

Page 18: Objects and Classes Part II CIS 61. Constructors We saw before two ways that member functions can be used to give values to the data items in an object

Destructors

Destructors have the same name as constructors except destructors are proceeded with a tilde “~”.

class Foo {private:

int data;public:

Foo() : data(0){ }

~Foo(){ }

};

Page 19: Objects and Classes Part II CIS 61. Constructors We saw before two ways that member functions can be used to give values to the data items in an object

Destructors

Like constructors, destructors do not have a return value.

They take no arguments, the thought is that there is only one way to destroy something.

The most common use for a destructor is to free up memory.

This will become more important in chapter 10 when we learn about pointers.

Page 20: Objects and Classes Part II CIS 61. Constructors We saw before two ways that member functions can be used to give values to the data items in an object

Objects as Function Arguments

Our next example adds some parts to the englobj example. We will demonstrate some new aspects of classes: constructor overloading, defining member functions outside the class, and objects as function arguments.Let’s look at “englcon.cpp”.

Page 21: Objects and Classes Part II CIS 61. Constructors We saw before two ways that member functions can be used to give values to the data items in an object

Overloaded Constructors

It would be convenient to be able to give variables of type Distance a value when they are first created.We would like to be able to use definitions like: Distance width(5, 6.25);This would define an object, width, and also initialize it to a value of 5 for feet and 6.25 for inches.

Page 22: Objects and Classes Part II CIS 61. Constructors We saw before two ways that member functions can be used to give values to the data items in an object

Overloaded Constructors

To do this we write a constructor like:

Distance(int ft, float in) : feet(ft), inches(in)

{ }

This sets the member data feet and inches to whatever values are passed as arguments to the constructor.

Page 23: Objects and Classes Part II CIS 61. Constructors We saw before two ways that member functions can be used to give values to the data items in an object

Overloaded Constructors

But what happens now if we want to define variables of type Distance like we did in our last example, without initializing them like “Distance dist1, dist2;”In that example there is not a constructor but they still worked.How did they still work?

Page 24: Objects and Classes Part II CIS 61. Constructors We saw before two ways that member functions can be used to give values to the data items in an object

Overloaded Constructors

It turns out that there is a implicit no-argument constructor that is built into the program automatically by the compiler, and it’s this constructor that created the objects, even though we didn’t define it in the class.

This no-argument constructor is called the default constructor.

Page 25: Objects and Classes Part II CIS 61. Constructors We saw before two ways that member functions can be used to give values to the data items in an object

Overloaded Constructors

Often you will want to initialize data members in the default (no argument) constructor as well.

If we let the default constructor do it, we don’t really know what values the data members may be given.

If we care what values they may be given, we need to explicitly define the constructor.

Page 26: Objects and Classes Part II CIS 61. Constructors We saw before two ways that member functions can be used to give values to the data items in an object

Overloaded Constructors

For our last example we would do this:

Distance() : feet(0), inches(0.0) //default

{ } //no function body, does nothing

Page 27: Objects and Classes Part II CIS 61. Constructors We saw before two ways that member functions can be used to give values to the data items in an object

Overloaded Constructors

The data members are initialized to constant values.

In this case the integer value 0 and the float value 0.0 for feet and inches.

Now we could use objects initialized with the no-argument constructor and be confident that they represent no distance, rather than some arbitrary value.

Page 28: Objects and Classes Part II CIS 61. Constructors We saw before two ways that member functions can be used to give values to the data items in an object

Overloaded Constructors

Since now we have 2 constructors for the same class, we could say that the constructor is overloaded.

Which of the two constructors is executed when an object is created depends on how many arguments are used in the definition.