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

Info iconThis preview shows pages 1–5. 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 5.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 5.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 Create new product (constructor). s Read in a new product’s details (mutator). s Compare two products (accessor). s Print out a product’s details (accessor). b Which of our functions should get the const modifier? The Product Class Declaration class Product { public: Product(); void read(); bool is_better_than(Product b) const; void print() const; private: string name; double price; int score; }; b Place in your program above main() or in own file.
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 Sec 5.4 : Member Functions
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/27/2010 for the course PIC 157-051-20 taught by Professor Wittman during the Winter '10 term at UCLA.

Page1 / 10

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

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

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