1 / 9

struct v.s. class

struct v.s. class. struct point { double x; double y; }; void main() { point p; double distance; p.x = 3.0; p.y = 4.0; distance = sqrt( p.x * p.x + p.y * p.y ); ....... }. class point { public: point(double x, double y); double xcoord();

Télécharger la présentation

struct v.s. class

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. structv.s.class struct point { double x; double y; }; void main() { point p; double distance; p.x = 3.0; p.y = 4.0; distance = sqrt(p.x*p.x+ p.y*p.y); ....... } class point { public: point(double x, double y); double xcoord(); double ycoord(); void setx(double x); void sety(double y); double from_org(); double disst(class point p); private: double myX; double myY; }; ............. void main() { point p1(3,4); double distance=p1.from_org(); ...... } p1.myX=3.0; Error!!!

  2. Homework int main() { point p1(0,4); point p2(4,0); cout << p1.from_org() << endl; cout << p2.from_org() << endl; cout << p2.disst(p1) << endl; } Refer to the previous slide, change class point to struct point and have a program to do the same thing as above.

  3. morestruct struct student { string last_name; string first_name; Ctime DOB; double GPA; int credits; char gender; int class; }; void main() { Student a,b; ............ a.class = b.class; a.credits += 9; a.last_name = "....."; ............ } A collection of information in different data types. Heterogeneous aggregate A user can (has to) update the values of the variables defined insider the struct(ure).

  4. structasclass struct Point { Point(); Point(double px, double py); double distanceFrom(const Point& p) const; double x; double y; }; Point::Point(double px, double py) : x(px), y(py) {} Point::Point() : x(0), y(0) {} double Point::distanceFrom(const Point& p) const { return sqrt((x - p.x)*(x - p.x) + (y - p.y)*(y - p.y)); }

  5. structis notclass struct Point { Point(); Point(double px, double py); double distanceFrom(const Point& p) const; double x; double y; }; void main() { Point P1(0,4); Point P2; cout << P1.distanceFrom(P2) << endl; P2.x=4; P2.y=0; cout << P1.distanceFrom(P2) << endl; }

  6. Copy constructor; overloaded operator void main() { point p1(0,4); // class point p2(4,0); point p3; p3 = p2; if (p3 == p2) cout << "p2 == p3"; Point P1(0,4); // struct Point P2; Point P3; P3 = P2; if (P2 == P3) cout << "P2 == P3"; }

  7. Easy way to output Points ..... void main() { .......... Point P1(0,4),P2; ........ cout << "(" << P1.x << "," << p1.y << ")"; ......... if ((P1.x == P2.x) && (P1.y == P2.y)) ... ......... } Can we simply use cout << P1 and have (0,4) printed? Can we simply use P1==P2 ?

  8. Overload the standard operators include <sstream> struct Point { ..... string tostring() const; ..... }; string Point::tostring() const { ostringstream out; out << "(" << x << ", " << y << ")"; return out.str(); } ostream & operator << (ostream& os, const Point& p) { os << p.tostring(); return os; } bool operator == (const Point& p1, const Point& p2) { return p1.x == p2.x && p1.y == p2.y; }

  9. What's wrong? class point { public: point(double x, double y); double xcoord(); double ycoord(); void setx(double x); void sety(double y); double from_org(); double disst(class point p); private: double myX; double myY; }; bool operator == (point& p1, point& p2) { return p1.x == p2.x && p1.y == p2.y; } bool operator == (point& p1, point& p2) { return p1.myX == p2.myX && p1.myY == p2.myY; } bool operator == (point& p1, point& p2) { return p1.xcoord() == p2.xcoord() && p1.ycoord() == p2.ycoord(); }

More Related