Announcements
- 6 people not signed into Piazza - Activation email sent Thursday night
- Register your clicker
- still missing a few students
- Update github profile with name/email
- lab01 due Sunday night
- partnered labs start next Monday
- partners strongly encouraged
- you can select, or we can pair
- Questions
This week
- Object Oriented programming in C++
- Basic Classes
- Polymorphism
- Encapsulation
Imperative programming
- variables know stuff - nouns/no action
- functions do stuff - verbs/no memory
OOP - Object Oriented programming
C++ Object Syntax
- Class declaration
- Creating objects of a class -> Constructors
- calling methods
Class declaration
class ClassName {
private:
// data or methods you want to protect from external access
public:
// data or methods the class-user can access
}; // this semicolon is required
- data in a class are declared just like variables, but in the
class
block.
- methods are declared just like functions, but in the
class
block.
- Constructor
- has same name as class. Used to make new Objects
- Destructor - destroys instance of a class. frees dynamic memory. More on this later
- Accessors/getters: query properties of class
- Mutators/setters: modify properties of an object
Wednesday
Exercise: worksheet
- Look at the Point class handout
- Constructor syntax
Point p(3,2);
or <ClassName> <varName>(<params>)
- Unnamed object for e.g., temp use or passing to a function:
Point(3,2)
- Clicker Questions
- Modify constructor to redeclare member variables. What happens?
- separate specification from implementation
- can read
.h
file and understand how to use class
- implementation might be longer, difficult to understand, in a different file
.cpp
, or even precompiled into a library .o, .a, .so
Sample test
- Show compilation
clang++ -c point.cpp
clang++ -o testPoint point.o testPoint.cpp
- Compiling to object file can speed builds if point code doesn’t change
point.cpp
includes point.h
, compiler will automatically read this in when seeing the #include
A Diversion into pointers
- A variable is a named container that can store a value.
- In a statically typed language like C++, each type has a fixed size in terms of number of bytes needed to store a value of that type
- The computer has a large address space in which these container can be placed.
- Containers should not physically overlap. Good idea for private properties, seats at sharples, classes on your schedule.
- So in addition to a value, each declared variable also has an address
- The address in memory is a numeric value. Hmm. Can we store this value in a variable? Yes!
- What is the type of memory address of a container? A pointer!
- For any type, we declare a related type that is pointer to the address of a container of given type. We do this by appending a
*
to the name of an existing type.
type |
ptr type |
int |
int* |
float |
float* |
string |
string* |
Point |
Point* |
- The
&
operator returns the address of any variable
- The
*
operator returns the value at the address stored by a pointer variable.
int a=5;
int* p;
p=&a; //p is the address of a
//prints out the value of the variable stored at the address stored by p.
cout << *p << endl;
*p = 6; //changes the value of a
nullptr
is a special empty address (a black hole), can’t modify the contents by dereferencing a nullptr.
- If you took CS31 or are familiar with
NULL
, please use nullptr
in CS35
Friday
Missing Clicker accounts
There are 36 registered clickers and 38 students in the class. Please purchase and register your clicker.
The following clicker IDs are not registered to a student name. Please connect your clicker to this course.
207, 35C, 61A, A10, F66
Exercise: worksheet
- Pointer worksheet
- clicker questions
Dynamic memory
- Organization of memory
- code
- global data
- function call stack/grows down
- heap/Dynamic Data/grows up
- Static data
- sizes known at compile time
- automatically placed on function call stack when function starts
- automatically removed when function returns
- Dynamic memory
- compiler doesn’t know how much space is needed
- special call
new
can allocate space at runtime, placing it on the heap
- not automatically created nor destroyed
- can be advantageous
- can be annoying.
- need to explicitly free memory with matching
delete
- Dynamic memory errors
- leaks, forgetting to delete
- losing track of all references to dynamic memory
- orphaned memory, unreachable
- deleting the same allocation twice
- Two uses
- dynamic arrays
int* arr = new int[size]; delete [] arr
- dynamic objects
Point* p = new Point(3,2); delete p
new
returns a pointer type
(*p).getX()
? Yuck
p->getX()
? OK, but I’m going to mix these up.
Coming up. Using pointers for Polymorphism
- Shapes
- circle
- rectangle
- triangle
- Shape properties
- Want an array of shapes that can store circles, rectangles, triangles.
- Want a
getArea()
method that does the right thing on each shape
- C++ arrays can only store items of one type.