October 1997
Intermediate to advanced
800 pages
20h 48m
English
Our classes up to now have shown you the relative merits of private data members, private member functions, and public member functions. What about classes with public data members? Unlike private state variables (which are always unavailable to user programs), user programs can access an object's public data members. To demonstrate, let's look at a Point class and a program that creates Point objects.
// point1.C - public data members #include <iostream.h> class Point { public: double x, y; Point(double x1 = 0, double y1 = 0) { x = x1; y = y1; } }; int main() { Point p(20, 20); Point q; // initialize q to (0,0) p.x++; // go right p.x--; // go left p.y++; // go up p.y--; ... |