Download presentation
Presentation is loading. Please wait.
1
CS 117 Spring 2002 More Classes
2
IEEE Student Conference Student Professional Awareness Conference –Paul Kostek: career options for 21st Century –Panel discussion by local companies April 4, 2002 Lookout room in SUB email to Ben Millemon ieee@boisestate.edu ieee@boisestate.edu posters in MEC and ET buildings
3
4/1/02 exam 3 on April 10 –files –arrays –strings –classes
4
Random numbers use the rand function from stdlib.h –int rand() returns an integer from 0 to RAND_MAX (a big number) –the sequence you get is always the same use % to get numbers from 1 to 6 dieValue = 1 + rand() % 6; randomize.h
5
User Defined Types Define what data we want in the class –what data is needed to represent the class Create the functionality to operate on the data –class member functions provide access to data and operations that can be done on or with it
6
Money Class Two attributes –dollars –cents Behavior –decimal value –addition –comparison –input/output
7
Money.h class Money { public: Money() {dollars = cents =0;} Money( double); double decValue() const; private: int dollars, cents; // overloaded operators };
8
Money Class Allocate a Money object –money unitPrice( 5000.00); Assignments –taxAmount = salePrice * taxPercent/100.0; –itemPrice = unitPrice * quantity; –finalCost = itemPrice + taxAmount;
9
Objects and Functions class types can be used as both parameters and return types for functions –use C ob1 to pass by value a copy is made (copy constructor) not efficient for big objects –Use C& ob1 as formal reference argument –Use const C& ob1 to specify object can’t be modified by a function
10
Objects as Operands Assignment operator has a default definition ob2 = ob1; Others must be "overloaded" –>> for input –<< for output –arithmetic if appropriate –comparison
11
Object Input and Output Often need to be able to –read information to initialize an object –print out information about an object Reading/Printing the information from your program or another class is contrary to the goal of encapsulation Define functions for input and output in the class
12
Approaches Write member functions for each operator needed –read(), print(), equals(), lessThan(),... Overload the appropriate operators –consistency with primitive variables
13
Class functions two approaches a.lessThan (b) –A and b are objects that are compared by passing one object to the other objects member function lessThan lessThan2 (a, b) –Both objects are treated the same –Must use a friend function to do this
14
Two ways to write less than bool Money ::lessThan (const Money & m) const { return (decValue()<m.decValue()); } bool lessThan2 (const Money& m1, const Money& m2) { return (m1.decValue() < m2. decValue()); }
15
Friends Second version of lessThan needs access to the private data of the class –but it isn't a member function so Make it a friend function –declared in the class declaration –allows access to private data –example: getline for strings classes can be friends too
16
Friend Function Declaration Form:(in class declaration) –friend result-type function-name (arg list); –friend result-type operator opSymbol (arg list); Example: –friend bool equals (const Money&, const Money& ); –friend bool operator == (const Money&, const Money& );
17
Overloading Operators lets you use objects the way you do primitive types can be either member or friend functions prototypes are relatively fixed
18
string class string s1, s2 = " a string", s3; can use << to print: cout << s2; can use >> to read: cin >> s1; can use + to concatenate: s1 + s2 can use = to assign: s3 = "stuff" can use ==, <, etc. to compare
19
operator< bool Money ::operator<(const Money & m) const { return (decValue()<m.decValue()); } bool operator< (const Money& m1, const Money& m2) { return (m1.decValue() < m2.decValue()); }
20
Operators for Money Class want input and output –do it once so you don't have to write it again every time you use the class addition makes sense for Money objects multiply by either a double or an int
21
Common Programming Errors Function argument & return value errors Not defining a function as a member of a class Prefixing a class function call –obj.function(); Referencing a private attribute of a class Missing header files Missing ; on class definition
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.