The Class Construct and OOD Chapter 7 : class construct information hiding encapsulation data members member functions constructors inspectors mutators.

Slides:



Advertisements
Similar presentations
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Advanced Parameter Passing Reference parameters, const parameters, and default parameters.
Advertisements

 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Software Testing and Quality Assurance
C++ Classes in Depth. Topics Designing Your Own Classes Attributes and Behaviors Writing Classes in C++ Creating and Using Objects.
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Inheritance Mechanism for deriving new classes uses existing classes as bases.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
ASP.NET Programming with C# and SQL Server First Edition
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
IT PUTS THE ++ IN C++ Object Oriented Programming.
UML Basics & Access Modifier
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Abstract Data Types Using Classes Lecture-5. Abstract Data Types Using Classes Representing abstract data types using C++ We need the following C++ keywords.
Miscellaneous JPC and JWD © 2002 McGraw-Hill, Inc.
1 Object-Oriented Programming Using C++ CLASS 27.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Modifying objects Operators and Expressions.
Modifying objects Operators and Expressions JPC and JWD © 2002 McGraw-Hill, Inc.
EzWindows API A Graphical Application Programmer Interface JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Copyright  Hannu Laine C++-programming Part 3 Hannu Laine.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
1 Chapter 13 Introduction to Classes. 2 Topics 12.1 Procedural and Object-Oriented Programming 12.2 Introduction to Classes 12.3 Defining an Instance.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
CONSTRUCTORS AND THEIR TYPES. Prepared by. MURLI MANOHAR. PGT (COMP
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Object-Oriented Programming. Procedural Programming All algorithms in a program are performed with functions and data can be viewed and changed directly.
11 Introduction to Object Oriented Programming (Continued) Cats.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More about.
The Class Construct Defining objects with attributes and behavior JPC and JWD © 2002 McGraw-Hill, Inc.
1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
1 CSC241: Object Oriented Programming Lecture No 02.
Structures and Classes Version 1.0. Topics Structures Classes Writing Structures & Classes Member Functions Class Diagrams.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 12 Introduction to Classes.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Objects and Classes.
Inheritance Mechanism for deriving new classes from existing classes Chapter 13.
Object-Oriented Paradigm The Concept  Bundled together in one object  Data Types  Functionality  Encapsulation.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Classes, Interfaces and Packages
1 Introduction to Object Oriented Programming Chapter 10.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Starting Out with C++, 3 rd Edition 1 Chapter 13 – Introduction to Classes Procedural and Object-Oriented Programming Procedural programming is a method.
CLASSES AND OBJECTS Chapter 3 : constructor, Separate files, validating data.
1 Object-Oriented Programming Using C++ CLASS 2 Honors.
1 Class 19 Chapter 13 – Creating a class definition.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Object-Oriented Programming Using C++ Third Edition Chapter 7 Using Classes.
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Creating Your Own Classes
A First C++ Class – a Circle
Defining objects with attributes and behavior
Review: Two Programming Paradigms
Introduction to Classes
Introduction to Classes
COMS 261 Computer Science I
Modifying Objects Assignment operation Assignment conversions
Lecture 8 Object Oriented Programming (OOP)
Chapter 5 Classes.
Presentation transcript:

The Class Construct and OOD Chapter 7 :

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

Defining objects with attributes and behavior Usage of Class:

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

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

The declaration of class class ClassName { 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... } Class keyword Public keyword Private keyword A valid C++ name

class RectangleShape { 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

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 Member Functions The behavior of object

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

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);

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(); Member Functions ReturnType GetXXX() const;

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); Member Functions void SetXXX(ParameterList);

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

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

public:public: private:private: classclass Access from outside of class The public members serve as the interfaces for outside Private members are the hiding information for outside, so access denied Public Members Private Members Only class members have access to the private members Encapsulation

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()

#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; Preprocessor directives ezwin.h get us definitions of SimpleWindow and color Passed by reference, do not want a copy of the window const indicates the member functions won’t change the object Reference return, brings actual window (not a copy)

// 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 Lack of const indicate the member function might change the object Close of #ifndef directive Keep the actual window (not a copy)

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 RectangleShape R1(W,2,2,Cyan,4,3); RectangleShape R2(W,5,8,Red,5,6); Define Object

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);

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; } Program 7.1 Display color palette

Program 7-2 Building A Kaleidoscope

Quadrant 2 Quadrant 1 Quadrant 3 Quadrant 4 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 called repeatedly in an interval of time

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

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

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

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. Parts-inspection trainer - OOA

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

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

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

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

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 VideoCamera Status CaptureImage() TurnOn() TurnOff() GetStatus()

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 VideoImage Part1 Part2 Part3 GetPart1() GetPart2() GetPart3()

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 Part Color GetColor()

Parts-inspection trainer - OOD 3. Determine the behaviors and attributes of the object #ifndef DISPLAYUNIT_H #define DISPLAYUNIT_H #include #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 VideoDisplayUnit Window Status DisplayImage() GetStatus() GetWindow()

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 Console Status GetStatus() GetResponse() TurnOn() TurnOff() PrintStatistic()

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.

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