From 1a62445e14debcbdc539bebe6ce46e5282c9cee0 Mon Sep 17 00:00:00 2001 From: lshprung Date: Tue, 12 Jan 2021 10:47:12 -0800 Subject: Post-class 01/12 --- 01-07.md | 3 + 01-12.md | 227 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ assignment_0.md | 166 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 396 insertions(+) create mode 100644 01-12.md create mode 100644 assignment_0.md diff --git a/01-07.md b/01-07.md index a10e53c..cfe82ee 100644 --- a/01-07.md +++ b/01-07.md @@ -258,3 +258,6 @@ bool search(vector arr, int item){ //if vector arr{1, 2, 3, 4, 5} search(arr, 1); //O(1) -> Best Case search(arr, 5); //O(N) -> Worst Case +``` + +[01/12 ->](01-12.md) diff --git a/01-12.md b/01-12.md new file mode 100644 index 0000000..b07f77d --- /dev/null +++ b/01-12.md @@ -0,0 +1,227 @@ +[\<- 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 +#include +#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 +``` diff --git a/assignment_0.md b/assignment_0.md new file mode 100644 index 0000000..1e0c28a --- /dev/null +++ b/assignment_0.md @@ -0,0 +1,166 @@ +# Notes from Assignment 0 Videos + +## Tenets of OOP - https://www.freecodecamp.org/news/object-oriented-programming-concepts-21bb035f7260/ + +- Four principles: **encanpsulation**, **abstraction**, **inheritance**, **polymorphism** + +### Encapsulation + +- When an object has private and public states/functions/methods +- Example: A "Cat" class + - The class may have private fields, such as mood, hunger, and energy, and a private function `meow()` + - The class may have three public functions/methods + - `sleep()` will increase the object's energy and hunger + - `play()` will increase the object's mood, decrease its energy, and call the private `meow()` function + - `feed()` will decrease the object's hunger, increase its mood, and call the private `meow()` function + +### Abstraction + +- Concept that objects should only expose high-level mechanisms + - Natural extension of Encapsulation concept +- Concept can be seen in real life, for example, a water cooler + - There is technology going on under the hood, but the end user only sees the lever to pull for water +- Another example: smartphone + - Very complicated under the hood, but at a high level, there is only the home button, volume buttons, and charge input + +### Inheritance + +- Idea to combat the problem that often times, many objects are similar but differ slightly +- Involves creating a parent class and a child class, which is derived from the parent +- Example: We can have a Person class with a name and an email + - Then we can define a Teacher, which has all the fields from the Person class, plus an additional subject field + - We could create child classes from Teacher for Private Teacher and Public Teacher, which inherit the teacher fields with their own fields as well + - We could also define a Student, which has all the fields from the Person class, plus additional class and grades fields + +### Polymorphism + +- Creating a method in a parent that differs slightly between child classes that inherit this method +- Example: There is a Shape parent class that has the method `CalculateArea()` + - The child classes are Triangle, Circle, and Rectangle, so each child class will have its own implementation of the `CalculateArea()` method + +--- + +## C++ Data Types - https://www.softwaretestinghelp.com/data-types-in-cpp/ + +- C++ supports two types of data + - Primitive/Standard data types + - User-defined data types + +### Primitve or Standard Data Types + +|Data Type |C++ Keyword|Value Type | +|--------------|-----------|--------------------------------------| +|Character |char |Character (ASCII values) | +|Integer |int |Numeric whole numbers | +|Floating point|float |Decimal values with single precision | +|Decimal point |double |Double precision floating point values| +|Boolean |bool |True or false | +|void |void |Valueless (no value) | +|Wide character|wchar_t |Character including Unicode strings | + +- Primitves can also be modified in memory length using the following data modifiers: + - signed + - unsigned + - short + - long + +### User-defined Data Types + +- **Typedef** + - Sets an alias for another data type + - Example: + - `typedef int age;` + - The above code creates an alias `age` for the `int` data type + +- **Enumeration** + - Example: + - `enum daysOfWeek {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};` + - The above code creates a data type `daysOfWeek`. It recognizes all the above keywords (they are really aliases for integer values 0, 1, 2, etc.) + - Boolean is, in theory, an enumeration, since it recognizes the keywords true and false + - C++ example: + +``` +#include +using namespace std; + +enum daysOfWeek {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday}; + +int main(){ + daysOfWeek today; + today = Thursday; + cout << "This is day " << today << " of the week"; + return 0; +} + +--- + +//output: +This is day 4 of the week +``` + +- **Class** + - Collection of objects + - Example: a Student class: + +``` +class student{ + char *name; + int age; + + public: + void printDetails(){ + cout << "Name: " << name; + cout << "Age: " << age; + } +} +``` + +- **Structure** + - A Structure is like a Class, but does not support methods (it can only have variables as its members) + - A Class is really just a structure that can support both variables and methods + - Example: + +``` +struct employee{ + char name[50]; + float salary; + int empId; +}; +``` + +## What C++ is and Why - https://www.youtube.com/watch?v=tbNe-3FXqFM + +### C++ in two lines + +- Direct map to hardware + - of infrastructure and fundamental data types + - Initially from C + - Future: use novel hardware better (caches, multicores, GPUs, FPGAs, SIMD, ...) + +- Zero-overhead abstraction + - Classes, inheritance, generic programming, ... + - Initially from Simula (where it wasn't zero overhead) + +### What really matters? + +- People +- A programming language is a tool, not an end goal + - "Ordinary people" want great systems, not programming languages +- Software developers want great tools + - not just programming language features + +### The onion principle + +- Management of complexity + - Make simple things simple! +- Layer of abstraction + - The more layers you peel off the more you cry + +### An engineering approach + +- Principled and pragmatic design +- Progress gradually guided by feedback +- There are always many tradeoffs + - Choosing is hard +- Design decisions have consequences + - Determine what kind of applications can be done well -- cgit