Lec5 - Lecture 5 Constructors Destructors PIC 10B Todd...

Info icon This preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Lecture 5: Constructors & Destructors PIC 10B Todd Wittman The Product Class ring2 Last class we defined the Product class which stores an item's name, price, and score. class Product { public: Product(); void read(); bool is_better_than(Product b) const; void print() const; private: string name; double price; int score; }; ring2 Today we'll add some more functions to this class.
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 18.3 : The Default Constructor ring2 Last class we created a default constructor for the Product class. Product::Product ( ) { price = 1; score = 0; name = “No item”; } ring2 Recall the constructor is just the name of the class and has no return value. ring2 This function is called to set up a uninitialized Product with dummy values. Product my_product; ring2 Then we have to use read( ) to set the values. ring2 A better constructor would read in values when we create it. Constructor With Parameters ring2 In C++ we can overload a function, so that we can have multiple versions of the same function. ring2 The compiler will look up the function with corresponding parameters. ring2 So in addition to our default constructor Product( ), we could have the following constructor. Product :: Product (string new_name, double new_price, int new_score) { name = new_name; price = new_price; score = new_score; } ring2 Note the parameters are NOT named name, price, score. ring2 We can then create a product with the call: Product my_product ("Millenium Falcon", 29.99, 10);
Image of page 2
3 Constructor With Parameters ring2 There is an alternative syntax for the constructor with parameters (p. 227). ClassName :: ClassName (parameters) : field1(expression1), field2(expression2), ... { **STATEMENTS** } ring2 It sets the value of the private variable field1 to whatever is inside the parentheses, usually a parameter.
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