1 / 39

C++ Classes

C++ Classes. List of circles. Represent a list of circles: vector<double> xlist; // x-coordinates of centers vector<double> ylist; // y-coordinates of centers vector<double> rlist; // circle radii. inputCircles.cpp . ... int main() {

hien
Download Presentation

C++ Classes

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 The Ohio State University

  2. List of circles • Represent a list of circles: vector<double> xlist; // x-coordinates of centers vector<double> ylist; // y-coordinates of centers vector<double> rlist; // circle radii The Ohio State University

  3. inputCircles.cpp ... int main() { vector<double> xlist; // x-coordinates of circle centers vector<double> ylist; // y-coordinates of circle centers vector<double> rlist; // circle radii double x, y, radius; int n(0); cout << "Enter number of circles: "; cin >> n; for (int i = 0; i < n; i++) { cout << "Enter circle " << i << " (x, y, radius): "; cin >> x; xlist.push_back(x); cin >> y; ylist.push_back(y); cin >> radius; rlist.push_back(radius); } ... The Ohio State University

  4. Class Circle • Create an “object” (C++ class) representing a circle: class Circle { public: double x; double y; double radius; }; The Ohio State University

  5. Class Attributes • C++ class representing a circle: class Circle { public: double x; double y; double radius; }; • x, y, radius are called the “class attributes” or “data members” of class Circle The Ohio State University

  6. Class Attributes class Circle { public: double x; double y; double radius; }; • To declare a Circle object: Circle c; • To access the Circle attributes: c.x c.y c.radius The Ohio State University

  7. Example class Circle { public: double x; double y; double radius; }; ... Circle c; cin >> c.x; cin >> c.y; cin >> c.radius; cout << "Radius = " << c.radius << endl; The Ohio State University

  8. List of circles • C++ class representing a circle: class Circle { public: double x; double y; double radius; }; • Declare a vector of class Circle: vector<Circle> list; The Ohio State University

  9. circleClass.cpp ... class Circle { public: double x; // x-coordinate of circle center double y; // y-coordinate of circle center double radius; // radius of circle }; int main() { Circle c; vector<Circle> list; int n(0); ... The Ohio State University

  10. circleClass.cpp ... cout << "Enter number of circles: "; cin >> n; for (int i = 0; i < n; i++) { cout << "Enter circle " << i << " (x, y, radius): "; cin >> c.x; cin >> c.y; cin >> c.radius; list.push_back(c); } cout << endl << "Circles:" << endl; for (int i = 0; i < list.size(); i++) { c = list[i]; cout << "Center: (" << c.x << "," << c.y << ")."; cout << " Radius: " << c.radius << endl; } ... The Ohio State University

  11. circleClass2.cpp ... class Circle { public: double x; double y; int radius; // Note: radius is type int, not double }; int main() { Circle c; vector<Circle> list; int n(0); ... The Ohio State University

  12. What’s the error? ... class Circle { public: double x; // x-coordinate of circle center double y; // y-coordinate of circle center double radius; // radius of circle } int main() { Circle c; vector<Circle> list; int n(0); ... The Ohio State University

  13. circleIntersect.cpp ... class Circle { public: double x; double y; double radius; }; // protoypes void read_circle(const char * prompt, Circle & c); bool intersects(const Circle & c1, const Circle & c2); int main() { Circle c1, c2; ... The Ohio State University

  14. circleIntersect.cpp ... // protoypes void read_circle(const char * prompt, Circle & c); bool intersects(const Circle & c1, const Circle & c2); int main() { Circle c1, c2; read_circle("Enter first circle (x,y,radius): ", c1); read_circle("Enter second circle (x,y,radius): ", c2); if (intersects(c1, c2)) { cout << "Circles intersect." << endl; } else { cout << "Circles do not intersect." << endl; }; ... The Ohio State University

  15. read_circle() void read_circle(const char * prompt, Circle & c) { cout << prompt; cin >> c.x; cin >> c.y; cin >> c.radius; } The Ohio State University

  16. intersects() bool intersects(const Circle & c1, const Circle & c2) { double diffx = c1.x - c2.x; double diffy = c1.y - c2.y; double dist = sqrt(diffx*diffx + diffy*diffy); if (dist <= c1.radius + c2.radius) { return(true); } else { return(false); } } The Ohio State University

  17. What’s the error? ... // protoypes void read_circle(const char * prompt, Circle & c); bool intersects(const Circle & c1, const Circle & c2); class Circle { public: double x; double y; double radius; }; int main() { Circle c1, c2; ... The Ohio State University

  18. C++ Classes • Class definition should always be followed by a semicolon: class Circle{ ... }; // <-Note: semicolon The Ohio State University

  19. C++ Classes • C++ classes should usually be passed by reference. (Why?) • Use const if the C++ class is not changed inside the function • Declare C++ classes before function prototypes The Ohio State University

  20. Class Circle class Circle { public: double x; // x-coordinate of circle center double y; // y-coordinate of circle center double radius; // radius of circle }; The Ohio State University

  21. New Class Circle class Point { public: double x; // x-coordinate double y; // y-coordinate }; class Circle { public: Point center; // circle center double radius; // circle radius }; The Ohio State University

  22. circleContains.cpp ... int main() { Circle c; Point p; cout << "Enter point (x,y): "; cin >> p.x; cin >> p.y; cout << "Enter circle (x,y,radius): "; cin >> c.center.x; cin >> c.center.y; cin >> c.radius; ... The Ohio State University

  23. Classes Containing Classes • Access the attributes x and y of class center as: c.center.x c.center.y • For instance: cout << "Enter circle (x,y,radius): "; cin >> c.center.x; cin >> c.center.y; cin >> c.radius; The Ohio State University

  24. circleContains.cpp ... double diffx = p.x - c.center.x; double diffy = p.y - c.center.y; double dist = sqrt(diffx*diffx + diffy*diffy); if (dist <= c.radius) { cout << "Point is inside circle." << endl; } else { cout << "Point is not inside circle." << endl; } return 0; } The Ohio State University

  25. circleContains2.cpp ... class Point { public: double x; double y; }; class Circle { public: Point center; double radius; }; void read_point(const char * prompt, Point & p); void read_circle(const char * prompt, Circle & c); double compute_distance(const Point & p1, const Point & p2); ... The Ohio State University

  26. circleContains2.cpp ... int main() { Circle c; Point p; read_point("Enter point (x,y): ", p); read_circle("Enter circle (x,y,radius): ", c); double dist = compute_distance(p, c.center); if (dist <= c.radius) { cout << "Point is inside circle." << endl;} else { cout << "Point is not inside circle." << endl; } return 0; } ... The Ohio State University

  27. read_circle() and read_point() void read_circle(const char * prompt, Circle & c) { cout << prompt; cin >> c.center.x; cin >> c.center.y; cin >> c.radius; } void read_point(const char * prompt, Point & p) { cout << prompt; cin >> p.x; cin >> p.y; } The Ohio State University

  28. compute_distance() double compute_distance(const Point & p1, const Point & p2) { double diffx = p1.x - p2.x; double diffy = p1.y - p2.y; double dist = sqrt(diffx*diffx + diffy*diffy); return(dist); } The Ohio State University

  29. compute_distance() • Note: Function compute_distance() has no idea that p2 is the center of a circle ... int main() { ... double dist = compute_distance(p, c.center); ... } double compute_distance(const Point & p1, const Point & p2) { double diffx = p1.x - p2.x; double diffy = p1.y - p2.y; double dist = sqrt(diffx*diffx + diffy*diffy); return(dist); } The Ohio State University

  30. Comparing Line Segments • Write a program to read in two line segments and report which one is longer • Each line segment has two endpoints • Each endpoint has two coordinates The Ohio State University

  31. Class LineSegment class Point { public: double x; double y; }; class LineSegment { public: Point endpoint1; Point endpoint2; }; The Ohio State University

  32. lineSegment.cpp ... void read_point(const char * prompt, Point & p); void read_line_segment (const char * header, LineSegment & seg); double compute_distance (const Point & p1, const Point & p2); int main() { LineSegment segA; LineSegment segB; read_line_segment("Enter first line segment: ", segA); read_line_segment("Enter second line segment: ", segB); ... The Ohio State University

  33. lineSegment.cpp ... double length1 = compute_distance(segA.endpoint1, segA.endpoint2); double length2 = compute_distance(segB.endpoint1, segB.endpoint2); 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

  34. read_point() and read_line_segment() void read_point(const char * prompt, Point & p) { cout << prompt; cin >> p.x; cin >> p.y; } void read_line_segment (const char * heading, LineSegment & seg) { cout << heading << endl; read_point ("Enter first endpoint (x,y): ", seg.endpoint1); read_point ("Enter second endpoint (x,y): ", seg.endpoint2); } The Ohio State University

  35. compute_distance() • Note: Function compute_distance()has no idea that p2 and p2 are line segment endpoints ... double length1 = compute_distance(segA.endpoint1, segA.endpoint2); double length2 = compute_distance(segB.endpoint1, segB.endpoint2); ... double compute_distance(const Point & p1, const Point & p2) { double diffx = p1.x - p2.x; double diffy = p1.y - p2.y; double dist = sqrt(diffx*diffx + diffy*diffy); return(dist); } The Ohio State University

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

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

  38. lineSegment2.cpp void read_point(const char * prompt, Point & p) { cout << prompt; cin >> p.x; cin >> p.y; } void read_line_segment (const char * heading, LineSegment & seg) { cout << heading << endl; read_point ("Enter first endpoint (x,y): ", seg.endpoint[0]); read_point ("Enter second endpoint (x,y): ", seg.endpoint[1]); } The Ohio State University

  39. Class Person class Person { public: int age; double height; double weight; bool gender; string first_name; string last_name; }; The Ohio State University

More Related