Summary: The following source codes are used in this chapter. An abstract data type (ADT) is a data type (a set of values and a collection of operations on those values) that is accessed only through an interface. This style of programming, which is called object-oriented programming, is directly supported by the C++ class construct. ADTs provide the flexibility to make it convenient to implement the fundamental data structures and algorithms.
// Data Structure & Algorithm C++, Otgonbayar Tovuudorj
// Chapter 3. C++ and Data Abstraction
// Point class - ADT
#include <iostream>
using namespace std;
class Point {
public:
Point() { x = y = 0; }
Point(int vx, int vy): x(vx), y(vy) {};
~Point() {}; // not required for Point class
void move(int dx, int dy);
void reset(int nx, int ny);
friend ostream & operator<<(ostream & out, const Point &p);
private:
int x, y;
};
void Point::move(int dx, int dy) {
x += dx;
y += dy;
}
ostream &operator<<(ostream &out, const Point &p)
{
out << "x=" << p.x << endl;
out << "y=" << p.y << endl;
return out;
}
int main()
{
Point p1, p2(3,7);
cout << "Point p1:\n" << p1;
cout << "Point p2:\n" << p2;
cout <<"After moved each point by 2 units (both x and y)" << endl;
p1.move(2,2);
p2.move(2,2);
cout << "Point p1:\n" << p1;
cout << "Point p2:\n" << p2;
return 0;
}
