550 likes | 648 Views
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);
E N D
C++ Classes and Member Functions The Ohio State University
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
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
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
Class Circle class Circle { public: double x; double y; double radius; }; The Ohio State University
Class Circle: Version B class Circle { public: double coord[2]; double radius; }; The Ohio State University
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
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
Class Circle: Version C class Point { public: double x; double y; }; class Circle { public: Point center; double radius; }; The Ohio State University
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
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
Information Hiding • Hide the implementation of class Circle • Provide an interface to set/get the coordinates. The Ohio State University
circleA.cpp class Circle { public: double x; double y; double radius; }; The Ohio State University
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
... • 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
Information Hiding Function main() Other functions C++ Class X Member functions C++ Class X Private attributes The Ohio State University
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
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
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
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
... 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
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
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
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
Double the Circle Sizes • Read in a list of circles • Double the circles sizes (the size of the radii) The Ohio State University
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
Double() void Circle::Double() { radius = radius*2.0; } The Ohio State University
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
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
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
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
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
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
lineSegment2.cpp class Point { public: double x; double y; }; class LineSegment { public: Point endpoint[2]; // Declare endpoints as an array }; The Ohio State University
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
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