1 / 55

C++ Classes and Member Functions

C++ Classes and Member Functions. circleA.cpp. ... class Circle { public: double x; double y; double radius; }; void read_circle(const int i, Circle & c); void write_circle(const Circle & c);. circleA.cpp. ... int main() { Circle c; vector<Circle> list; int n(0);

york
Download Presentation

C++ Classes and Member Functions

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. C++ Classes and Member Functions The Ohio State University

  2. circleA.cpp ... class Circle { public: double x; double y; double radius; }; void read_circle(const int i, Circle & c); void write_circle(const Circle & c); ... The Ohio State University

  3. circleA.cpp ... int main() { Circle c; vector<Circle> list; int n(0); cout << "Enter number of circles: "; cin >> n; for (int i = 0; i < n; i++) { read_circle(i, c); list.push_back(c); } cout << endl << "Circles:" << endl; for (int i = 0; i < list.size(); i++) { c = list[i]; write_circle(c); } The Ohio State University

  4. circleA.cpp ... void read_circle(const int i, Circle & c) { cout << "Enter circle " << i << " (x, y, radius): "; cin >> c.x; cin >> c.y; cin >> c.radius; } void write_circle(const Circle & c) { cout << "Center: (" << c.x << "," << c.y << ")."; cout << " Radius: " << c.radius << endl; } The Ohio State University

  5. Class Circle class Circle { public: double x; double y; double radius; }; The Ohio State University

  6. Class Circle: Version B class Circle { public: double coord[2]; double radius; }; The Ohio State University

  7. circleB.cpp ... int main() { Circle c; vector<Circle> list; int n(0); cout << "Enter number of circles: "; cin >> n; for (int i = 0; i < n; i++) { read_circle(i, c); list.push_back(c); } cout << endl << "Circles:" << endl; for (int i = 0; i < list.size(); i++) { c = list[i]; write_circle(c); } Note: No change from routine main() in circleA.cpp. The Ohio State University

  8. CircleB.cpp ... void read_circle(const int i, Circle & c) { cout << "Enter circle " << i << " (x, y, radius): "; cin >> c.coord[0]; cin >> c.coord[1]; cin >> c.radius; } void write_circle(const Circle & c) { cout << "Center: (" << c.coord[0] << "," << c.coord[1] << ")."; cout << " Radius: " << c.radius << endl; } The Ohio State University

  9. Class Circle: Version C class Point { public: double x; double y; }; class Circle { public: Point center; double radius; }; The Ohio State University

  10. CircleC.cpp ... int main() { Circle c; vector<Circle> list; int n(0); cout << "Enter number of circles: "; cin >> n; for (int i = 0; i < n; i++) { read_circle(i, c); list.push_back(c); } cout << endl << "Circles:" << endl; for (int i = 0; i < list.size(); i++) { c = list[i]; write_circle(c); } Note: No change from routine main() in circleA.cpp. The Ohio State University

  11. CircleC.cpp ... void read_circle(const int i, Circle & c) { cout << "Enter circle " << i << " (x, y, radius): "; cin >> c.center.x; cin >> c.center.y; cin >> c.radius; } void write_circle(const Circle & c) { cout << "Center: (" << c.center.x << "," << c.center.y << ")."; cout << " Radius: " << c.radius << endl; } The Ohio State University

  12. Information Hiding • Hide the implementation of class Circle. • Provide an interface to set/get the coordinates. The Ohio State University

  13. circleA.cpp class Circle { public: double x; double y; double radius; }; The Ohio State University

  14. circleA2.cpp class Circle { private: double x; double y; public: double radius; // member functions void setCenter(const double cx, const double cy); double getX() const; double getY() const; }; The Ohio State University

  15. Class Circle (A) Member Functions void Circle::setCenter(const double cx, const double cy) { x = cx; y = cy; } double Circle::getX() const { return(x); } double Circle::getY() const { return(y); } The Ohio State University

  16. Member Functions • The function header for a member function f of class C is: <return type> C::f(…) • For example: void Circle::setCenter(const double cx, const double cy) { ... } • The term "Circle::setCenter()" indicates that setCenter is a member function of class Circle. The Ohio State University

  17. Examples of Member Function • cout.precision(5); • cout.setf(ios::fixed); • fin.open(filename, ios::in); • fin.close(); • s.length(); // s is a string • s.substr(0, 2); • v.size(); // v is a vector • v. push_back(3); The Ohio State University

  18. Class Circle (A) Member Functions void Circle::setCenter(const double cx, const double cy) { x = cx; y = cy; } double Circle::getX() const { return(x); } double Circle::getY() const { return(y); } The Ohio State University

  19. circleA2.cpp void read_circle(const int i, Circle & c) { double x, y; cout << "Enter circle " << i << " (x, y, radius): "; cin >> x; cin >> y; c.setCenter(x,y); cin >> c.radius; } void write_circle(const Circle & c) { cout << "Center: (" << c.getX() << "," << c.getY() << ")."; cout << " Radius: " << c.radius << endl; } The Ohio State University

  20. circleA2.cpp ... int main() { Circle c; vector<Circle> list; int n(0); cout << "Enter number of circles: "; cin >> n; for (int i = 0; i < n; i++) { read_circle(i, c); list.push_back(c); } cout << endl << "Circles:" << endl; for (int i = 0; i < list.size(); i++) { c = list[i]; write_circle(c); } Note: No change from routine main() in circleA.cpp. The Ohio State University

  21. circleB2.cpp class Circle { private: double coord[2]; public: double radius; // member functions void setCenter(const double cx, const double cy); double getX() const; double getY() const; }; Note: Same member functions as in circleA2.cpp. The Ohio State University

  22. Class Circle (B) Member Functions void Circle::setCenter(const double cx, const double cy) { coord[0] = cx; coord[1] = cy; } double Circle::getX() const { return(coord[0]); } double Circle::getY() const { return(coord[1]); } The Ohio State University

  23. circleB2.cpp void read_circle(const int i, Circle & c) { double x, y; cout << "Enter circle " << i << " (x, y, radius): "; cin >> x; cin >> y; c.setCenter(x,y); cin >> c.radius; } void write_circle(const Circle & c) { cout << "Center: (" << c.getX() << "," << c.getY() << ")."; cout << " Radius: " << c.radius << endl; } Note: No change from routines in circleA2.cpp. The Ohio State University

  24. circleC2.cpp class Circle { private: Point center; public: double radius; // member functions void setCenter(const double cx, const double cy); double getX() const; double getY() const; }; Note: Same member functions as in circleA2.cpp. The Ohio State University

  25. Class Circle (C) Member Functions void Circle::setCenter(const double cx, const double cy) { center.x = cx; center.y = cy; } double Circle::getX() const { return(center.x); } double Circle::getY() const { return(center.y); } The Ohio State University

  26. circleC2.cpp void read_circle(const int i, Circle & c) { double x, y; cout << "Enter circle " << i << " (x, y, radius): "; cin >> x; cin >> y; c.setCenter(x,y); cin >> c.radius; } void write_circle(const Circle & c) { cout << "Center: (" << c.getX() << "," << c.getY() << ")."; cout << " Radius: " << c.radius << endl; } Note: No change from routines in circleA2.cpp. The Ohio State University

  27. Information Hiding class Circle { private: double x; double y; public: double radius; // member functions void setCenter(const double cx, const double cy); double getX() const; double getY() const; }; • Attributes x and y are private so that no one can write code which relies upon x and y. The Ohio State University

  28. circleA2Error.cpp class Circle { private: double x; double y; public: double radius; ... }; ... for (int i = 0; i < n; i++) { read_circle(i, c); list.push_back(c); // ERROR: Accessing private attributes. cout << "x = " << c.x << " y = " << c.y << endl; } The Ohio State University

  29. ... • class Circle • { • private: • double x; • double y; • … • // ERROR: Accessing private attributes. • cout << "x = " << c.x << " y = " << c.y << endl; • … > g++ circleA2Error.cpp circleA2Error.cpp: In function `int main()': circleA2Error.cpp:11: `double Circle::x' is private circleA2Error.cpp:40: within this context circleA2Error.cpp:12: `double Circle::y' is private circleA2Error.cpp:40: within this context > The Ohio State University

  30. Information Hiding Function main() Other functions C++ Class X Member functions C++ Class X Private attributes The Ohio State University

  31. Information Hiding Class X Member functions Class X Private main() Other functions Class Y Member functions Class X Private Class Z Member functions Class X Private The Ohio State University

  32. const Member Functions class Circle { ... // member functions void setCenter(const double cx, const double cy); double getX() const; double getY() const; }; double Circle::getX() const { return(x); } double Circle::getY() const { return(y); } The Ohio State University

  33. const Member Functions • A const member function does not change any of the class values. • A const member function can be applied to a const variable. void write_circle(const Circle & c) { cout << "Center: (" << c.getX() << "," << c.getY() << ")."; cout << " Radius: " << c.radius << endl; } The Ohio State University

  34. constError.cpp class Circle { private: double x; double y; public: double radius; // member functions void setCenter(const double cx, const double cy); double getX(); // Note: Not declared as const double getY(); // Note: Not declared as const }; The Ohio State University

  35. ... void write_circle(const Circle & c) { cout << "Center: (" << c.getX() << "," << c.getY() << ")."; cout << " Radius: " << c.radius << endl; } … > g++ constError.cpp constError.cpp: In function `void write_circle(const Circle&)': constError.cpp:61: passing `const Circle' as `this' argument of `double Circle::getX()' discards qualifiers constError.cpp:61: passing `const Circle' as `this' argument of `double Circle::getY()' discards qualifiers > The Ohio State University

  36. circleC3.cpp class Circle { private: Point center; public: double radius; // member functions void setCenter(const double cx, const double cy); double getX() const; double getY() const; }; Note: Attribute radius is public. The Ohio State University

  37. circleC3.cpp class Circle { private: Point center; double radius; public: // member functions void setCenter(const double cx, const double cy); void setRadius(const double r); double getX() const; double getY() const; double getRadius() const; }; The Ohio State University

  38. setRadius(), getRadius() void Circle::setRadius(const double r) { if (r < 0.0) { cerr << "Warning. Radius cannot be negative." << endl; cerr << " Setting radius to zero." << endl; radius = 0.0; } else { radius = r; } } double Circle::getRadius() const { return(radius); } The Ohio State University

  39. Double the Circle Sizes • Read in a list of circles; • Double the circles sizes (the size of the radii). The Ohio State University

  40. doubleCircle.cpp class Circle { private: Point center; double radius; public: // member functions void setCenter(const double cx, const double cy); void setRadius(const double r); void Double(); double getX() const; double getY() const; double getRadius() const; }; The Ohio State University

  41. Double() void Circle::Double() { radius = radius*2.0; } The Ohio State University

  42. doubleCircle.cpp ... cout << "Enter number of circles: "; cin >> n; for (int i = 0; i < n; i++) { read_circle(i, c); list.push_back(c); } for (int i = 0; i < list.size(); i++) { list[i].Double(); } cout << endl << "New Circles:" << endl; for (int i = 0; i < list.size(); i++) { c = list[i]; write_circle(c); } The Ohio State University

  43. Object Oriented Programming void Circle::Double() { radius = radius*2.0; } ... for (int i = 0; i < list.size(); i++) { list[i].Double(); } ... • Member function Double() operates on the object list[i]. The Ohio State University

  44. Object Oriented Programming From wikipedia.org: An object-oriented program is a collection of interacting objects, as opposed to the conventional model, in which a program is seen as a list of tasks (subroutines) to perform. In objected-oriented programming, each object is capable of receiving messages, processing data, and sending messages to other objects. Each object can be viewed as an independent "machine" with a distinct role or responsibility. The actions on these objects are closely associated with the object. The Ohio State University

  45. scaleCircle.cpp class Circle { private: Point center; double radius; public: // member functions void setCenter(const double cx, const double cy); void setRadius(const double r); void Double(); void Scale(const double scale); ... }; The Ohio State University

  46. Scale() void Circle::Scale(const double scale) { if (scale <= 0.0) { cerr << "Scale factor must be positive." << endl; return; } radius = radius*scale; } void Circle::Double() { Scale(2.0); // Note: Use Scale() to double circle. } The Ohio State University

  47. scaleCircle.cpp ... for (int i = 0; i < n; i++) { read_circle(i, c); list.push_back(c); } cout << "Enter scale factor: "; cin >> s; for (int i = 0; i < list.size(); i++) { list[i].Scale(s); } cout << endl << "New Circles:" << endl; for (int i = 0; i < list.size(); i++) { c = list[i]; write_circle(c); } The Ohio State University

  48. lineSegment2.cpp class Point { public: double x; double y; }; class LineSegment { public: Point endpoint[2]; // Declare endpoints as an array }; The Ohio State University

  49. lineSegment2.cpp ... double length1 = compute_distance(segA.endpoint[0], segA.endpoint[1]); double length2 = compute_distance(segB.endpoint[0], segB.endpoint[1]); if (length1 > length2) { cout << "First line segment is longer." << endl; } else if (length2 > length1) { cout << "Second line segment is longer." << endl; } else { cout << "Line segments have equal length." << endl; } return 0; } ... The Ohio State University

  50. lineSegment3.cpp class LineSegment { private: Point endpoint[2]; public: void setEndpoint(const int iend, const Point & p); double getEndpointX(const int iend) const; double getEndpointY(const int iend) const; double getLength() const; }; The Ohio State University

More Related