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 branches
  • git merge branchName - creates a new commit with parents that are the latest commits on the current branch and branchName
  • git merge feature
  • git log - shows a history of commits
  • git push - send work to a remote branch
  • git 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

Image|600

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

class Book{
	string title, author;
	int length;
protected:
	int getLength()const {
		return length;	
	}
public:
	Book(string title, string author, int length): title{title}, author{author}, length{length} {}
	virtual bool isHeavy() const {
		return length > 200;
	}
};
 
class Text: public Book{
	string topic;
public:
	Text(string tilte, string author, int length, string topic): Book{title, author, length}, topic{topic} {}
	bool isHeavy() const override{
		return getLength() > 500;
	}
};

Why Book{tilte ,author, length}? Recall: 4 steps of object construction

  1. Space is allocated
  2. Superclass ctor runs
  3. Fields initialize
  4. 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:

Book* b;
string choice;
cin >> choice;
if(choice == "Book") b = new Book{...};
else b = new Text{...};
cout << b->isHeavy() << endl;

b has “two types” associated with it. Static and Dynamic Type of Pointer

  • Static type: b is a Book*, 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