Presentation is loading. Please wait.

Presentation is loading. Please wait.

Wrap Up and Misc Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.

Similar presentations


Presentation on theme: "Wrap Up and Misc Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series."— Presentation transcript:

1 Wrap Up and Misc Ying Wu Electrical & Computer Engineering Northwestern University yingwu@ece.northwestern.edu ECE230 Lectures Series

2 What We’ve learned (I) Fundamentals of C++ Class –Default arguments –Constructor/destructor –When constructors and destructor are called

3 What We’ve learned (II) Advanced C++ concepts –Dynamic Memory Allocation –Classes with pointer data members –Copy constructor –Operator overloading

4 Local variable and scope 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? void main() { int t, t1 = 1, t2 = 1; t = myfunc(t1, t2); } copy t t1=1 t2=1 a c c’ copy

5 What can not be returned? class M; M myfun1() { M temp; return temp; } M* myfun2() { M temp; return &temp; } M& myfun3() { M temp; return temp; } double myfun1() { double temp; return temp; } double* myfun2() { double temp; return &temp; } double& myfun3() { double temp; return temp; }

6 What would be the right sol.? class M; M myfun1() { M temp; return temp; } M* myfun2() { M *temp = new M; return temp; } M& myfun3() { M *temp = new M; return *temp; }

7 Different Constructors class CArray { public: CArray(int = 5);// default constructor CArray(const CArray &);// copy constructor ~CArray(); int getSize() const { return m_nSize; }; const CArray& operator=( const CArray &); // assignment int& operator[](int); private: int m_nSize;// size of the array int *m_pData;// pointer to the array };

8 void main() { CArray a0; CArray aaa(5);// what does it mean? CArray a1 = 10;// what does it mean? for(int i=0;i<10;i++) a1[i] = i; CArray a2 = a1;// what does it mean? CArray a3; a3 = a2;// what does it mean? }

9 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"; }

10 void myfun1(M t) { cout << "go inside myfunc1()\n"; } M& myfun4() { cout << “go inside myfunc4()\n”; M *temp = new M; return *temp; } void myfun2(M &t) { cout << "go inside myfunc2()\n"; } M myfun3() { cout << “go inside myfunc3()\n”; M temp; return temp; }

11 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 go inside myfunc1() call destructor go inside myfunc2() go inside myfunc3() call default constructor call copy constructor call destructor go inside myfunc4() call default constructor call destructor IMPORTANT! RUN this example by yourself!


Download ppt "Wrap Up and Misc Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series."

Similar presentations


Ads by Google