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

Info iconThis preview shows pages 1–4. 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 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 Add the const mondifier to accessors. Functions that create the object. Class functions. Internal variables and functions. Note the weird semi-colon at the end. Only for class declarations.
Background image of page 2
3 The Product Class b Suppose we’re cataloging and comparing different products, e.g. Star Wars toys. b It would be helpful to store the various items for sale in some standard class. b What data should the product store? s Product name (string) s Product price (double) s Product score on 0-10 scale (int) b What functions would we like to have? s
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/05/2008 for the course PIC 40Bsdg taught by Professor Sgd during the Spring '08 term at UCLA.

Page1 / 10

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

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

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