/* ** $Id: structs.cc 824 2008-03-09 19:23:52Z phf $ ** ** Just an example of how structs and classes are related; ** also note that C++ structs are different from C structs! */ #include struct Point { // default public visibility int x; int y; private: // can make things private though int w; }; class Doint { // default private visibility int x; int y; public: // can make things public though int w; }; int main() { // keyword struct in front optional, unlike C! Point p; // assign to fields, works fine p.x = 10; p.y = 20; // this won't work, field is private! // p.w = 30; // allocating a struct dynamically Point *pp = new Point(); // printing stuff std::cout << pp->x << std::endl; // class in front optional, nobody does that anyway Doint d; // this won't work, fields are private! // d.x = 10; d.y = 20; // assign to public field, works fine d.w = 30; // allocating a class (well, we call it object now) dynamically Doint *dp = new Doint(); // printing stuff std::cout << dp->w << std::endl; // freeing the dynamic stuff... delete pp; delete dp; }