Lecture 4 Classes PIC 10B Todd Wittman Pop Quiz Hotshot 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 Using formula we saw last class takes O N2 time Ex What s the runtime of this code for int i 1 i N i 100 i cout i 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 1 Sec 5 1 Classes Recall that a class is a suped up data type equipped with special member functions We call an instance of a class an object The string class is an example of a class string my string vader class name object name value of object The insert function is an example of a member function my string insert 3 yoda Use period after object name This week we re going to gradually build up two example classes The Product Class from Ch 6 of your book The IntMatrix Class for HW2 Sec 5 2 Declaring Classes Declaring a class is a lot like declaring a function class Class name Functions that create the object public constructor declarations Class functions member function declarations private Internal variables and functions data fields Note the weird semi colon at the end Only for class declarations Public fields are accessible outside the class Private fields are only accessible to the class itself much like local variables Member function types constructor accessor mutators Add the const mondifier to accessors 2 The Product Class Suppose we re cataloging and comparing different products e g Star Wars toys It would be helpful to store the various items for sale in some standard class What data should the product store What functions would we like to have Product name string Product price double Product score on 0 10 scale int Create new product constructor Read in a new product s details mutator Compare two products accessor Print out a product s details accessor 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 Place in your program above main or in own file 3 Sec 5 4 Member Functions We ve declared the class but we haven t defined any of its functions yet The general form of a member function definition is return type ClassName function name parameters const STATEMENTS Optional Add if it s an accessor We add the const modifier at the end if it s an accessor Should match the consts used in the declaration By adding ClassName before the function name we make sure that the program knows this is a member function for that class Product s print function Every member function has the private variables ready to go name price score Print the values of a product void Product print const cout Name name n cout Price price n cout Score score n n return We call this function from main or wherever with something like Product my product my product print 4 Product s read function Read in the name price and score of a product void Product read cout Please enter the model name getline cin name cout Please enter the price cin price cout Please enter the score cin score string remainder getline cin remainder Note we do not declare the variables name price and score They re already declared by the class declaration What does the variable remainder do Why do we use getline once and cin twice Product s is better than function To find the bang for the buck ratio compute score price Return true if the current product has better ratio than a passed product b Be sure to avoid division by zero bool Product is better than Product b const if b price 0 return false if price 0 return true return score price b score b price We can access the private variable of another product Explict Object s Price b price Implict Object s Price price To call this function for Products a b use something like if a is better than b cout Buy a Note how we access the different variables To get the implicit price just say price To get price of b use b price Recall that non member functions can t get to price directly 5 Sec 5 5 The Default Constructor Last class we created a default constructor for the Product class Product Product price 1 score 0 name No item Recall the constructor is just the name of the class and has no return value This function is called to set up a uninitialized Product with dummy values Product my product Then we have to use read to set the values A better constructor would read in values when we create it Sec 5 6 Constructor With Parameters In C we can overload a function so that we can have multiple versions of the same function The compiler will look up the function with corresponding parameters 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 Note the parameters are NOT named name price score We can then create a product with the call Product my product Millenium Falcon 29 99 10 6 Constructor With Parameters There is an alternative syntax for the constructor with parameters p 248 ClassName ClassName parameters field1 expression1 field2 expression2 STATEMENTS It sets the value of the private variable field1 to whatever is inside the parentheses usually a parameter 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 You don t have to use this syntax Personally I don t care for it But it s necessary if you don t have a default constructor Sec 15 3 The Copy Constructor A common programming style is to copy one object into another through the parameters ClassName newObject oldObject Even if you don t like this constructor it s useful because the compiler automatically looks for this constructor for assignment ClassName newObject oldObject It s also implicitly called when you need a temporary object like in a calculation cout object1 object2 The copy constructor just needs to copy the data from the passed object ClassName ClassName const ClassName right COPYING STATMENTS Note we pass the parameter by reference Why We also add the word const to the parameter Why 7 The Copy Constructor The copy constructor for Product is Product Product const Product right name right name price right price score right score Now both of the following statements work Product new product old product Product product1 product2 The
View Full Document