Chapter3 - Chapter 3 Class Basics Part 1 User Perspective...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter 3 – Class Basics Part 1 – User Perspective While putting our code and our friend’s into separate files has certainly improved matters, we still haven’t solved all of the problems we identified. In particular we need some way of ensuring that only the functions that implement the Date ADT can access the components of a Date structure (no “back door” access). As things stand, nothing stops a program from directly manipulating structure components (e.g. if we assume day, month, and year components, we can write “ = 5”). It is also desirable to more closely tie together the structure definition and the functions (to make the connection explicit). These ends can be achieved by 1/. Putting the function prototypes inside the structure definition (making the functions into “member functions”). This makes the connection between the structure and the related functions absolutely clear. 2/. Declaring that only member functions can access structure components . This prevents “back door” access by code outside the ADT. When we do this, the structure becomes a “class”. Actually we could continue to use the term “struct” (in C++ “struct” and “class” are essentially synonyms), but in practice we reserve the term “struct” for simple cases (no member functions, no access control). The revised header file is given below. Note, although it may look quite different, the basic idea remains. We’re still defining a structure (having day, month, and year components), and still have a series of function prototypes. class Date {// “class” and “struct” are essentially equivalent private: // only member functions may access the following… int day, month, year; // details unimportant (could be different) public: // the following are to be generally accessible… // reads date from the specified input stream void read (istream &is); // moves date forward by the specified number of days void move (int days); // outputs date to the specified output stream void write (ostream &os) const; // const means that object is not modified }; Note that our functions have changed a bit. For a start, the names have changed. “readDate” has become just “read”, and so on. The “Date” is redundant because the functions are defined within the “Date” class, and it is therefore implicit that “read” reads a date. Being able to use simpler names is one advantage of classes. The first parameter of each function (the Date to be operated on) has also disappeared. It is now implicit. Because the functions are defined within the Date class, it is understood that each of them operates on a “Date”. We are now at the conceptual watershed between
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
“procedural” and “object oriented” programming. In the procedural point of view, the function is the centre of attention. We call a function, and, almost as an afterthought, pass information to it. In the object oriented point of view, we start with an “object” (a
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/24/2010 for the course SYSC 2002 taught by Professor Unknown during the Fall '07 term at Carleton.

Page1 / 16

Chapter3 - Chapter 3 Class Basics Part 1 User Perspective...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online