summaryrefslogtreecommitdiff
path: root/01-12.md
blob: 50d9d85b9f294539143e8a1f937eab0b71c00a6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
[\<- 01/07](01-07.md)

# OOP: Classes, Objects, and ADTs

## Object Oriented Programming

- OOP is a programming language model organized around **objects** rather than "actions" and **data** rather than logic
- There are 4 major principles that make a language Object Oriented
	- Encapsulation
	- Data Abstraction
	- Polymorphism
	- Inheritance

### Encapsulation

- Hiding mechanism, hiding of data implementation
	- Private variables
	- Public accessor methods

### Abstractions

- A concept or an idea which is not associated with any particular instance
	- Expressing the "intent" and not the "implementation"
	- **Abstract** classes and **virtual** members

### Polymorphism

- One name, many forms!
	- What does `a+b` mean?
		- What is `a` and what is `b`?

### Inheritance

- "is a" or "has a" relationship between objects
	- Dog "is a" mammal
	- Ferrari "is a" car, "has an" engine

## Class

**A class is a new kind of data type**

- A collection of data, such as integers, characters, and so on
- We implement data structures as classes
- A class has the ability to include special functions, called **member functions** which are designed specifically to manipulate the class

### Example: Thinking Cap

```
class thinking_cap{

	public:
		void slots(char new_green[], char new_red[]);
		void push_green() const;
		void push_red() const;

	private:
		char green_string[50];
		char red_string[50];
};
```

- Note: the `const` means that the function cannot change the data in the variables of the class
	- `const` should be specified for all "getter" functions

### Files for Thinking Cap

- The `thinking_cap` class definition, which we have just seen, is placed with documentation in a file called `thinker.hxx` outlined here
- The implementations of the three member functions will be placed in a separate file called `thinker.cxx`, which we will examine in a few minutes

### Using the Thinking Cap

- A program that wants to use the `thinking_cap` must **include** the thinker header file (along with its other head inclusions)
- The program starts by calling the slots member function for student

```
#include <iostream.h>
#include <stdlib.h>
#include "thinker.h"

int main(){
	thinking_cap student;
	thinking_cap fan;

	student.slots("Hello", "Goodbye");
	fan.slots("Go Broncos!", "Boo!");
	student.push_green(); //expected output: "Hello"
	fan.push_green();     //expected output: "Go Broncos!"
	student.push_red();   //expected output: "Goodbye"
}
```

### Thinking Cap Implementation

- The below code belongs in the `thinking_cap` class definition

```
void thinking_cap::slots(char new_green[], char new_red[]){
	assert(strlen(new_green) < 50);
	assert(strlen(new_red) < 50);
	strcpy(green_string, new_green);
	strcpy(red_string, new_red);
}

void thinking_cap::push_green{
	cout << green_string << endl;
}
```

### A Common Pattern

- Often, one or more member functions will place data in the member variables so that other member functions may use that data

### Summary

- Classes have member variables and member functions. An object is a variable where the data type is a class
- You should know how to declare a new class type, how to implement its member functions, how to use the class type
- Frequently, the member functions of an class type place information in the member variables, or use information that's already in the member variables
- In the future, we will see more features of OOP

---

## Class Constructors

- **Constructors** provide an initialization function **that is guaranteed to be called**
- **A constructor is a member function** with **these special properties**
	- A constructor is **called automatically** whenever a variable of the class is declared
	- The **name of a constructor must be the same as the name of the class**
	- A constructor **does not have any return value**
		- You must not write `void` (or any other return type) at the front of the constructor's head

### Example

```
namespace scu_coen79_2A{
	class point{

		public:
			//CONSTRUCTOR
			point(double i_x=0; double i_y=0);

			void shift(double x_amount, double y_amount);
			void rotate90();
			double get_x() const { return x; } //inline function
			double get_y() const { return y; } //inline function

		private:
			double x; //x coordinate of this point
			double y; //y coordinate of this point
	};
}
```

- Notice that the above code has a couple of inline functions
	- It is best practice to only do this for very short functions

### Write a Constructor for Thinking Cap

```
//prototype in the class definition
thinking_cap();

//definition
thinking_cap::thinking_cap(){
	strcpy(green_string, "Hi"); //default green_string is "Hi"
	strcpy(red_string, "Bye");  //default red_string is "Bye"
}
```

### Automatic Constructor

- If you write a class with no constructor,
	- The compiler automatically creates a simple default constructor
	- **This automatic default constructor doesn't do much work**

### Value Semantic

- The value semantics of a class determines how values are copied from one object to another
- In C++, the value semantics consists of two operations:
	- Assignment operator
	- Copy constructor

### Assignment Operator

**Example**
- DS1 is a data structure that includes the names of employees in a hospital
- DS2 is an object that can include names of employees
- We want to assign the values in DS1 to DS2. This is what we do:

```
employeeName DS2;
DS2 = DS1;
```

- The **automatic assignment operator** simple **copies each member variable** from the object on the left of the assignment

**Note**: Automatic assignment operator **does not always work** (future lectures)

### Copy Constructor

**Example**

- DS1 is a data structure that includes the names of employees in a hospital
- We want to create an object DS2 while making it an exact copy of DS1
- This is what we do:

```
employeeNames DS2(DS1);
```

- **A constructor with exactly one argument**
- The data type of the argument is the same as the constructor's class
- Or equivalently

```
employeeNames DS2 = DS1;
```

### C++ Copy Constructor

- C++ provides an **automatic copy constructor**
- The automatic copy constructor initializes a new object by **merely copying all the member variables from the existing object**
- When you implement a class, **the documentation should include a comment indicating that the value semantics is safe to use**

```
//VALUE SEMANTICS for the XYZ class:
	//Assignments and the copy constructor may be used with XYZ objects
```

[01/14 ->](01-14.md)