C++ Classes and OO More Examples HW2
C++ Classes and OO More Examples HW2
More Examples
HW2
Objects
C++ and OO
int i , j = 3;
Declares; allocates ; initializes
For a simple native type this happens
automatically via a stack
int main()
{ int i = 9, j = 3;
cout << i is << i << j is << j <<endl;
{ int i = j + 2;
cout << i is << i << j is << j <<endl;
cout << i is << i << j is << j <<endl;
}
Answer
l
i is 9 j is 3
i is 5 j is 3
i is 9 j is 3
point(){ x = y = 0.0;}
or
l point(){ this -> x = 0.0; this ->y = 0.0}
l Or best
l point():x(0.0),y(0.0){}
l
Use
l
Constructor overloading
l
point(double x, double y)
l {this -> x = x; this ->y = y;}
l
More Constructors
Constructors: Initialize
Constructors: Convert
Constructors: Allocate
Also: constructors can check for correctness
Memory management
new - allocator -think malloc()
l delete deallocator think free()
l
~ destructor
l
l
l
l
l
l
l
l
Prepend to slist
l
l
l
l
l
l
l
l
~ destructor
slist::~slist()
l {
l
cout << destructor called << endl;
l
//just for demonstration debug
l
release(); //march thru list with
l
//deletes
l }
l
Unconnected graph
O
O
O
Connected graph
O
O
O
Density is 0.19
l
l
l
l
Quiz:
l
Answer:
l
Details
l
l
l
l
Is_connected
l
l
l
l
l
l
l
l
l
At this point
Open set has node 0 on it
l Question would this work if other node is
selected
l
Add to close
l
l
l
l
l
Add to open
for(int j = 0; j < size; ++j)
l
open[j] = open[j] || graph[i][j];
l
}
l
}
l
Are we done?
l
l
L6 Lists
List Element
l
struct list_element{
list_element(int n = 0, list_element* ptr = 0):
d(n), next(ptr){}
int d;
list_element* next;
};
Or equivalently
l
l
class list_element{
public:
list_element(int n = 0, list_element* ptr = 0):
d(n), next(ptr){}
int d;
list_element* next;
};
Quiz
l
List
l
class list{
public:
list():head(0), cursor(0){}
void prepend(int n); //insert at front value n
int get_element(){return cursor->d;}
void advance(){ cursor= cursor-> next;}
void print();
private:
list_element* head;
list_element* cursor;
};
prepend
l
void list::prepend(int n)
{ if (head == 0)//empty list case
cursor = head = new list_element(n,
head);
else//add to front -chain
head = new list_element(n, head);
}
Quiz : prepend(5)
l
-> 7 -> 3 ##
Answer
l
Draw here----
Print() chaining
l
Use of list
l
int main()
{
list a, b;
a.prepend(9); a.prepend(8);
cout << " list a " << endl;
a.print();
for (int i = 0; i < 40; ++i)
b.prepend(i*i);
cout << " list b " << endl;
b.print();
}
Ans:
l
Simulate here:(run)
More elaborate
class list{
public:
list():head(0), cursor(0){}
list(const int* arr, int n);
list(const list& lst);//copy constructor
...
~list(); //delete
private:
list_element* head;
list_element* cursor;
};
Deep: Pi is transcendental
Deep v. Shallow
l
Copy constructor
l
More code
l
else
{
cursor = lst.head;
list_element* h = new list_element();
list_element* previous;
head = h;
h->d = lst.head->d;
previous = h;
~ destructor
l
list::~list()
{
for( cursor = head; cursor != 0; )
{
cursor = head->next;
delete head;
head = cursor;
}
}
Here the destructor chains through the list
returning each list_element created by a
corresponding new.
int main()
{
list a, b;
int data[10] = {3,4, 6, 7, -3, 5};
list d(data, 6);
list e(data, 10);
a.prepend(9); a.prepend(8);
cout << " list a " << endl;
a.print();
C++ More
HW2 Questions?
l
l
l