Lec5 - Constructors & Destructors

Lec5 - Constructors & Destructors - Lecture 5:...

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

View Full Document Right Arrow Icon
1 Lecture 5: PIC 10B Todd Wittman The Product Class r 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; }; r Today we'll add some more functions to this class.
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 18.3 : The Default Constructor r Last class we created a default constructor for the Product class. Product::Product ( ) { price = 1; score = 0; name = “No item”; } r Recall the constructor is just the name of the class and has no return value. r This function is called to set up a uninitialized Product with dummy values. Product my_product; r Then we have to use read( ) to set the values. r A better constructor would read in values when we create it. Constructor With Parameters r In C++ we can overload a function, so that we can have multiple versions of the same function. r The compiler will look up the function with corresponding parameters. r 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; } r Note the parameters are NOT named name, price, score. r We can then create a product with the call: Product my_product ("Millenium Falcon", 29.99, 10);
Background image of page 2
3 Constructor With Parameters r There is an alternative syntax for the constructor with parameters (p. 227). ClassName :: ClassName (parameters) : field1(expression1), field2(expression2), . .. { **STATEMENTS** } r It sets the value of the private variable field1 to whatever is inside the parentheses, usually a parameter. r So we could rewrite our constructor as: Product :: Product (string new_name, double new_price, int new_score) : name(new_name), price(new_price), score(new_score) { } r You don't have to use this syntax. Personally I don't care for it. r But it's necessary if you don't have a default constructor. The Copy Constructor r A common programming style is to copy one object into another through the parameters: ClassName newObject (oldObject); r Even if you don't like this constructor, it's useful because the compiler automatically looks for this constructor for assignment (=). ClassName newObject = oldObject;
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 11/19/2011 for the course PIC 10B taught by Professor Wittman during the Winter '08 term at UCLA.

Page1 / 11

Lec5 - Constructors & Destructors - Lecture 5:...

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