CheatSheet1
CheatSheet1
Dog::Dog() {
name = “fido”;
breed = “peekadoodle”;
weight = 20;
}
Dog::Dog(string n, string b, int w) {
Name = n;
Breed = b;
Weight = w;
}
String Dog::speak() {
cout << “Bark, bark!”
return (“Woof”);
}
void Dog::changeWeight(int x) {
weight += x;
cout << name << “s new weight is “ << weight << endl;
}
Dog::~Dog() {
cout << “destroying “ << name << endl;
}
Constructors are called when you make an object. In other words, in either main or another function or another
class (NOT within the Dog class!!) you’d call the constructor as follows:
Dog d(“Killer”,”Pomeranian”,6);
If you wanted a pointer to a dog (you wanted the dog on the heap, you’d do:
Dog *d = new Dog(“Fluffy”,”German Shepherd”,65);
In the above class definition, the Dog constructor is overloaded because there are two different constructors
(exact same name) with different input parameter types. When a constructor is called, the one with the matching
input parameters is called.
Dog d; //calls the first constructor
Dog d(“Spot”,”Dalmation”,45); // calls the second constructor.
The constructor is overloaded.
The destructor is usually used for any field on the heap. In this case, the destructor just lets you know when a
particular dog object goes out of scope.
Linked Lists:
Basic DNode Class header file:
class DNode {
friend class DLL;
int data; // data could be any type – it could even be other class objects!
DNode *prev;
DNode *next;
public:
DNode();
DNode(int x);
};
Basic DLL Class header file:
class DLL {
DNode *first;
DNode *last;
int size;
public:
DLL(); // constructor - initializes an empty list
DLL(int x); // constructor, initializes a list with one new node with data x
void push(int x); // does what you'd think
int pop(); //does what you'd think
void addAtFront(int x); // add a new node to the beginning of the list
void addFirst(int x); // add the very first node to the list
void insertAt(int ind, int x); //insert x at index ind
int removeAtK(int ind); // remove the node at index ind and returns its data
void reverse(); // reverse the list
void removeX(int x, int *ind); // removes the first occurrence of x from the list and
//uses call by pointer to set ind to be the index of the first occurrence of x in the list.
void printList();
int remove(int d); //removes the first occurrence of d from the list
~DLL();
};
And the method definition of printList() is:
void DLL::printList() {
DNode *tmp = first;
while (tmp!= NULL) {
cout << tmp->data << “, “;
tmp = tmp->next;
}
cout<< endl;
}
MISC Info:
Matrix[x][y] – x is the row, and y is the column (yes, it’s the reverse of every other matrix we deal with).
&x gets the address of a variable x in memory, *x holds the address of a variable in memory.
IF you have a pointer to a class object, then accessing the fields and methods is done with ->
Stack is memory controlled by the compiler. We don’t control it. Everything that isn’t allocated (newed) by us is
on the stack. Everything that is allocated (newed) by us is on the heap.
Big O Order (worst to best):
O(n2) -> O(n log2n) ->O(n)->O(log2n)->O(1)