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

Info icon This 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 box4 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"; } box4 Using formula we saw last class, takes O(N 2 ) time. box4 Ex What's the runtime of this code? for (int i=1; i<=N && i<=100; i++) cout << i << " "; box4 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).
Image of page 1

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

View Full Document Right Arrow Icon
2 Sec 6.1 : Classes box4 Recall that a class is a suped-up data type equipped with special member functions . box4 We call an instance of a class an object . box4 The string class is an example of a class. string my_string = “vader”; class name object name value of object box4 The insert function is an example of a member function. my_string.insert(3,”yoda”); //Use period after object name. box4 This week we’re going to gradually build up two example classes. square6 The Product Class (from Ch. 6 of your book) square6 The IntMatrix Class (for HW2) Sec 6.2 : Declaring Classes box4 Declaring a class is a lot like declaring a function. class Class_name { public: constructor declarations member function declarations private: data fields }; box4 Public fields are accessible outside the class. box4 Private fields are only accessible to the class itself, much like local variables. box4 Member function types: constructor, accessor, mutators. box4 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.
Image of page 2
3 The Product Class box4 Suppose we’re cataloging and comparing different products, e.g. Star Wars toys. box4 It would be helpful to store the various items for sale in some standard class. box4 What data should the product store? square6 Product name (string) square6 Product price (double) square6 Product score on 0-10 scale (int) box4 What functions would we like to have?
Image of page 3

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern