240 likes | 356 Views
EECS 230 Lectures Series. Final Review. Ying Wu Electrical Engineering & Computer Science Northwestern University yingwu@ece.northwestern.edu. About EECS 230. The goal Getting a good glimpse of C/C++ programming Learning a real useful programming language Understanding software design
E N D
EECS 230 Lectures Series Final Review Ying Wu Electrical Engineering & Computer Science Northwestern University yingwu@ece.northwestern.edu
About EECS 230 • The goal • Getting a good glimpse of C/C++ programming • Learning a real useful programming language • Understanding software design • Accumulating experience • Becoming a junior programmer • What I want to concentrate • Familiarizing C/C++ syntax • Understanding important and core C/C++ concepts • Grasping a tool, i.e., the Debugger • Finding the best way for further study • What are beyond • Data structure • Compiler and OS • Algorithms • Architecture
Grading Scale • MPs and exams [MPs Midterm Final] [30% 30% 40%] • The weights for MPs [MP#1, MP#2, MP#3, MP#4, MP#5, MP#6, MP#7, MP#8] [ 3% 3% 3% 5% 4% 4% 4% 4% ] • Grade scale A/A- > 40% B+/B/B- > 45% C+/C/C- < 15% D you can figure it out!
CTEC survey • Please fill the CTEC survey • Our course number: EECS 230-0-20 • Instructor: • Professor Ying Wu • TAs are: • Ming Yang • David Choffnes • Yao Zhao
What we’ve learned (I) • Basic C/C++ syntax • Data type • Control • if, if/else, switch, while, do/while, for • Pointer, array, reference • Function • C++ Class
What we’ve learned (II) • Basic C/C++ concepts • Function • Call-by-value vs. call-by-reference • What can be returned and what can not be return? • Array and pointers • Relationship? (similarity and dissimilarity) • Data in memory • Local variable and scope (life-cycle) • Pointers and references • What is reference? Why is it special? • Point arithmetic • static • Const
What We’ve learned (III) • Advanced C++ concepts • Dynamic Memory Allocation • Classes with pointer data members • Copy constructor • Operator overloading • Constructors/destructors • When constructors and destructors are called • Linked list
About Final exam • Date: March 16th (Fri) 9 – 11 am • Place: Tech LR4 • Content: • Several straightforward problems • Read code and write results • Write code • For your benefits • Be extremely careful • Write down your intermediate results (as more as you can) for partial credits
Let’s Warm up … • Questions 1: • int a = 2, b = 4, c; • c = (++a >= 3 ? b -- : b ++); • Then a = ? b = ? c = ? • Questions 2: • int a = 2, b = 0, c = 0; • While ( a < 4){ • b += c ++; • a ++; • } • Then a = ? b = ? c = ?
2D pointers • Important Concepts int *p1; • p1 is a pointer • What does p1 point to? • Can be an integer • Can be an integer array and we can index by p1[n]. Here p1[n] is an integer int **p2; • p2 is a pointer • What does p2 point to? • Can be a pointer pointing to an integer • Can be an array of pointers and we can index by p2[n]. Here p2[n] is a pointer! Therefore, p2[n] can point to an integer or an integer array.
10090080 10020008 8 10010000 10020008 10010000 int a = 8; int *pa = &a; int **ppa = &pa; Question: [1] (*pa) = ? [2] (**ppa) = ?
10010000 10020008 10010000 int *pa = new int [3];
10010000 10010308 10014006 10090080 10020008 10010308 10020008 10010000 10014006 int **ppa = new int* [3]; for(int k=0;k<3;k++) ppa[k] = new int [3]; Question: (1) ppa[1] = ? (2) ppa[1][2] = ? (3) ppa + 2 = ?
Example: Table • I want to implement a general table class, which holds any size of row/column. • How can I construct such a table? • What should we do for the constructor and destructor?
class CTable() { int **m_pData; int m_nRow; int m_nCol; public: CTable(int r = 1, int c = 1); ~CTable(); … };
CTable::CTable(int r, int c) { assert(r>0 && c>0); m_nRow = r; m_nCol = c; m_pData = new int *[m_nRow]; assert(m_pData != NULL); for(int k=0; k<m_nRow; k++){ m_pData[k] = new int [m_nCol]; assert(m_pData[k] != NULL); } }
CTable::~CTable() { for(int k=0; k<m_nRow; k++) delete [] m_nData[k]; delete [] m_nData; }
Local variable and scope t copy t1=1 a t2=1 c c’ void main() { int t, t1 = 1, t2 = 1; t = myfunc(t1 + t2); } int myfunc(int a, int& b) { int c; c = a + b; return c; } • Questions: • What are the exact stuff passed to the function? • What happen to the memory when executing into the function? • What are the exact stuff return from the function? • What can NOT be returned? • What if a/b/c/t1/t2/t are not just int but objects? copy
An interesting question int *LocationOfAge() { int age; cin >> age; // missing code here } return &age; int *p = new int; *p = age; return p; return (new int(age));
When constr/desctr are called class M { public: M(); M(const M & ); ~M(); }; M::M() { cout << "call default constructor\n"; } M::M(const M & m) { cout << "call copy constructor\n"; } M::~M() { cout << "call destructor\n"; }
void myfun1(M t) { cout << "go inside myfunc1()\n"; } void myfun2(M &t) { cout << "go inside myfunc2()\n"; } M& myfun4() { cout << “go inside myfunc4()\n”; M *temp = new M; return *temp; } M myfun3() { cout << “go inside myfunc3()\n”; M temp; return temp; }
void myfun1(M t); void myfun2(M &t); M myfun3(); M& myfun4(); void main() { M a; M b = a; M c(a); myfun1(b); myfun2(a); a = myfun3(); b = myfun4(); M *d = new M; delete d; } call default constructor call copy constructor call copy constructor call copy constructor go inside myfunc1() call destructor go inside myfunc2() go inside myfunc3() call default constructor call copy constructor call destructor call destructor go inside myfunc4() call default constructor call default constructor call destructor call destructor call destructor call destructor IMPORTANT! RUN this example by yourself!
Linked list class CNode { public: CNode( const int &d ) : data(d), nextPtr(0){}; int data; CNode *nextPtr; };
Linked list firstPtr 7 9 8 3 5 Question: how do you find 8 in the list? Node* find(Node* firstPtr, int q) { ListNode *curPtr = firstPtr; // step I while (curPtr != NULL) // step II if (curPtr->data == q) // step III break; curPtr = curPtr->nextPtr; // step IV } return curPtr; } Question: how to allocate a node to hold 10? Question: how to insert the node of 10 after 8? (8 could be the last one in the list)