Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,

Similar presentations


Presentation on theme: "Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,"— Presentation transcript:

1 Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors, accessors, mutators, constant member functions simple examples with classes parameter passing default constructors, destructors, assignment templates

2 Basic class syntax class point { public: point() { x = y = 0.0; } point(float p, float q) { x = p; y = q; } float getx() {return x; } void setx (float p) { x = p; }... private: float x, y; } Information hiding: restrict the number of places that can modify data. If class A uses B, an internal change in B should not affect the way A uses B. constructors overloading

3 Basic class syntax Each instance of a class is called an object. An object can be constructed by calling the constructor. Point P(0.3, 0.5); Accessing a data field f of an object x is by x.f ; if this access is attempted by a function that does not belong to the class, you will get syntax error if f is a private field. Accessing a function f() that is supported by the class (to which x belongs) is done by using the syntax: x.f(); scope resolution: to explicitly refer to f() that belongs to class1, use the syntax class1::f()

4 Basic class syntax Basic rule: keep the data private, and provide access to the data through functions that can get and set the data. accessor: provides (read) access. mutator: allows the data value(s) to be changed. one way to make sure that the code for accessor does not change the data elements of the object is to specify in the prototype: int getX ( ) const const indicates that it can’t mutate the data.

5 Vectors vectors are safer versions of arrays (which don’t have size checks, assignment using = etc.) vector is part of STL. syntax for vector: vector A( 100 ) - defines a vector of integers of size 100. the function size() can be used to access the size of a vector. the item stored in index j can be accessed by A[j] just as in an array. time to access is constant just as for an array.

6 Complete program for merge sorting Recall the merging procedure that we presented last class. Write a program to implement merge sorting that uses the merging procedure. MERGE SORT(Input vector A, int low, int high) { s = A.size(); if (s < 2) return; int mid = (low + high) / 2; recursively perform MERGE SORT(A, low, mid) and MERGE SORT(A, mid + 1, high); MERGE the two segments into a single sorted array; }

7 Sec 1.5 C++ pointers – an example Consider the following program segment: #include int main() { int * p; int * q; int x; p = &x; *p = 6; cout << x << endl; q = p; *q = 8; cout << *p << endl; q = new int; *q = 9; cout << *q << endl; return 0; } What is the output produced by this program? If two pointers point to the same variable, they are aliases of each other. Aliases make it harder to read programs.

8 Memory leak and garbage collection memory used for local variables in a function will be cleared up when the call to the function ends. But dynamically created objects will not be automatically deleted. int * p; p* = 6; delete p; Now the memory address containing 6 is inaccessible. This data has become “garbage”. How to do garbage collection? Some languages like Java provides garbage collection, but c++ requires the programmer to do it.

9 How to destruct a linked list? void some_list_op ( ) { node temp();... // now temp is not needed delete temp; // obviously wrong The correct way to do this is: temp1 = temp; while (temp1 != 0) { temp1= temp1->next; temp->next = 0; delete temp; temp = temp1; }

10 Parameter passing call by value and reference: What are the outputs of the following program? #include using namespace std; int f(int x) { x++; cout << x << endl; return x*x; } int main() { int x = 5; cout << f(x) << endl; cout << x << endl; } #include using namespace std; int f(int& x) { x++; cout << x << endl; return x*x; } int main() { int x = 5; cout << f(x) << endl; cout << x << endl; }

11 Parameter passing In c++, all parameter passing is done by value unless & is used preceding the name of the variable. Advantage of call by value: safer easier to understand and debug Disadvantage of call by value: not efficient (copying costs both in time and space) especially a problem when a large object is passed as parameter. call by reference is complementary

12 Parameter passing call by const reference: getting advantages of both conventions should be used when passing objects (especially when it is large) what about return value? inside a procedure, an object is being built to be returned. Suppose the object being returned is one of the parameters. Now an alias is created. Next example explains this.

13 Parameter passing call by const reference: getting advantages of both conventions should be used when passing objects (especially when it is large)

14 Templates generic programming function templates: template provides the basis for the actual function. The code is obtained by substituting the generic type by the specific type used in the calling code segment. Example: template for findMax in a vector: template Comparable findMax (const vector & a) { int maxIndex = 0; for (int j = 1; j < a.size(); ++j) if (a [ maxIndex ] < a [ j ]) maxIndex = j ; return a [ maxIndex ] ; }

15 Templates template Comparable findMax (const vector & a) { int maxIndex = 0; for (int j = 1; j < a.size(); ++j) if (a [ maxIndex ] < a [ j ]) maxIndex = j ; return a [ maxIndex ] ; } int main() { vector v1( size1 ); vector v2 ( size2 );..... cout << findMax( v1 ) << endl; cout << findMax( v2 ) << endl;.... } Any type for which < is defined can be used

16 Implementation of a list class list class with the following functionality: list is singly-linked, with a head pointer constructor(s) functions: insert(int item, int posn) length() delete(int posn) search(int item)

17 Implementing insert To insert a key x as the k-th key a k g c x = ‘b’ and k = 5 a k g c b

18 Recursive version of insert void insertLast(Node* head, int x, int k) { // insert x as the k-th item of the list // assume that the list has at least k – 1 items Node* tmp = new Node(k); if (k == 1) { tmp->next = head; head = tmp; } else insert(head->next, x, k – 1); }


Download ppt "Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,"

Similar presentations


Ads by Google