You are on page 1of 77

CISP 400

Object Oriented
Programming in C++
Professor Fowler
Last Time

2
Agenda
Ch 7 - Object Oriented Programming
7.1 Abstract Data Types 7.8 Private member Functions

7.2 Object-Oriented 7.9 Passing Objects to Functions


Programming
7.10 Object Composition
7.3 Introduction to Classes
7.11 SKIP
7.4 Creating and using Objects
7.12 Structures
7.5 Defining Member Functions
7.13 More on Enumerated Data
7.6 Constructors Types

7.7 Destructors 8.13 Arrays of Objects

3
Theme for Today

4
“Initializing Classes”

5
Chapter 7
Object Oriented
Programming

6
Agenda
Ch 7 - Object Oriented Programming
7.1 Abstract Data Types 7.8 Private member Functions

7.2 Object-Oriented 7.9 Passing Objects to Functions


Programming
7.10 Object Composition
7.3 Introduction to Classes
7.11 SKIP
7.4 Creating and using Objects
7.12 Structures
7.5 Defining Member Functions
7.13 More on Enumerated Data
7.6 Constructors Types

7.7 Destructors 8.13 Arrays of Objects

7
Agenda
Ch 7 - Object Oriented Programming
7.1 Abstract Data Types 7.8 Private member Functions

7.2 Object-Oriented 7.9 Passing Objects to Functions


Programming
7.10 Object Composition
7.3 Introduction to Classes
7.11 SKIP
7.4 Creating and using Objects
7.12 Structures
7.5 Defining Member Functions
7.13 More on Enumerated Data
7.6 Constructors Types

7.7 Destructors 8.13 Arrays of Objects

8
Accountability Groups

9
Purpose

• Support each other around a common goal -


getting a good grade in this class.

10
First Meeting

• Exchange contact info (OK to use Canvas)

• Decide Leader (rotating, selected?)

• Decide on Group Name


(1st choice + Backup choice)

11
Activities
1. Quick Check - 1/2 words on how you’re doing.

2. Record your weekly goals (individual)

3. Did you achieve last week’s goals?


Yes - Lavish Praise
No - “How can we help?”

4. Take Roll (privately chat me who’s missing)

5. Make any arrangements to meet outside class.


- Missing Material
- Peer Review
- Study Groups

12
Debrief
Programming Exercise
Coding Exercise

Create a Date class with private member ints day,


month, and year. Add a setDate() method to set the
date. Add a print public member function to display
the date.

Write a test rig to exercise this class.

14
7.6 Constructors

18
Constructors

• Run when the class is instantiated.

• Same name as the class.

• No return type at all (not even void).

19
• Does this mean classes which we have not created
a constructor, do not have constructors?

20
Constructor Use Case

Set up connections between objects (DB, network,


etc)

Initialize data.

Serve as an interface.

21
Constructors as an interface

We usually have data we want to initialize with when


we instantiate our constructor.

What if we don’t have all the data? Does that mean


we can’t instantiate?

22
Overloading Constructors
• Computer always makes a default constructor - unless you write
ANY other constructor.

• Meaning: you will not have a default constructor if you’ve


overloaded it.

• Meaning: you cannot create an object when you do not pass


any values upon instantiation. Refuses to compile.

• Typical default constructor is not passed any arguments.

• However, we can have a default constructor with arguments if


we set the parameters with default values.

• We can only have one default constructor. We can have


unlimited number of overloaded constructors.

24
Bottom Line

• If you are adding a constructor which accepts


arguments, you will also need to create a default
constructor.

• A default constructor does not accept any


arguments, unless you make default parameters
available.

25
Constructors, functions
and methods, Oh my!

27
• Distinguishing between various constructors and
methods can be confusing.

28
Class ABC {
};

int main() {
ABC x; // Default - works fine
ABC x(); // Error - thinks it’s a method
ABC x(10) // Fine - overloaded default
Exercise
C++ Debugging
Exercise Goals

• Debugging Practice

31
Debug Exercise

• Correct the following code.

• We’ll spend about 5 minutes on this, then we’ll go


over it.

• You do not need to turn this in.

32
7.7 Destructors

34
• Constructors!

• Destructors!

Photo Credit: https://www.google.com/url?sa=i&rct=j&q=&esrc=s&source=images&cd=&ved=0ahUKEwink9zW1u_YAhWlc98KHYUODrgQjRwIBw&url=http%3A%2F%2Fitsallfreeonline.com%2Ffree-busch-beer-


swag%2F&psig=AOvVaw1xDLttWe8cGJb7-lJiD7_B&ust=1516851416043160
10.11 Pointers to Objects
as Function Parameters

37
• Our objects use regular variables all the time (ints,
floats, etc).

• Can we use pointers with objects?

• Yes, but …..

38
Nothing we haven’t
seen before.
Now, we go ahead an access it …
Now, we go ahead an access it …
Now, we go ahead an access it …

The problem is the dot operator (.) binds tighter


then the dereference operator (*).
How do we fix that?
We fix this the same way we did when we had
arrays - we use parentheses.
We fix this the same way we did when we had
arrays - we use parentheses.
We fix this the same way we did when we had
arrays - we use parentheses.

This notation is a pain - we have some syntactic sugar


for that.
Structure Pointer Operator (->)
We fix this the same way we did when we had
arrays - we use parentheses.

This notation is a pain - we have some syntactic sugar


for that.
Structure Pointer Operator (->)
Does this work with the heap?

48
10.12 Selecting
Members of Objects

50
What do we do when we need to access pointers
inside objects?
Programming Exercise
Activity
Coding Exercise
Create a Date class with private member ints day,
month, and year. Add a print public member function
to display the date. Use constructors to initialize to
specified values or default values if nothing is
specified. Set to error date of 99/99/9999 if any
invalid date values are used (don’t make this an
exhaustive test).

Write a test rig to exercise this class.

54
What’s this section
about?

55
Theme for Today

“Method.”

56
Agenda
Ch 7 - Object Oriented Programming
7.1 Abstract Data Types 7.8 Private member Functions

7.2 Object-Oriented 7.9 Passing Objects to Functions


Programming
7.10 Object Composition
7.3 Introduction to Classes
7.11 SKIP
7.4 Creating and using Objects
7.12 Structures
7.5 Defining Member Functions
7.13 More on Enumerated Data
7.6 Constructors Types

7.7 Destructors 8.13 Arrays of Objects

57
What’s Next?

58
Agenda
Ch 7 - Object Oriented Programming
7.1 Abstract Data Types 7.8 Private member Functions

7.2 Object-Oriented 7.9 Passing Objects to Functions


Programming
7.10 Object Composition
7.3 Introduction to Classes
7.11 SKIP
7.4 Creating and using Objects
7.12 Structures
7.5 Defining Member Functions
7.13 More on Enumerated Data
7.6 Constructors Types

7.7 Destructors 8.13 Arrays of Objects

59
Agenda
Ch 7 - Object Oriented Programming
7.1 Abstract Data Types 7.8 Private member Functions

7.2 Object-Oriented 7.9 Passing Objects to Functions


Programming
7.10 Object Composition
7.3 Introduction to Classes
7.11 SKIP
7.4 Creating and using Objects
7.12 Structures
7.5 Defining Member Functions
7.13 More on Enumerated Data
7.6 Constructors Types

7.7 Destructors 8.13 Arrays of Objects

60
Chapter 12 quiz SUNDAY

61
10.13 Smart Pointers

62
Use Case

We’ve seen the dangers of using raw pointers.

Problems of dual ownership, dangling pointers and


memory leaks cause problems.

Can’t pointers manage themselves?

63
Modify This

65
Exercise
Programming Exercise
Exercise Goals

• Increase Topic Mastery

• Construct Knowledge

• Give you data to evaluate how well you are


learning.

68
Programming Exercise
• Get 10-2demo.cpp working

• Explain

• Which methods are setters and getters?

• What’s the difference between print and other


getter?

• What’s a constant member function?

• What’s going on in GetTime()?


69
Exercise
C++ Debugging
Exercise Goals

• Debugging Practice

71
Debug Exercise

• Correct the following code.

• We’ll spend about 5 minutes on this, then we’ll go


over it.

• You do not need to turn this in.

72
Programming Exercise
Activity
Exercise Goals

• Increase Topic Mastery

• Construct Knowledge

• Give you data to evaluate how well you are


learning.

75
Programming Exercise
• With 10-2demo.cpp

• Replace SetTime() method with a constructor.

• Overload the constructor you created above with


one which will set the time to -99:-99:-99 if no
data is supplied to it.

• Construct a test rig to demo these methods.

76
Coding Exercise

Create a Date class with private member ints day,


month, and year. Add a setDate() method to set the
date. Add a print public member function to display
the date.

Write a test rig to exercise this class.

77

You might also like