Classes in C++
class Cat : public Animal {
public:
Cat(): name_("jeff"), age_(0) {}
Cat(string n, int a);
private:
string name_;
int age_;
void pet();
}
Cube(const Cube & other) {
//shallow copy example
cube.color_ = other.color_;
//deep copy example (with object where brick is a variable in Cube)
brick_ = other.brick_;
//deep copy example (with ptrs)
brick_ = new Brick(*other.brick_);
//deep copy references don't make sense
}
Automatic copy constructor
Big Three
Cube & Cube::operator=(const Cube & other){
if (this != &other) { //If I’m not copying myself
_destroy();
_copy(other);
}
return *this;
};
delete
or delete[]
void Cube::~Cube() {
delete lengthptr_;
delete[] properties_;
}
Namespaces in C++
Cube
is part of the cs225
namespace)cs225::Cube
)cs225::Cube
or std::cout
#pragma once
namespace cs225 {
class Cube {
public:
double getVolume();
double getSurfaceArea();
void setLength(double length);
private:
double length_;
};
Variables
int
char
bool
float
double
nullptr
Memory
Indirection in C++:
Reference variables
Cube s1; // A variable of type Cube
Cube & r1 = s1; // A reference variable of type Cube
Cube * p1; // A pointer that points to a Cube
Pointers
Differences and trade-offs between types of function calls
Indirection operators:
<aside> 💡 IMPORTANT STUFF:
Sticks to LHS = reference or ptr, Sticks to RHS: indirection operators.
LHS: List & l
= List Reference
List * l
= List Pointer
RHS: &l
= memory address of L (one step away from the data)
*l
returns data at memory address
</aside>
How to approach Pointer and References questions:
Stack memory: each function invocation gets a stack frame. frame is returned (marked as free) when the function returns. when the memory is marked as freed, it can be overwritten.
Heap memory: add stuff to the heap by using the new
keyword. using new
Functions: Calling and Returning
Name | By Value | By Pointer | By Reference | By Const Reference |
---|---|---|---|---|
What is copied | a cube | an address of a Cube * | nothing | nothing |
Do we modify OG object | no | yes if dereferenced | yes | cannot, won’t compile |
Is there always valid object passed? | yes | no | yes (if it is null, no) | yes (if it is null, no) |
Speed | slow (if data is rlly big) | fast | fast | fast |
Safety | safe | unsafe | unsafe | safe |
Runtimes
Array List
insertAtFront
, including running time, resize strategies, and proofs
insertAtIndex
, including running time, on both a sorted and unsorted list
removeAtIndex
, including running time, on both a sorted and unsorted list
insertAfterElement
, including running time, on both a sorted and unsorted list
removeAfterElement
, including running time, on both a sorted and unsorted list
findIndex
, including running time, on both a sorted and unsorted list
findData
, including running time, on both a sorted and unsorted list
Linked List
Operation insertAtFront
, including running time and insertion strategies
template <typename T>
void List<T>::insertAtFront(T & t, unsigned index) {
ListNode * node = new ListNode(t);
node->next = head_;
head_ = node;
}
Operation insertAtIndex
, including running time, on both a sorted and unsorted list
template <typename T>
void List<T>::insert(const T & t, unsigned index) {
ListNode *& node = _index(index);
ListNode * newNode = new ListNode(t);
newNode -> next = node;
node = newNode;
}
Operation removeAtIndex
, including running time, on both a sorted and unsorted list make sure that you DELETE the node
template <typename T>
T & List<T>::remove(unsigned index) {
ListNode *& node = _index(index);
ListNode * temp = node;
T & data = node -> data;
node = node -> next;
delete temp;
return data;
}
Operation insertAfterElement
, including running time, on both a sorted and unsorted list
Operation removeAfterElement
, including running time, on both a sorted and unsorted list
Operation findIndex
, including running time, on both a sorted and unsorted list
Operation findData
, including running time, on both a sorted and unsorted list
Iterators: used to read data from DS in a uniform way.
Trees
Mathematical foundations: