Class Implementation

Encapsulation

Classes are full-fledged data types like int, float and double. As we've seen, you can create variables of a class type (an object). You can also pass the them to functions, and a function can return a value of a class type. You can use a class just like any other data type.

A class models a single entity -- one Person, one Car, one Book, one Library.

Classes are used to implement user-defined ADTs. These are classes whose external behavior is known to the class user, but whose internal implemenation is hidden.

But a data type is more than just data..... it also has a set of associated operations. Built-in types like int, float, char have opearations such as add, subtract and multiply. Since the details of these operations are hidden, we can consider the built-in data types to be "Abstract Data Types" (ADTs).

To accomplish this goal, we need a mechanism to hide the details of the class implementation from the class user.

Recall the initial DayOfYear class definition from the text.

class DayOfYear { public: void Output( ); int m_month; int m_day; }; As shown in the example code, the class user is able to manipulate the data members of the class. Nothing is hidden from the class user.

Now consider this new improved (but not yet perfect) definition of the DayOfYear class. This definition is adapted from Display 6.4 of the text.

class DayOfYear { public: void Input( ); void Output( ); void Set( int newMonth, int newDay ); void Set( int newMonth ); int GetMonthNumber( ); int GetDay( ); private: int m_month; int m_day; }; Note the keywords public and private.

No item that follows the keyword private can be directly referenced by name in any code except the implementations of the class member functions. Attempting to do so results in a compiler error. The compiler enforces the hiding of class details through the use of the keyword private.

Items which follow the keyword public may be referenced in any code, just like members of a struct. Good programming practice, and a REQUIREMENT in this course is that all data members of a class must be declared private.

C++ implements encapsulation by allowing data members to be declared as private. A private datat member will only be accessible to functions that are members of that class and to functions and classes explicitly granted access permission by the class ("friends" -- more on this later).

Member Functions

Because the data members are now private, public member functions are writtten to provide indirect access to the data for the class user.

Member functions that allow the class user to retreive the value of a private data member are known as accessor -- GetDay( ), GetMonthNumber().

Member functions that allow the class user to change the value of a private data member are known as mutators -- both Set( ) functions.

Other member functions generally provide support for the ADTs operations and are often referred to as services -- Input( ), Output( ) As we'll see next time, other special methods are also provided.

Member Function Guidelines

  1. Like any well written function, each class' member function should perform just one task and do it well. Private functions may be written and called from the public function to allow proper modularity.
  2. No class member function should perform input from or output to the application user unless that is the function's explicit purpose.
    For example, a class that models a Person would likely have an accessor named GetName( ). This function allows the class user access to the data member which contains the Person's name. It DOES NOT prompt the application user to input the Person's name.
  3. Although member functions have direct access to private data members, your code should use accessors and mutators whenever available. Doing so further limits the scope of code that needs changed in the future.
  4. Proper passing of parameters is vital for efficiency of your program. Pass objects by reference whenever possible (almost always).
  5. Also see the guidelines for the use of const.

Example code

This is display 6.4 (page 244) from the text, modified for CMSC 202 coding standards. What changes, if any, would you recommend for this code? <style> body, html { background-color: #eee; } .wrap { position: absolute; perspective: 800px; perspective-origin: 50% 100px; top: 50%; left: 50%; transform: translate(-50%, -50%); } .cube { position: relative; transform-style: preserve-3d; width: 200px; margin: 0 auto; } .cube div { position: absolute; width: 200px; height: 200px; color: #000; background-color: #fff; text-align: center; line-height: 200px; font-size: 3em; box-shadow: inset 0 0 100px #999; } .shadow { position: relative; width: 200px; transform-style: preserve-3d; margin: 0 auto; } .shadow div { position: absolute; width: 200px; height: 200px; box-shadow: 0 0 200px #000; transform: rotateX(90deg) translateY(100px); transform-origin: bottom center; } .back { transform: translateZ(-100px) rotateY(180deg); } .right { transform: rotateY(-270deg) translateX(100px); transform-origin: top right; } .left { transform: rotateY(270deg) translateX(-100px); transform-origin: center left; } .top { transform: rotateX(-90deg) translateY(-100px); transform-origin: top center; } .bottom { transform: rotateX(90deg) translateY(100px); transform-origin: bottom center; } .front { transform: translateZ(100px); } @keyframes spin { from { transform: rotateY(0); } to { transform: rotateY(-360deg); } } .cube, .shadow { animation: spin 10s infinite linear; } </style> <div class="container"> <div class="text-center"> <h4>Sorry! We could not find what you were looking for</h4> <h4>Try going back to the main page <a href="https://www.csee.umbc.edu">here</a></h4> </div> </div> <div class="wrap"> <div class="shadow"> <div>&nbsp;</div> </div> </div> <div class="wrap"> <div class="cube"> <div class="front">4</div> <div class="back">4</div> <div class="top">0</div> <div class="bottom">0</div> <div class="left">0</div> <div class="right">0</div> </div> </div> <!-- <div class="wpgdprc-consent-bar wpgdprc-consent-bar--position-bottom" style="display: none;"> <div class="wpgdprc-consent-bar__inner"> <div class="wpgdprc-consent-bar__container"> <div class="wpgdprc-consent-bar__content"> <div class="wpgdprc-consent-bar__column wpgdprc-consent-bar__column--notice"> <div class="wpgdprc-consent-bar__notice"><p>This site uses functional cookies and external scripts to improve your experience.</p> </div> </div> <div class="wpgdprc-consent-bar__column wpgdprc-consent-bar__column--settings"> <button type="button" class="wpgdprc-button wpgdprc-button--settings" data-micromodal-trigger="wpgdprc-consent-modal" aria-expanded="false" aria-haspopup="true" > Settings and Information </button> </div> <div class="wpgdprc-consent-bar__column wpgdprc-consent-bar__column--accept"> <button type="button" class="wpgdprc-button wpgdprc-button--accept"> Accept </button> </div> </div> </div> </div> </div> <div class="wpgdprc wpgdprc-consent-modal" id="wpgdprc-consent-modal" aria-hidden="true"> <div class="wpgdprc-consent-modal__overlay" tabindex="-1" data-micromodal-close> <div class="wpgdprc-consent-modal__inner" role="dialog" aria-modal="true"> <div class="wpgdprc-consent-modal__header"> <p class="wpgdprc-consent-modal__title">Privacy settings</p> <button class="wpgdprc-consent-modal__close" aria-label="Close popup" data-micromodal-close> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512"><path d="M193.94 256L296.5 153.44l21.15-21.15c3.12-3.12 3.12-8.19 0-11.31l-22.63-22.63c-3.12-3.12-8.19-3.12-11.31 0L160 222.06 36.29 98.34c-3.12-3.12-8.19-3.12-11.31 0L2.34 120.97c-3.12 3.12-3.12 8.19 0 11.31L126.06 256 2.34 379.71c-3.12 3.12-3.12 8.19 0 11.31l22.63 22.63c3.12 3.12 8.19 3.12 11.31 0L160 289.94 262.56 392.5l21.15 21.15c3.12 3.12 8.19 3.12 11.31 0l22.63-22.63c3.12-3.12 3.12-8.19 0-11.31L193.94 256z"/></svg> </button> </div> <div class="wpgdprc-consent-modal__body"> <nav class="wpgdprc-consent-modal__navigation"> <ul class="wpgdprc-consent-modal__navigation-list"> <li class="wpgdprc-consent-modal__navigation-item"> <button class="wpgdprc-consent-modal__navigation-button wpgdprc-consent-modal__navigation-button--active" data-target="description">Privacy Settings and Information</button> </li> <li> <button class="wpgdprc-consent-modal__navigation-button" data-target="1">Google Analytics</button> </li> </ul> </nav> <div class="wpgdprc-consent-modal__information"> <div class="wpgdprc-consent-modal__description wpgdprc-consent-modal__description--active" data-target="description"> <p class="wpgdprc-consent-modal__title wpgdprc-consent-modal__title--description">Privacy Settings and Information</p> <div class="wpgdprc-content-modal__content"> <p>This site uses functional cookies and external scripts to improve your experience. Which cookies and scripts are used and how they impact your visit is specified on the left. Your choices will not impact your visit.</p> <p><strong>Details</strong></p> <p><em>NOTE:</em> Third-party Google scripts on this website may have access to cross-site third-party cookies under the google.com domain. We, the CSEE Department, do not access, read, or write these third-party cookies, and as a result, we do not control their presence on your browser. You may block them by using a third-party cookie blocker in your browser.</p> <p>If you click Accept below to accept the general cookie consent, then a &#8220;wpgdprc-consent&#8221; cookie will be stored on your browser, to record your general consent.</p> <p>If you click Accept below to accept the general cookie consent, and also have Google Analytics cookies enabled (on the sidebar to the left), the CSEE Department website will store and access Google Analytics cookies on your browser. We use the data from these cookies to collect information on website usage statistics and improve user experience. If you do not wish to allow Google Analytics cookies on your browser, then either do not click Accept on the bottom bar, or disable Google Analytics on the left.</p> <p>If you log in to this website, then several Wordpress cookies and session variables will be stored on your browser. Accessing the login screen constitutes your consent to have Wordpress cookies and session variables stored on your computer.</p> <p><strong>External Scripts</strong></p> <p>The CSEE Department website makes use of several external scripts to improve user experience. These include, but are not necessarily limited to: Google Calendar, Google Analytics, and ReCAPTCHA. If you choose to use this website, then you agree to allow these scripts to be loaded and executed.</p> <p><strong>External Links</strong></p> <p>Our service may contain links to other sites. If you click on a third-party link, you will be directed to that site. Note that these external sites are not operated by us. Therefore, we strongly advise you to review the Privacy Policy of these websites. We have no control over, and assume no responsibility for the content, privacy policies, or practices of any third-party sites or services.</p> <p><span class="wpgdprc-text--warning"><strong>NOTE:</strong> These settings will only apply to the browser and device you are currently using.</span></p> </div> </div> <div class="wpgdprc-consent-modal__description" data-target="1"> <p class="wpgdprc-consent-modal__title wpgdprc-consent-modal__title--description">Google Analytics</p> <div class="wpgdprc-content-modal__content"> <p>Enables Google Analytics.</p> </div> <div class="wpgdprc-content-modal__options"> <div class="wpgdprc-checkbox"> <label class="wpgdprc-switch wpgdprc-switch--column wpgdprc-switch--border" for="1"> <span class="wpgdprc-switch__text">Enable?</span> <span class="wpgdprc-switch__switch"> <input class="wpgdprc-switch__input" type="checkbox" id="1" name="1" value="1" /> <span class="wpgdprc-switch__slider round"> <span data-icon="check" class="icon--wrap"> <svg class="icon"> <use href=https://redirect.cs.umbc.edu/wp-content/plugins/wp-gdpr-compliance/Assets/icons//sprite-fontawesome-pro-regular.svg#check></use> </svg> </span> <span data-icon="times" class="icon--wrap"> <svg class="icon"> <use href=https://redirect.cs.umbc.edu/wp-content/plugins/wp-gdpr-compliance/Assets/icons//sprite-fontawesome-pro-regular.svg#times></use> </svg> </span> </span> </span> </label> </div> </div> </div> </div> </div> <div class="wpgdprc-consent-modal__footer"> <div class="wpgdprc-consent-modal__footer__information"> <a href="https://cookieinformation.com/?utm_campaign=van-ons-go-premium&#038;utm_source=van-ons-wp&#038;utm_medium=referral" target="_blank">Powered by Cookie Information</a> </div> <button class="wpgdprc-button wpgdprc-button--secondary">Save my settings</button> </div> </div> </div> </div> <div class="footer"> <div class="container box"> <p style="text-align: left"> <span> ©2024 University of Maryland Baltimore County Computer Science and Electrical Engineering Department<br> </span> <span> 1000 Hilltop Circle, ITE 325, Baltimore, Maryland 21250 | 410-455-3500 | <a href="https://plus.google.com/100554857285792052165"><i class="fa fa-google" title="Google Plus Link"></i></a> <a href="https://www.facebook.com/pages/UMBC-CSEE-Department/104803409392"><i class="fa fa-facebook" title="Facebook Link"></i></a> <a href="https://twitter.com/umbccsee"><i class="fa fa-twitter" title="Twitter Link"></i></a> <a href="https://calendar.google.com/calendar/embed?src=umbccsee@gmail.com"><i class="fa fa-calendar" title="Google Calendar Link"></i></a> <a href="https://www.csee.umbc.edu/feed"><i class="fa fa-rss" title="RSS Link"></i></a> </span> <br> <a href="http://coeit.umbc.edu/">College of Engineering and Information Technology</a> | <a href="/about/contact-us">Contact Us</a> | <a href="https://umbc.edu/go/equal-opportunity">Equal Opportunity</a> | <a href="https://enrollment.umbc.edu/consumer_info/">Consumer Information</a> | <a href="https://accessibility.umbc.edu/digital-content-accessibility/website-accessibility-at-umbc/">Accessibility</a> </p> </div> </div> </body> </html> -->


[CSEE] | [CMSC202] | [Spring '07 CMSC202]             Last Modified: 28 Aug 2006 10:15:53 EDT