CS247 Lecture 9
Source Control
-
manage source code for your program (version1, version2)
-
uploading code to dropbox so coworkers can see
-
see changes, collaborate w coworkers, track changes over time
-
GIT - Linus Torvalds
-
Historically, people have used SVN, Subversion, Mercurial.
- Most people nowadays just use git.
-
git init from within a directory
-
git has a staging area - prepare which parts will be committed.
-
commit unit of work - commit early and commit often to understand how it is developed over time
-
git add
- allows you to add first to the staging area -
git status
- let’s us see which branch we’re going -
git add - p
- let’s you stage interactively -
git diff
- to see the difference between unstaged files and the most recent commit -
git diff --staged
to see the difference between all unstaged and staged files
One thing we may want to do is develop features or work refactor separately from other development.
git brand branchName
git checkout branchName
- to switch between branchesgit merge branchName
- creates a new commit with parents that are the latest commits on the current branch and branchNamegit merge feature
git log
- shows a history of commitsgit push
- send work to a remote branchgit pull
- pulls work from remote branch, merge as well
Example: Currently on master branch
git branch feature
git checkout feature
git commit
git checkout master
git commit
System Modelling Goal: Visualize classes and the relationships between them at a high level
Popular Standard: UML - Unified Modelling Language
Modelling Class Relationships:
- Composition, “owns-a” relationship
If A “owns-a” B: Generally:
- B does not have an independent existence from A
- If A dies, then B dies
- If A is copied, B is copied as well (deep copy)
Typically, this is implemented in C++
via object fields.
Aggregation, “has-a” relationship If A “has-a” B then generally:
- B has an independent existence outside of A
- If A dies, B lives on
- If A is copied, B isn’t (shallow) Generally Implemented via references or non-owning pointers. (one you don’t delete)
Example: Quest-like System If a student dies, university lives on,. If we copy a student, don’t copy whole uni.
Finally, specialization: “is-a” relationship One class is a “version” of another class.
- Typically - subclassing or subtyping relationship
- If B “is-a” A, then we should be able to substitute something of type B, wherever we expect something of type A.
In C++
, achieve this via public inheritance
Why Book{tilte ,author, length}
? Recall: 4 steps of object construction
- Space is allocated
- Superclass ctor runs
- Fields initialize
- Ctor body runs
We cannot set title, author, length because they are private to Book. And if we do not specify how or initialize the Book portion, it will use Book’s default ctor, won’t compile if default ctor does not exist.
C++
Virtual and Overriding
Virtual keyword is used for achieving dynamic dispatch for polymorphic types.
- Dynamic dispatch: Choosing which function to run at run-time as opposed to compile time.
- Polymorphic types: one type that can take on multiple different object types.
Consider the following:
b
has “two types” associated with it. Static and Dynamic Type of Pointer
- Static type:
b
is aBook*
, compiler knows this. - Dynamic type: describes what type
b
is pointing to at run-time. May depend on user-input, cannot be determined by the compiler.
Next: CS247 Lecture 10