400 likes | 521 Views
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() {
E N D
C++ Classes The Ohio State University
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
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
Class Circle • Create an “object” (C++ class) representing a circle: class Circle { public: double x; double y; double radius; }; The Ohio State University
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
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
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
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
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
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
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
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
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
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
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
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
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
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
C++ Classes • Class definition should always be followed by a semicolon: class Circle{ ... }; // <-Note: semicolon The Ohio State University
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
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
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
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
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
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
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
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
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
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
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
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
Class LineSegment class Point { public: double x; double y; }; class LineSegment { public: Point endpoint1; Point endpoint2; }; The Ohio State University
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
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
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
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
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
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
Class Person class Person { public: int age; double height; double weight; bool gender; string first_name; string last_name; }; The Ohio State University