cse202: lecture 19the ohio state university1 c++ classes
TRANSCRIPT
CSE202: Lecture 19 The Ohio State University 1
C++ Classes
CSE202: Lecture 19 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
CSE202: Lecture 19 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); } ...
CSE202: Lecture 19 The Ohio State University 4
Class Circle
• Create an “object” (C++ class) representing a circle:
class Circle
{
public:
double x;
double y;
double radius;
};
CSE202: Lecture 19 The Ohio State University 5
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.
CSE202: Lecture 19 The Ohio State University 6
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.
CSE202: Lecture 19 The Ohio State University 7
Class Attributesclass 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
CSE202: Lecture 19 The Ohio State University 8
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;
CSE202: Lecture 19 The Ohio State University 9
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;
CSE202: Lecture 19 The Ohio State University 10
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);...
CSE202: Lecture 19 The Ohio State University 11
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; }...
CSE202: Lecture 19 The Ohio State University 12
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);...
CSE202: Lecture 19 The Ohio State University 13
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);...
CSE202: Lecture 19 The Ohio State University 14
circleIntersect.cpp...class Circle{public: double x; double y; double radius;};
// protoypesvoid read_circle(const char * prompt, Circle & c);bool intersects(const Circle & c1, const Circle & c2);
int main(){ Circle c1, c2;...
CSE202: Lecture 19 The Ohio State University 15
circleIntersect.cpp...// protoypesvoid 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; };...
CSE202: Lecture 19 The Ohio State University 16
read_circle()
void read_circle(const char * prompt, Circle & c){ cout << prompt; cin >> c.x; cin >> c.y; cin >> c.radius;}
CSE202: Lecture 19 The Ohio State University 17
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); }}
CSE202: Lecture 19 The Ohio State University 18
What’s the error?...// protoypesvoid 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;...
C++ Classes
• Class definition should always be followed by a semicolon:
class Circle{
...
}; // <-Note: semicolon
CSE202: Lecture 19 The Ohio State University 19
CSE202: Lecture 19 The Ohio State University 20
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.
CSE202: Lecture 19 The Ohio State University 21
Class Circle
class Circle{public: double x; // x-coordinate of circle center double y; // y-coordinate of circle center double radius; // radius of circle};
CSE202: Lecture 19 The Ohio State University 22
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};
CSE202: Lecture 19 The Ohio State University 23
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;...
CSE202: Lecture 19 The Ohio State University 24
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;
CSE202: Lecture 19 The Ohio State University 25
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;}
CSE202: Lecture 19 The Ohio State University 26
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);...
CSE202: Lecture 19 The Ohio State University 27
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;}...
CSE202: Lecture 19 The Ohio State University 28
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;}
CSE202: Lecture 19 The Ohio State University 29
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);}
CSE202: Lecture 19 The Ohio State University 30
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);}
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.
CSE202: Lecture 19 The Ohio State University 31
CSE202: Lecture 19 The Ohio State University 32
Class LineSegment
class Point{public: double x; double y;};
class LineSegment{public: Point endpoint1; Point endpoint2;};
CSE202: Lecture 19 The Ohio State University 33
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);...
CSE202: Lecture 19 The Ohio State University 34
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;}...
CSE202: Lecture 19 The Ohio State University 35
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);}
CSE202: Lecture 19 The Ohio State University 36
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);}
CSE202: Lecture 19 The Ohio State University 37
lineSegment2.cpp
class Point{public: double x; double y;};
class LineSegment{public: Point endpoint[2]; // Declare endpoints as an array};
CSE202: Lecture 19 The Ohio State University 38
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;}...
CSE202: Lecture 19 The Ohio State University 39
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]);}
Class Person
class Person {
public:
int age;
double height;
double weight;
bool gender;
string first_name;
string last_name;
};
CSE202: Lecture 19 The Ohio State University 40