1 / 43

The Class Construct and OOD

Chapter 7 :. The Class Construct and OOD. Key Concepts:. destructor const functions access specification public private object-oriented analysis object-oriented design. class construct information hiding encapsulation data members member functions constructors

wilke
Download Presentation

The Class Construct and OOD

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 7 : The Class Construct and OOD

  2. Key Concepts: • destructor • const functions • access specification • public • private • object-oriented analysis • object-oriented design • class construct • information hiding • encapsulation • data members • member functions • constructors • inspectors • mutators • facilitators

  3. Usage of Class: Defining objects with attributes and behavior

  4. Class Types • Class construct • Allows programmers to define new data types for representing information • Class type objects can have both attribute components and behavior components • Provides the object-oriented programming in C++ • Example we shall consider is • RectangleShape of ezWindow library

  5. Terminology • Client • Program using a class • Object behaviors • Realized in C++ via member functions (methods) • RectangleShapescan be drawn or resized • Object attributes • Are known as data members in C++ • RectangleShapes have width, height, position, color

  6. A valid C++ name Class keyword Public keyword Private keyword The declaration of class classClassName { public: // Prototypes for constructors // and public member functions // and declarations for public // data member go here ... ... private: // Prototypes for private // member functions and // declarations for private // data member go here ... ... }

  7. classRectangleShape { public: RectangleShape(SimpleWindow &Window, float fXCoord, float fYCoord, const color &Color, float fWidth , float fHeight); void Draw(); color GetColor() const; float GetWidth() const; float GetHeight()const; void GetSize(float &fWidth, float &fHeight) const; void GetPosition(float &fXCoord, float &fYCoord) const; SimpleWindow& GetWindow() const; void SetColor(const color &Color); void SetPosition(float fXCoord, float fYCoord); void SetSize(float fWidth, float fHeight); private: SimpleWindow &Window; color m_emColor; float m_fXCenter; float m_fYCenter; float m_fWidth; float m_fHeight; } The declaration of class

  8. Member Functions The behavior of object • Provide a controlled interface to data members and object access and manipulation • Create objects of the class • Inspect, mutate, and manipulate object of the class • Can be used to keep data members in a correct state

  9. Identifier.Message([Arg1, Arg2, … ArgN]); Optional Argument List (Message Contents) Member Access Operator Message Type (Member Function) Object Name Member Functions • Send a message to objects by invoking a member function through object • R.SetSize(3, 5); • R.SetColor(Red); • R.Draw();

  10. Member Functions • Constructors • Member functions that initialize an object and allocate resources for the object during its definition • Constructor’s name is always same as the name of class • Constructors do not have a type • A class can have multi-constructor • Constructors are automatically invocated when the object is defined • Example RectangleShape(SimpleWindow &Window, float fXCoord, float fYCoord, const color &Color, float fWidth , float fHeight);

  11. ReturnType GetXXX() const; Member Functions • Inspectors • Member functions that act as a messenger that returns the value of an attribute • Inspectors normally have a syntax some like • Example • RectangleShapes have an inspector color GetColor() const; • Invocate it by object R color CurrColor = R.GetColor();

  12. void SetXXX(ParameterList); Member Functions • Mutators • Changes the value of an attribute • Mutators normally have a syntax some like • Example • RectangleShapes have a mutator void SetColor(const color &Color); • Invocate it by object R R.SetColor(Black);

  13. Member Functions • Facilitators • Causes an object to perform some action or service • Example • RectangleShapes have a facilitator void Draw(); • Invocate it by object R R.Draw();

  14. Access specification • public access • All clients and class members have access to the public members • The public members serve as the interfaces for outside • private access • Only class members have access to the private members • Private members are the hiding information • It is best that always put the data members of class into private access

  15. class public: Access from outside of class Only classmembershave accessto theprivatemembers Public Members private: Private Members Encapsulation The public members serve as the interfaces for outside Private members are the hiding information for outside, so access denied

  16. A Simple RectangleShape Class • Consider a simpler version of the RectangleShape than what is defined in rect.h • Giving the class definition not the implementation • The definition in rect.h uses inheritance and member functions with default parameters • If you are wondering what is missing • Default constructor parameters • Member function • Erase() • Inherited member functions • HasBorder(), SetBorder(), and ClearBorder()

  17. Preprocessor directives ezwin.hget us definitions of SimpleWindow and color Passed by reference, do not want a copy of the window #ifndef RECT_SHAPE_H #define RECT_SHAPE_H #include "ezwin.h” class RectangleShape { public: // constructor RectangleShape(SimpleWindow &Win, float x, float y, const color &c, float cx, float cy); // facilitator void Draw(); // inspectors color GetColor() const; float GetWidth() const; float GetHeight() const; void GetSize(float &cx, float &cy)const; void GetPosition(float &x, float &y)const; SimpleWindow& GetWindow() const; constindicates the member functions won’t change the object Reference return, brings actual window (not a copy)

  18. Lack of const indicate the member function might change the object // mutators void SetColor(const color &c); void SetPosition(float x, float y); void SetSize(float cx, float cy); private: // data members SimpleWindow &m_Window; float m_fXCenter; float m_fYCenter; color m_emColor; float m_fWidth; float m_fHeight; }; #endif//RECT_SHAPE_H Keep the actual window (not a copy) Close of #ifndef directive

  19. RectangleShape Window Color XCenter YCenter Width Height Draw() GetColor() GetWidth() GetHeight() GetSize() GetPosition() GetWindow() SetColor() SetPosition() SetSize() Object: R1 Window : &W Color : Cyan XCenter: 2.0 YCenter: 2.0 Width : 4.0 Height : 3.0 Object: R2 Window : &W Color : Yellow XCenter: 15.0 YCenter: 10.0 Width : 5.0 Height : 6.0 Define Object RectangleShape R1(W,2,2,Cyan,4,3); RectangleShape R2(W,5,8,Red,5,6);

  20. Access Tests • Consider SimpleWindow W("Testing", 20, 10); RectangleShape R1(W, 2, 2, Cyan, 4, 3); const RectangleShape R2(W, 15, 10, Red, 5, 6); • Can we do the following? • color c = R1.GetColor(); • color d = R2.GetColor(); • color d = R1.m_Color; • R1.SetColor(Red); • R2.SetColor(Black);

  21. Program 7.1 Display color palette

  22. Program 7.1 Display color palette #include "rect.h" const float EX_SIDE_SIZE = 1.0; const float EX_Y_POSITION= 4.0f; SimpleWindow ColorWindow("Color Palette", 8.0, 8.0); int ApiMain() { float fXPosition = 1.5; ColorWindow.Open(); // Create a RectangleShape to use RectangleShape ColorPatch(ColorWindow, fXPosition, EX_Y_POSITION, White, EX_SIDE_SIZE, EX_SIDE_SIZE); // Loop over colors drawing ColorSquare with // that color next to the previous square for (color c = Red; c <= Magenta; c = (color) (c + 1)) { ColorPatch.SetColor(c); ColorPatch.SetPosition(fXPosition, EX_Y_POSITION); ColorPatch.Draw(); fXPosition += EX_SIDE_SIZE; } return 0; }

  23. Program 7-2 Building A Kaleidoscope

  24. Quadrant 2 Quadrant 1 Quadrant 3 Quadrant 4 Program 7-2 Building A Kaleidoscope • Object-Based Programming • Design of Kaleidoscope: • The function (not class), Kaleidoscope(), draws squares of various sizes, colors and position symmetrically within a simple window • Identical trinkets are display in diagonally opposite quadrants of four quadrants of the window • The functions RandomColor(),RandomTrinketSize() and RandomOffset() generate the various sizes, colors and positions of the trinkets respectively in function Kaleidoscope() • Kaleidoscope()will be calledrepeatedly in an interval of time

  25. Object-Oriented Analysis and Design(OOA & OOD) • The difference between object-oriented and traditional software development methods Requirements specification Requirements specification Testing Testing Implementation Design Design Implementation Traditional methods Object-oriented methods

  26. Factory automation system Display Unit Control Unit Accept or reject signal Video Camera Conveyor control signal Video control signal Video image Parts Conveyor belt

  27. Parts-inspection trainer - OOA Our job: Write a program that can be used to train and evaluate the performance of potential part inspectors

  28. Parts-inspection trainer - OOA • More precise description • The program simulates the operation of the inspection component of the assembly line. • To simulate parts moving down the conveyor belt, we will use three rectangle with randomly chosen colors. • The display unit is represented by a window where three rectangles will be displayed. • A console accepts the input of inspector. And the inspector can accept a group of parts by typing the letter “a” or can reject a group of parts by typing letter “r”. • A group of parts is acceptable if two of the parts are same color, otherwise the group should be rejected. • After the training session is completed, the following statistic should be displayed or printed: the length of the training session, the number of inspected part group, the number of correct and incorrect decisions, and the percentage of correct decisions.

  29. Parts-inspection trainer - OOD • Step one – Determine the objects or classes in system • Step two – Determine how the objects interact • Step three – Determine the behaviors and attributes of the object

  30. Parts-inspection trainer - OOD 1. Determine the objects • Video display – class VideoDisplayUint • Video camera – class VideoCamera • Video image – class VideoImage • Parts – class Part • Console – class Console • Simulation controller – High level design • Some other uncertain objects

  31. Console Video display Video Image Simulation controller Video Image Video Camera Control signal Part Parts-inspection trainer - OOD 2. Determine how the objects interact

  32. VideoCamera Status CaptureImage() TurnOn() TurnOff() GetStatus() VideoImage Part1 Part2 Part3 GetPart1() GetPart2() GetPart3() Console Status GetStatus() GetResponse() TurnOn() TurnOff() PrintStatistic() Part Color GetColor() VideoDisplayUnit Window Status DisplayImage() GetStatus() GetWindow() Parts-inspection trainer - OOD 3. Determine the behaviors and attributes of the object

  33. VideoCamera Status CaptureImage() TurnOn() TurnOff() GetStatus() Parts-inspection trainer - OOD 3. Determine the behaviors and attributes of the object #ifndef VIDEOCAMERA_H #define VIDEOCAMERA_H #include "image.h" enum CameraStatus { CameraOn, CameraOff }; class VideoCamera { public: VideoCamera(); CameraStatus GetStatus()const; VideoImage CaptureImage(); void TurnOn(); void TurnOff(); private: CameraStatus m_Status; }; #endif //VIDEOCAMERA_H

  34. VideoImage Part1 Part2 Part3 GetPart1() GetPart2() GetPart3() Parts-inspection trainer - OOD 3. Determine the behaviors and attributes of the object #ifndef VIDEOIMAGE_H #define VIDEOIMAGE_H #include "part.h“ class VideoImage { public: VideoImage(const color &c1, const color &c2, const color &c3); Part GetPart1() const; Part GetPart2() const; Part GetPart3() const; private: Part m_Part1; Part m_Part2; Part m_Part3; }; #endif //VIDEOIMAGE_H

  35. Part Color GetColor() Parts-inspection trainer - OOD 3. Determine the behaviors and attributes of the object #ifndef PART_H #define PART_H #include "ezwin.h“ class Part { public: Part(const color &c = Red); color GetColor() const; private: color m_Color; }; #endif//PART_H

  36. VideoDisplayUnit Window Status DisplayImage() GetStatus() GetWindow() Parts-inspection trainer - OOD 3. Determine the behaviors and attributes of the object #ifndef DISPLAYUNIT_H #define DISPLAYUNIT_H #include <string> #include "ezwin.h" #include "image.h" using namespace std; enum DisplayStatus{ DisplayOn, DisplayOff }; class VideoDisplayUnit { public: VideoDisplayUnit(const string &Title); void DisplayImage(const VideoImage &Image); void TurnOn(); void TurnOff(); private: SimpleWindow m_Window; DisplayStatus m_Status; }; #endif //DISPLAYUNIT_H

  37. Console Status GetStatus() GetResponse() TurnOn() TurnOff() PrintStatistic() Parts-inspection trainer - OOD 3. Determine the behaviors and attributes of the object #ifndef CONSOLE_H #define CONSOLE_H enum ConsoleStatus { ConsoleOn, ConsoleOff }; class Console { public: Console(); void TurnOn(); void TurnOff(); char GetResponse(); void PrintStatistic(long lTime, int iAttempts, int iCorrect, int iWrong); private: ConsoleStatus m_Status; }; #endif//CONSOLE_H

  38. Parts-inspection trainer - OOD 4. The simulation controller – High level design  Get part image  Display parts images in the display window  Read and record the response of the trainee  Score the response  Check to see whether the training session should end. If time is not up, go back to step . If time is up, the final statistics of the training session are computed and printed.

  39. Home works End of Chapter 7 Exercises 7.6 Exercises 7.8 Exercises 7.20

More Related