C++_classes - CSE 332: C++ classes rom Procedural to...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CSE 332: C++ classes rom Procedural to Object-oriented Programming Procedural programming Functions have been the main focus so far Parameters, return values, call stack Mostly viewed data, functions as being separate Object-oriented programming Allows us to package data and functions together Makes data more interesting (adds behavior) Makes functions more focused (restricts data scope) Next 3 lectures will focus on OO programing Using classes/structs, member functions/variables New ideas like inheritance, polymorphism, substitution CSE 332: C++ classes Structure of a Simple C++ Class class Date { public: // 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 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: // visible only to class member functions int d_, m_, y_; }; The compiler defines these 4 if you dont CSE 332: C++ classes Class Members and Memory Locations class Date { public: Date (); Date (const Date &); Date (int d, int m, int y); virtual ~Date (); operator= (const Date &); int d () const; int m () const; int y () const; void d (int); void m (int); void y (int); string yyyymmdd () const; private: int d_, m_, y_; }; class members live in whichever memory segment an object (a class instance ) was created Date d int d.d_ logical memory location int d.m_ int d.y_ Actual memory locations CSE 332: C++ classes Access Control Declaring access control scopes within a class public : visible outside the class protected : visible to derived classes (more later) private : visible only within the class Access control in a class is private by default but, better style to label access control scopes 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 Versus classes, that have both data and behavior 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 others internal details But other classes shouldnt have such direct access 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?...
View Full Document

This note was uploaded on 04/07/2008 for the course CSE 332S taught by Professor Smart during the Spring '08 term at Washington University in St. Louis.

Page1 / 20

C++_classes - CSE 332: C++ classes rom Procedural to...

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

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