Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations

Presentation on theme: "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."— Presentation transcript:

1 Objects and Classes Part II CIS 61

2 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 its first created, without requiring a separate call to a member function.

3 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.

4 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.

5 A Counter Example Each time one of these events take place, the counter is incremented (1 is added to it). Lets 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.

6 A Counter Example But remember that global variables should be avoided in most cases and can complicate a programs designed and can be accessed accidentally. Lets look at the example counter.cpp.

7 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.

8 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

9 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.

10 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.

11 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.

12 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.

13 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, theres no program for it to return anything to. This is the second way the compiler knows that it is a constructor.

14 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.

15 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) { }

16 Initializer List If multiple members must be initialized, theyre separated by commas. Here is what that would look like: someClass() : m1(7), m2(33), m3(4) { }

17 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.

18 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() {} };

19 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.

20 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. Lets look at englcon.cpp.

21 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.

22 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.

23 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?

24 Overloaded Constructors It turns out that there is a implicit no- argument constructor that is built into the program automatically by the compiler, and its this constructor that created the objects, even though we didnt define it in the class. This no-argument constructor is called the default constructor.

25 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 dont 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.

26 Overloaded Constructors For our last example we would do this: Distance() : feet(0), inches(0.0) //default {} //no function body, does nothing

27 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.

28 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.

Download ppt "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."

Similar presentations

Ads by Google