Tuesday
Agenda
- Imperative Programming vs Object Oriented Programming
- What is an Object?
- Syntax in C++
- Data Encapsulation
Our C++ programs so far consist of a sequence of statements that contain:
- variables -- bucket that contains data (it "knows stuff")
- functions -- sequence of instructions that accomplishes a task (it "does stuff")
Object Oriented Programming
Objects are "blobs" that contain a bit of data and a bit of code
- Since they contain data (attributes or member variables), they "know stuff".
- Since they contain code (methods), they can "do stuff".
A Class is the blueprint of an object. It describes the attributes and implementation of the behaviors.
An Object is an instance of a class which (usually) holds explicit values for each attribute.
C++ Class Syntax
Example of a Point class.
Topics covered:
- Basic class syntax -- don't forget the semi-colon at the end
- private vs public attributes and methods
- private: implementation details not part of user accessible interface
- public: can be seen/called by user of object.
Having private attributes and methods enables the programmer to hide the implementation details from the user (abstraction) and helps to ensure that the object is used in a controlled way (encapsulation)
- Constructors and Destructors
- The constructor has the same name as the class and initializes the member attributes
- The destructor is mainly used to free the memory that was dynamically allocated over the lifetime of the object. More on this later.
- Accessing attributes and running methods from objects
Thursday
Agenda
- Pointers
- Method Stack Diagram
- Dynamic Memory
Pointers
- All variables in your program are stored at a specific location in memory. These 'locations' in memory are called memory addresses.
- A pointer is a variable that contains a memory address. We usually depict them with an arrow pointing to the variable or object whose address they store.
- There are two operators related to pointers in C++:
- The
&
operator, called address of operator, returns the address of any variable.
You can think of it as the "give me an arrow to" operator.
- The
*
operator, called dereference operator, returns the value at the address stored by a pointer variable, or refers to the box at that address.
You can think of it as the "follow the address of" operator.
- In C++, the type of a pointer depends on the type of the variable or object it points to. We denote the type of the pointer by adding a star (
*
) to the type of variable or object it points to.
For example, int*
is a pointer to an int
, string*
is a pointer to a string
, int**
is a pointer to a pointer to an int, etc.
-
nullptr
is a special value we assign to a pointer to signify that the pointer is pointing nowhere. Dereferencing a nullptr
results in a memory error.
- When dynamically allocating memory (see next topic),
new
returns a pointer type
- Accessing an attribute variable from a pointer to an object:
(*p).getX()
? Yuck
p->getX()
? OK, but I’m going to mix these up.
- More examples with the Point class.
Dynamic Memory
- 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