Exercises2

Report
Exercises on Basic OOP
TCP1201: 2013/2014
Catch the Bug 1
class Point
{
private :
int x, y;
public :
Point(int u, int v) : x(u), y(v) { }
int getX() { return x;
int getY() { return y; }
};
int main()
{
Point p(5, 3);
cout << p.x << " " << p.y << "\n";
return 0;
}
BUGS:
x and y are private members and
cannot be accessed outside of the
class.
Catch the Bug 2
class Point {
private :
int x, y;
public :
Point (int u, int v) : x(u), y(v) { }
int
getX () { return x; }
int
getY () { return y; }
void
setX (int newX )
const { x = newX ; }
};
int main ()
{
BUGS:
Point p(5, 3);
p. setX (9001) ;
cout << p. getX () << ’ ’ << p. getY ();
return 0;
}
The function setX is declared const,
Catch the Bug 3
class
Point
{
private :
int x, y;
public :
Point (int u, int v) : x(u), y(v) { }
int getX () { return x; }
void setX(int newX);
};
void setX (int newX) {
x = newX; }
int main() {
Point p(5, 3);
p.setX(0);
cout << p.getX() << " " << "\n";
}
BUGS:
setX is missing the scope; the function should
be declared as:
void Point::setX(int newX) { x = newX; }
Catch the Bug 4
...
int size;
cin >> size;
int *nums = new int[size];
for(int i = 0; i < size; ++i)
{
cin >> nums[i];
}
... // Calculations with nums omitted
delete nums;
...
BUGS:
Deleting a dynamically allocated array requires
delete[ ], not delete.
Catch the Bug 5
class Point
{
private :
int x, y;
public :
Point (int u, int v) : x(u), y(v) { }
int getX () { return x;
}
int getY () { return y;
}
};
BUGS:
p is allocated using new, but is never
deallocated with delete. Every piece
of memory allocated with new must be
deallocated somewhere with a
corresponding delete.
int main ()
{
Point *p = new Point (5, 3);
cout << pgetX () << ’ ’ << p getY ();
delete p;
return 0;
}
14
Catch the Bug 6
int add(int x, int y) {
return x + y;
}
int main(){
int a = 1, b = 2;
cout << add(a,b) << endl;
double d = 1.8, e = 1.3;
cout << add(d, e) << endl;
}
Solution:
Function add does not adds two
double numbers. Use a template
function to allow the function add
to accept any data type.
template <typename T>
T add(T x,T y) {
return x + y;
}
Catch the Bug 7
class Rectangle
{
protected:
int width;
int height;
public:
void setWidth(int w) { width = w;
}
void setHeight(int h) { height = h;
}
int getArea()
{ return (width * height); }
};
int main(void)
{
Rectangle Rect;
Rect.setWidth(5);
Rect.setHeight(7);
Rect.getArea();
}
Solution:
Rect.getArea() returns an integer only. It does not
print out the value. To print out the value, you should
use cout as follows:
cout << "Total area: " << Rect.getArea() << endl;
Catch the Bug 8
class Rectangle
{
protected:
int width;
int height;
public:
void setWidth(int w) { width = w;
}
void setHeight(int h) { height = h;
}
int getArea()
{ return (width * height); }
Solution:
};
int main(void)
RectsetWidth(5);
{
Rectangle* Rect = new Rect; Rect  setHeight(7);
cout << "Total area: " << Rect  getArea() << endl;
Rect.setWidth(5);
Rect.setHeight(7);
cout << "Total area: " << Rect.getArea() << endl;
delete Rect;
}
Catch the Bug 9
class Shape {
protected:
int width;
int height;
public:
void setWidth(int w) {
void setHeight(int h) {
};
width = w;
height = h;
}
}
class Rectangle: Shape {
public:
int getArea() { return (width * height); }
};
int main(void) {
Rectangle Rect;
Rect.setWidth(5);
Rect.setHeight(7);
Rect.getArea();
}
Solution:
Shape is not recognized. The definition of
Rectangle class should be:
class Rectangle: public Shape
Programming Exercise 1 (Class Template)
The following program show the usage of function template to compute the
addition of two numbers. Extend the program below to use class template to
develop a simplified calculator with the functions of adding, substracting,
multiplying and dividing two numbers.
template <typename T>
T add(T x, T y) {
return x + y;
}
int main(){
int a = 1, b = 2;
cout << add(a,b) << endl;
double d = 1.8, e = 1.3;
cout << add(d, e) << endl;
}
Programming Exercise 1 (Class Template)
Solution:
template<typename T>
class Calculator {
public:
T add(T a, T b);
T subtract(T a, T b);
T multiply(T a, T b);
double divide(T a, T b);
};
template<typenameT>
T Calculator<T>::add(T a,T b) {
return a + b;
}
template<typename T>
T Calculator<T>::subtract(T a,T b) {
return a - b;
}
Programming Exercise 1 (Class Template)
template<typename T>
T Calculator<T>::multiply(T a,T b) {
return a * b;
}
template<typenameT>
double Calculator<T>::divide(T a,T b) {
return (double) a / b;
}
int main() {
Calculator<int> iCalc;
cout << iCalc.add(1, 2) << endl;
cout << iCalc.subtract(1, 2) << endl;
cout << iCalc.multiply(1, 2) << endl;
cout << iCalc.divide(1, 2) << endl;
Calculator<float> fCalc;
cout << fCalc.add(1.7, 2.2) << endl;
cout << fCalc.subtract(1.7, 2.2) << endl;
cout << fCalc.multiply(1.7, 2.2) << endl;
cout << fCalc.divide(1.7, 2.2) << endl;
}
Programming Exercise 2 (OOP Basics)
Define a class named GroceryItem. Include private fields that hold an
item’s stock number, price, quantity in stock, and total value. Write a public
function named dataEntry that calls four private functions. Three of the
private functions prompt the user for keyboard input for a value for one of
the data fields stock number, price, and quantity in stock. The function that
sets the stock number requires the user to enter a value between 1000 and
9999 inclusive; continue to prompt the user until a valid stock number is
entered. The functions that set the price and quantity in stock require nonnegative values; continue to prompt the user until valid values are entered.
Include a fourth private function that calculates the GroceryItem’s total
value field (price times quantity in stock).
Write a public function that displays a GroceryItem’s values. Write a
driver program that declares an array of 10 GroceryItem objects.
Assign values to all 10 items and display them.
Programming Exercise 3 (OOP Basics)
Define a class named CoffeeOrder. Declare a private static
field that holds the price of a cup of coffee as $1.25. Include
private integer fields that you set to a flag value of 1 or 0 to
indicate whether the order should have any of the following:
cream, milk, sugar, or artificial sweetener. Include a public
function that takes a user’s order from the key board and sets
the values of the four fields in response to four prompts. If the
user indicates both milk and cream, turn off the milk flag to
allow only cream. If the user indicates both sugar and artificial
sweetener, turn off the artificial sweetener flag, allowing only
sugar. Include another function that displays the user’s
completed order. Write a driver program to test the functions
of the class.
Programming Exercise 3a (OOP Basics)
Define a class named CoffeeOrder. Declare a private static
field that holds the price of a cup of coffee as $1.25. Include
private integer fields that you set to a flag value of 1 or 0 to
indicate whether the order should have any of the following:
cream, milk, sugar, or artificial sweetener. Include a public
function that takes a user’s order from the key board and sets
the values of the four fields in response to four prompts. If the
user indicates both milk and cream, turn off the milk flag to
allow only cream. If the user indicates both sugar and artificial
sweetener, turn off the artificial sweetener flag, allowing only
sugar. Include another function that displays the user’s
completed order. Write a driver program to test the functions
of the class.
Programming Exercise 3b (OOP Basics)
Using the CoffeeOrder class, write a driver program that
continues to ask a user for an order in a loop until the user
indicates the order is complete or 10 orders have been placed,
whichever comes first. After the user indicates that ordering is
complete, display a recap of all the coffee orders, including the
cream, milk, sugar, and sweetener status of each, as well as a
count of the number of coffees ordered and the total price.
Programming Exercise 5 (Inheritance)
Create a base class named Book. Data fields include title and
author; functions include those that can set and display the
fields. Derive two classes from the Book class: Fiction,
which also contains a numeric grade reading level, and
NonFiction, which contains a variable to hold the number of
pages. The functions that set and display data field values for the
subclasses should call the appropriate parent class functions to
set and display the common fields, and include specific code
pertaining to the new subclass fields.
Write a driver program that demonstrates the use of the
classes and their functions.
Programming Exercise 6 (Inheritance)
Implement a base class Account and derived classes
Savings and Checking. In the base class, supply member
functions deposit and withdraw. Provide a function
daily_interest that computes and adds the daily interest.
For calculations, assume that every month has 30 days.
Checking accounts yield interest of 3 percent monthly on
balances over $1000. Savings accounts yield interest of 6
percent on the entire balance.
Write a driver program that makes a month’s worth of
deposits and withdrawals and calculates the interest every day.

similar documents