[ Pobierz całość w formacie PDF ]
.// Fig// Fig.9.8: point2.h// Definition of class Point#ifndef POINT2_H#define POINT2_Hclass Point {friend ostream &operator<<( ostream &, const Point & );public:Point( int = 0, int = 0 ); // default constructorvoid setPoint( int, int ); // set coordinatesint getX() const { return x; } // get x coordinateint getY() const { return y; } // get y coordinateprotected: // accessible to derived classesint x, y; // coordinates of the point};#endif// Fig.9.8: point2.cpp// Member functions for class Point#include <iostream.h>#include "point2.h"// Constructor for class PointPoint::Point( int a, int b ) { setPoint( a, b ); }// Set the x and y coordinatesvoid Point::setPoint( int a, int b ){x = a;y = b;}// Output the Pointostream &operator<<( ostream &output, const Point &p ){output << '[' << p.x << ", " << p.y << ']';return output; // enables cascading}// Fig.9.8: fig09_08.cpp// Driver for class Point#include <iostream.h>#include "point2.h"int main(){Point p( 72, 115 ); // instantiate Point object p// protected data of Point inaccessible to maincout << "X coordinate is " << p.getX()<< "\nY coordinate is " << p.getY();p.setPoint( 10, 10 );cout << "\n\nThe new location of p is " << p << endl;return 0;}X coordinate is 72Y coordinate is 115The new location of p is [10, 10]
[ Pobierz całość w formacie PDF ]