#include <iostream>
int main() {
std::cout << "Hello traveller, here's a towel for protection. Take care of yourself out there." << std::endl;
}
| Weekly Notes | Assignments | Labs |
| ------ | ------ | ------ |
| [Week-1](#Week-1) | [Homework01](https://github.com/2023-Spring-UAF-CS202/hw01-sowens23) | [Lab00](https://github.com/2023-Spring-UAF-CS202/lab00-sowens23) |
| [Week-2](#Week-2) | [Homework02](https://github.com/2023-Spring-UAF-CS202/hw02-sowens23) | [Lab01](https://github.com/2023-Spring-UAF-CS202/lab01-sowens23) |
| [Week-3](#Week-3) | [Homework03](https://github.com/2023-Spring-UAF-CS202/hw03-sowens23) | [Lab02](https://github.com/2023-Spring-UAF-CS202/lab02-sowens23) |
| [Week-4](#Week-4) | [Homework04](https://github.com/2023-Spring-UAF-CS202/hw04-sowens23) | [Lab03](https://github.com/2023-Spring-UAF-CS202/lab03-sowens23) |
| [Week-5](#Week-5) | | |
| [Week-6](#Week-6) | | [Lab05](https://github.com/2023-Spring-UAF-CS202/lab05-sowens23) |
| [Week-7](#Week-7) | | |
| [Week-8](#Week-8) | | |
| [Week-9](#Week-9) | | |
| [Week-10](#Week-10) | | |
| [Week-11](#Week-11) | | |
| [Week-12](#Week-12) | | |
| [Week-13](#Week-13) | | |
| [Week-14](#Week-14) | | |
| [Week-15](#Week-15) | | |
- Empty and sad.
Sample Code : No Overview : Top
- Talked about HW04
Sample Code : No Overview : Top
- Passing functions to as a parameter
- void firstFunction(std::function<void(int)> secondFunction) {...}
- When you call a function with a parameter
- A copy constructor must exist for you to pass a class object.
- A copy constructor does not exist for a virtual class
- You can pass as a reference to bypass this
- Using the '&' reference operater to pass an object is common practice
- Not using & uses the COPY Constructor to pass an identical copy of the object
```
class Animal {
public:
virtual void speak() = 0;
}
class Cat : public Animal {
public:
//Automatically created if we don't make it
//Cat() {}
//~Cat() {}
//Cat(const Cat& other) {}
void speak() override { // Override not necessary
cout <<"meow">>;
}
}
//This line below is the focus of this example.
//You cannot call Animal animal as a parameter here because a virtual class DOES NOT have a copy constructor for this function parameter to COPY. So you MUST pass this as a reference instead.
// The '&' is necessary to pass as reference here.
void speakTo(Animal& animal) {
animal.speak();
}
int main () {
Cat garfield;
speakTo(garfield);
}
```
Zoom - Assigned and reviewed HW03 concepts - virtual void speak() = 0; - = 0, means that when a child class inherits this, it must be overridden, or the class will remain virtual.
Sample Code : No Overview : Top
- Vector is actually a brilliant way of safetly managing 'chunks' of memory.
- When you create a 'new' int pointer int* p = new int, it allocates enough bits for you to store a single int variable, but if you call p[-1], you can alter a peice of memory from that pointer that is not supposed to be changed.
- When managing a vector you basically will only ever be able to access those same 'chunks' of memory, but with limitations making it more safe. You can't access a [-2] location of a vector and alter the memory value of 2 locations before it in the heap.
- You can initialize a pointer to nowhere 'double* pd0 = nullptr;'
- Nullpointer check [2] 'if (pd0 != nullptr)' and 'if (pd0)'
- '->' '.' Arrow and . Notaion are often called 'member access operators'
- When an object...
- is created: At minimum it's default constructor WILL run.
- becomes out of scope: At minimum, it's deconstructor WILL run.
- If an object has allocated memory, it should be deleted at end of scope.
- scope is defined by the parameters of the local curly brace
- If you are calling *new*, doing so
- allocates a chunk of memory on the *free store* (aka the heap)
- and then calls the appropriate constructor
- If you all delete, doing so
- recycles the appropriate memory chunk back to the heap
- Ever invocation of *new* should have exacly one invocation of *delete*
- every *new* has a *delete*
- every *new[]* has a *delete[]*
- Generally we want to use these power, inside of a constructor 99% of the time.
- To reiterate: Every class that 'owns' a resource needs a deconstructor
- Read Chapters 11, 17-19
- See "nm -C Task.o"
- See week 13 examples to see how to use new/delete
- Here is an example of a CLASS constructor, and default DECONSTRUCTOR
- Task(std::string message);
- ~Task();
- manual memory management
- 'new' is a constructor that will reserve a pointer address to a chunk of memory
- 'delete' can be used to unallocate and wipe those chunks of memory
- For every "new" pointer you reserve, you must have a "delete" pointer as well
- "delete" should be used inside of a deconstructor
- If you do not delete your memory after using it, this is a "memory leak"
Sample Code : Overview : Top
Zoom - Absent, I was sick
- No notes, turned in test and left.
No Sample Code - No Overview - Top
- Midterm Exam
Zoom - ./part_a ; echo "Return code was $?" - Week 6 Sample code will help finish HW02
Sample Code : Overview NA : Top - Pointers - & notation - * notation - References - The this pointer in a class
- No class
- No notes
No Sample Code - No Overview - Top
- Spring break
- Spring break
Sample Code : Overview : Top
- Formatting of streams using <iomanip>
- The oddity of x++ vs ++x
- understand and be able to explain the why
int x{0};
cout << x++ << endl;
cout << ++x << endl;
Zoom - We went over HitPoints.cpp - We talked about what 'this*' is and why you should use it. - When you use '->' you are referencing an ADDRESS, so this->
Zoom - Unusable notes //+ - * / % - To make a class operator - 3 Ways to use a private variable - Declare them as public (Wrong answer) - Class function to return private values - Allow friends access to private data
Sample Code : Overview : Top
- Reading command line parameters
- `int argc`
- `char **argv`
- Converting an array of C-strings to `vector<string>`
- Basic file I/O
- Read from file using `ifstream`
- Write to file using `ofstream`
- No class
Zoom - Lost notes
Sample Code : Overview : Top
- Classes
- Member variable initialization
- const methods
- Inheritance
* virtual and override
* hierarchy of types with an is-a relationship
- Shape
- Rectangle is-a Shape
- Square is-a Rectangle
Zoom - Lost notes
Zoom
- Reading Command Line Parameters
-int main (int num_args, ) {
- This is how you take in multiple parameters to a program.
- Below is an example of how you can use the Pointer *, this will allow you to the address location of the variable
const char * mystery_variable = "Hello"
const char * something_else = mystery_variable +4;
const char mystery_char = *something_else;
cout << mystery_char << endl;
- One thing to note is that mystery_variable[4] will return an 'o'. mystery_variable will return the 'beginning' of the address, where the following
characters in Hello, will be [0] [1] [2] [3] [4]
- in **argv
- Converting an array of C-strings to 'vector<string>
- Basic file I/O
1. Read from file using 'ifstream'
2. Write to file using 'ofstream'
Sample Code : Overview : Top
- Classes
- Member variable initialization
- const methods
- Inheritance
* virtual and override
* hierarchy of types with an is-a relationship
- Shape
- Rectangle is-a Shape
- Square is-a Rectangle
- Custom ostream <<
- Enum classes
- what benefit do they have
- also, they are usable in switch-case statements
- Custom ostream <<
- Error handling
- throw
- try
- catch
Zoom - Abstract base class 1. We know it's abstract by "virtual" keyword, this means a child class may override this method. 2. The "=0" in conjustion with "virtual" keyword, means that a child class must override it, or it will inheret the virtual class
Zoom - Going over Classes again - We talked about the 4 parts of HW00 - We talked about how to make a Makefile - Classes - Need 4 things to exist; Name, - 'virtual' int area() const = 0 means it doesn't exist unless coupled to another class/function - This is called an Abstract based class, this is incorporeal - int area() const override means that you will be implimenting a virtual variable
Sample Code : Overview : Top
- Review of object-oriented programming from CS 201
- Students created their own custom data structure based on a problem that they came up with
- Sample code for a Hot Air Balloon data structure was provided
- Code for a class was broken into two files
- a .h file that declares the class, but does not implement the body of the member functions
- a .cpp file that implements the bodies of the member functions of the class
- That code was then used in a separate main function in another file
- using #include "a_local_file.h"
- Misc
- #pragma once
- #include
- using
- why not to use using in a header (polluting the namespace)
- use of class keyword to declare a custom data type
- Constructors
- default constructor
- non-default constructor
Example: given either of these two, you should be able to provide the other
Spaceship(string name);
Spaceship enterprise{"NCC1701"};
- Member functions and member variables
- what is a member function
- what is a member variable
- each object has its own copy of each member variable
- use dot syntax to access member functions
- public vs private
Zoom - Classes - #include - using std::runtime_error; - if (test-fails) throw runtime_error("invalid: "+variable-with-issue) - try _{ test your code; } catch (const runtime_error e) { std::cerr << e.what(); - We went over 1> 2> &> and > vs >> - UNIX Philosophy - Every program should know how to take three things from user, or from program - Standard Output - Standard Input - Standard Error - \textcolor{red}{red} - Makefile notes MAKEFILE CXX=g++ CXX=g++ -g -std=c++17 -Wall -Wextra -pedantic
Zoom - Reasons to use Enum class 1. You can create a specific, limited amount of class variables defined in the enum - No more variables can be created afterwards 2. A good way to reduce global variables 3. Much easier to reference using switch-case function
Sample Code : Overview : Top
- Class cancelled: Waiting for material.
- Absent: Waiting for class recording.
Sample Code NA : Overview NA : Top
- Classes and Object Oriented Programming
- A **_function_** is a group of related code
- A **_struct_** is a group of related data
- A **_class_** is related data and code
- Data and responsibilities to do things with that data
- Examples
- Print it to an output stream
- get or set a value
- Classes you are already familiar with
- string
- fstream, ifstream, ofstream, sstream
- vector<...>
- Classes are like types. An _object_ of a class is a variable of that type.
- The parts of a class are called _members_.
- There are _data members_ (variables in the class)
- There are also Member functions_ (functions in the class - sometimes called _methods)
- Class declaration review
class Brick {
public:
int getHealth();
Color getColor();
void setHealth(int health);
void setColor(Color color);
void draw();
private:
int _health;
Color _color;
};
- Types of loops
- For
- For-each
- While
- for (___; ___; ___:)
- Do-while
No Sample Code - No Overview - Top
- Lab 01
- Review chapter 4
- Course policy, schedule, expectations.