CS114-009 Class 19 Today Practice with classes Announcements Turn in algorithm for Project 5 in...
-
Upload
irene-carpenter -
Category
Documents
-
view
215 -
download
0
Transcript of CS114-009 Class 19 Today Practice with classes Announcements Turn in algorithm for Project 5 in...
CS114-009 Class 19 Today
Practice with classes Announcements
Turn in algorithm for Project 5 in class todayProject 5 due 11/11 by midnight – e-mail to
[email protected] Make sure you have read Section 10.2
pp. 540-572
Review – creating a class Our class
LARGE INTEGERS Normal integer can only
hold up to 2 billion Want to be able to hold
a number with up to 100 digits in it
Declarationclass INT {
// Attributes
// items needed to
// represent the data
public:
// Methods
// constructors
// basic arithmetic (+, –)
// input and output
};
Sample Class (for large ints) Data representation
100 digits Sign (positive or negative)
Constructors Two constructors Default
INT a, b, c[10]; Construct from an integer
INT a(-37), b=89, c[2]={12,34}; Other operations
Addition Read and Write
Last four methods are better Use actual + and – operators Use actual << and >> operators
Friends (for I/O) Overload << and >> for INT
Will focus on those in CS 124
class INT {int digits[100];char sign;
public:INT( );INT(int num);void Add(INT); // x.Add(y);void Read( ); // x.Read();void Write( ); // x.Write();
INT& operator + (const INT& a);INT& operator - (const INT& a);friend ostream& operator<<
(ostream&, const INT&);friend istream& operator>>
(istream&, INT&);};
class INT {
int digits[100];
char sign;
public:
INT( );
void Read( );
void Write( );
void Add( INT );
};
Try to get this version workingint main( ) {
INT a, b;
a.Read( );
b.Read( );
a.Write( ); cout << endl;
b.Write( ); cout << endl;
a.Add(b);
a.Write( ); cout << endl;
return 0;
}
“Dot” notation We’ve seen this, but haven’t discussed. Classes we’ve worked with: cin, cout, string
cin.eof() // End of File? string num; num.length(); // length of a string num.at(i); // character in num cout.setf(ios::fixed) // set the output format
The ‘dot’ is a way of calling a function for that object.s1.length(); s2.length(); s3.length();
// different objects, different lengths
Code for Constructor
INT::INT( ) {
for (int a=0;a<100;a++)
digits[a] = 0;
sign = 'p';
}
System automatically calls constructor whenever a variable of this type is declared
Constructor initializes all digits to zero and the sign to ‘p’ ‘n’ represents negative
Code for Read & Writevoid INT::Write() {
int a = 0;if (sign == 'n') cout << '-';while (a < 100)
cout << digits[a++];}
void INT::Read() {string num;cin >> num;if (num.at(0) == '-') sign = 'n';if (num.at(0) == '+') sign = 'p';int s = 0;if ( !isdigit( num.at(0) ) ) s = 1;int loc = 100 – (num.length() - s);for (int a=s; a<num.length(); a++) digits[loc++] = num.at(a) – '0';
}
Write simply prints out a leading sign (if the number is negative) followed by 100 digits
Read inputs the variable into a string (temporarily) and then extracts from the string into the array of digits
Code for Add method
void INT::Add(INT num) {if (num.sign == 'n' || sign == 'n') cout << “not done" << endl;else { int a, carry = 0, total; for (a=99; a>=0; a--) {
total = digits[a] + num.digits[a] +
carry;if (total > 9) carry = 1;else carry = 0;digits[a] = total % 10;
}}
}
Syntax INT a, b; a.Add(b);
Only adds numbers if both are positive
Ignores overflow (result greater than 100 digits)
Class Exercise The class we have started is on our web
page: Big_INT.cpp
Download this program, then compile and run
Modify Write( ) so that it does not print leading zeros in the number
Modify Add( ) so that it is also capable of adding two negative numbers
Modify Read( ) so that it erases the previous contents of the object
Addition to Class Exercise:Modify Add( ) so that it is also capable of adding
two negative numbers Consider how “Add()” works. It’s called this way:
INT a, b; // read values…a.Add(b); // adds the value of b to a
void INT::Add(INT num)
if (sign == ‘n’ || num.sign == ‘n’) … What’s the difference between “sign” and
“num.sign”? We’re adding two objects.
One is the parameter “num”.The other one is “this” object.
Call: a.Add(b) adds ‘b’ to ‘a’.
a: 42 b: 13
void INT::Add(INT num)
if (sign == ‘n’ || num.sign == ‘n’) …
Which one is “sign”?
Which one is “num.sign”?
a: 42 b: 13
a.Add(b);
Every Class needs… Functions that give you the (private) values Functions that let you change the values We call them:
Accessors (or “getters”) Usually called “getValue()” (for each “Value” attribute)
Mutators (of “setters”) Usually called “setValue(x)”
Other language:Class data members are called “attributes”Class functions are called “methods”
The Car Class Download the file cars.cpp from our class web page What is the constructor of the Car class?
What does this constructor do, in terms of setting attributes?
Identify the accessors Better known as “getter” methods
Identify the mutators Better known as “setter” methods
Identify the other methods. What do they do? Could the functions outside of the Cars class be rewritten as
methods for the Cars class?
Class Exercise Write the definitions to the following methods, whose
prototypes you’ll find on cars.cpp setCarInfo() – set a car’s information, given the make,
model, type, doors, quantity of the car in stock, cylinder engine, and liters of the engine
load() – reads in information from the file cars.txt, and uses setCarInfo() to set the information for an instance of car Data for each car is on one line. The order of the data:
Make, Model, Type, Doors, Liters, Cylinders, Quantity
Class exercise Download the file box.cpp from our class web
page Write the definitions to the following methods,
whose prototypes you’ll find in box.cpp volume() – given the height, width, and length of a
box, return its volume compare() – compares the volume of a box to another
box; returns true if the volumes of the 2 boxes are equivalent, otherwise returns false.
Insert lines of code into main( ) as indicated to utilize the appropriate member function. (see comments in box.cpp)
End of Class 19