cs 117 spring 2002 more classes. ieee student conference student professional awareness conference...
Post on 19-Dec-2015
220 Views
Preview:
TRANSCRIPT
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• posters in MEC and ET buildings
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 6dieValue = 1 + rand() % 6;
• randomize.h
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
Money Class
• Two attributes– dollars– cents
• Behavior– decimal value– addition– comparison– input/output
Money.h
class Money {public:
Money() {dollars = cents =0;}Money( double);double decValue() const;
private:int dollars, cents;
// overloaded operators
};
Money Class
• Allocate a Money object– money unitPrice( 5000.00);
• Assignments– taxAmount = salePrice * taxPercent/100.0;– itemPrice = unitPrice * quantity;– finalCost = itemPrice + taxAmount;
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
Objects as Operands
• Assignment operator has a default definitionob2 = ob1;
• Others must be "overloaded"– >> for input– << for output– arithmetic if appropriate– comparison
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
Approaches
• Write member functions for each operator needed– read(), print(), equals(), lessThan(), ...
• Overload the appropriate operators– consistency with primitive variables
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
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());
}
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
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& );
Overloading Operators
• lets you use objects the way you do primitive types
• can be either member or friend functions
• prototypes are relatively fixed
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
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());
}
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
top related