1 / 13

Exercises on Basic OOP

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);

Download Presentation

Exercises on Basic OOP

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Exercises on Basic OOP TCP1201: 2013/2014

  2. 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.

  3. 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,

  4. 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; }

  5. 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.

  6. 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 << pgetX() << ’ ’ << pgetY(); 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;

  7. 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; }

  8. 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;

  9. 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: RectsetWidth(5); RectsetHeight(7); cout<< "Total area: " << RectgetArea() << endl;

  10. 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

  11. 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; }

  12. 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; }

  13. 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; }

More Related