Lec4 - Classes

Lec4 - Classes - Lecture 4: Classes PIC 10B Todd Wittman...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Lecture 4: Classes PIC 10B Todd Wittman Pop Quiz, Hotshot b Ex What does this print? What's the runtime? for (int i=1; i <= N; i++) { for (int j=1; j <= i; j++) cout << j << " "; cout << "\n"; } b Using formula we saw last class, takes O(N 2 ) time. b Ex What's the runtime of this code? for (int i=1; i<=N & i<=100; i++) cout << i << " "; b If N<100, runs for N (linear) iterations. If N>100, runs for 100 (constant) iterations. Since Big O is for large values of N, the runtime is O(1).
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Sec 6.1 : Classes b Recall that a class is a suped-up data type equipped with special member functions . b We call an instance of a class an object . b The string class is an example of a class. string my_string = “vader”; class name object name value of object b The insert function is an example of a member function. my_string.insert(3,”yoda”); //Use period after object name. b This week we’re going to gradually build up two example classes. s The Product Class (from Ch. 6 of your book) s The IntMatrix Class (for HW2) Sec 6.2 : Declaring Classes b Declaring a class is a lot like declaring a function. class Class_name { public: constructor declarations member function declarations private: data fields }; b Public fields are accessible outside the class. b Private fields are only accessible to the class itself, much like local variables. b Member function types: constructor, accessor, mutators. b
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 7

Lec4 - Classes - Lecture 4: Classes PIC 10B Todd Wittman...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online