130 likes | 263 Views
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);
E N D
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; intgetY() { 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 (intu, int v) : x(u), y(v) { } intgetX () { return x; } intgetY () { return y; } void setX (int newX ) const { x = newX ; } }; int main () { Point p(5, 3); p. setX (9001) ; cout << p. getX () << ’ ’ << p. getY (); return 0; } BUGS: 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) { } intgetX() { return x; } void setX(intnewX); }; void setX (intnewX) { x = newX; } intmain() { 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(intnewX) { x = newX; }
Catch the Bug 4 ... intsize; cin>> size; int*nums = new int[size]; for(inti = 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 (intu, int v) : x(u), y(v) { } intgetX() { return x; } intgetY() { return y; } }; int main () { Point *p = new Point (5, 3); cout << pgetX() << ’ ’ << pgetY(); return 0; } 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. delete p;
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; } intgetArea() { return (width * height); } }; intmain(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; } intgetArea() { return (width * height); } }; intmain(void) { Rectangle* Rect = new Rect; Rect.setWidth(5); Rect.setHeight(7); cout << "Total area: " << Rect.getArea() << endl; delete Rect; } Solution: RectsetWidth(5); RectsetHeight(7); cout<< "Total area: " << RectgetArea() << endl;
Catch the Bug 9 class Shape { protected: int width; int height; public: void setWidth(int w) { width = w; } void setHeight(int h) { height = h; } }; class Rectangle: Shape { public: intgetArea() { 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 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 <typenameT> 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 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<typenameT> T Calculator<T>::subtract(T a, T b) { return a - b; }
Programming Exercise 1 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; } intmain() { 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; }