C++_classes - From Procedural to Object-oriented...

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

View Full Document Right Arrow Icon
CSE 332: C++ classes From Procedural to Object-oriented Programming Procedural programming Functions have been the main focus so far Function parameters and return values Exceptions and how the call stack behaves We have viewed data and functions as being separate for the most part Object-oriented (a.k.a. OO) programming Allows us to package data and functions together Makes data more interesting (adds behavior) Makes functions more focused (restricts data scope) This module will focus on OO programing Using classes/structs, member functions/variables New ideas like inheritance, polymorphism, substitution
Background image of page 1

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

View Full DocumentRight Arrow Icon
CSE 332: C++ classes Structure of a Simple C++ Class class Date { public: // member functions, visible outside the class Date (); // default constructor Date (const Date &); // copy constructor Date (int d, int m, int y); // another constructor virtual ~Date (); // (virtual) destructor Date & operator= (const Date &); // assignment operator int d () const; int m () const; int y () const; // accessors void d (int); void m (int); void y (int); // mutators string yyyymmdd () const; // generate a formatted string private: // member variables, visible only within functions above int d_; int m_; int y_; }; The compiler defines these 4 if you don’t Notice the semicolon at the end of the class declaration operators can be member functions as well
Background image of page 2
CSE 332: C++ classes Access Control Declaring access control scopes within a class private : visible only within the class protected : also visible within derived classes (more later) public : visible everywhere Access control in a class is private by default but, it’s better style to label access control explicitly A struct is the same as a class , except Access control for a struct is public by default Usually used for things that are “mostly data” E.g., if initialization and deep copy only, may suggest using a struct Versus classes, which are expected to have both data and some form of non-trivial behavior E.g., if reference counting, etc. probably want to use a class
Background image of page 3

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

View Full DocumentRight Arrow Icon
CSE 332: C++ classes Issues with Encapsulation in C++ Sometimes two classes are closely tied For example, a container and its iterators One needs direct access to the other’s internal details But other classes shouldn’t have such direct access Can put their declarations in the same header file Can put their definitions in the same source file Poses interesting design forces How should iterator access members of container? Making container members public violates encapsulation Any class, not just iterator could modify them Make protected, derive iterator from container? Could work:
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 19

C++_classes - From Procedural to Object-oriented...

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

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