Defining objects with attributes and behavior

Slides:



Advertisements
Similar presentations
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Advertisements

The Line Class Suppose you are involved in the development of a large mathematical application, and this application needs an object to represent a Line.
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
CHAPTER 13 Object Oriented Programming. Objectives  Design class definitions  Implement data hiding and encapsulation  Use accessor and mutator methods.
Visual C++ Programming: Concepts and Projects Chapter 13A: Object-Oriented Programming (Concepts)
Abstract Data Types Development and Implementation JPC and JWD © 2002 McGraw-Hill, Inc.
Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Object Oriented Concepts 2 Stewart Blakeway FML 213
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Templates and Polymorphism Generic functions and classes.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
1 Lecture 06(Abstract Classes)Lecture 9 Abstract Classes Overview  Abstract Classes: A Definition.  Declaring Abstract Classes.  Abstract Methods: A.
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Inheritance Mechanism for deriving new classes uses existing classes as bases.
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
UML Basics & Access Modifier
1 Interface Types & Polymorphism & introduction to graphics programming in Java.
Templates and Polymorphism Generic functions and classes
Miscellaneous JPC and JWD © 2002 McGraw-Hill, Inc.
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.
EzWindows API A Graphical Application Programmer Interface JPC and JWD © 2002 McGraw-Hill, Inc.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
CSC 205 Java Programming II Polymorphism. Topics Polymorphism The principle of substitution Dynamic binding Object type casting Abstract class The canonical.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Chapter 4 Introduction to Classes, Objects, Methods and strings
The Class Construct Defining objects with attributes and behavior JPC and JWD © 2002 McGraw-Hill, Inc.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Inheritance Mechanism for deriving new classes from existing classes Chapter 13.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
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.
The Class Construct and OOD Chapter 7 : class construct information hiding encapsulation data members member functions constructors inspectors mutators.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
OBJECT ORIENTED PROGRAMMING. Design principles for organizing code into user-defined types Principles include: Encapsulation Inheritance Polymorphism.
1 Review for Midterm 2 Aaron Bloomfield CS 101-E.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Classes Classes are a major feature of C++. They support – – Abstraction – Data hiding – Encapsulation – Modularity – Re-use through inheritance.
1 Sections 5.1 – 5.2 Digital Image Processing Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Re-Intro to Object Oriented Programming
Classes C++ representation of an object
Access Functions and Friend Functions
Abstract Data Types Programmer-created data types that specify
A First C++ Class – a Circle
Classes Object-oriented programming: Example: Bank transactions
Introduction to Classes
Introduction to Classes
4.14 GUI and Graphics Case Study: Creating Simple Drawings (Cont.)
Classes and Data Abstraction
Implementing Non-Static Features
C++ Interlude 1 C++ Classes
COMS 261 Computer Science I
C++ Tutorial Rob Jagnow
Development and Implementation
Classes C++ representation of an object
CPS120: Introduction to Computer Science
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
C++ data types.
Lecture 8 Object Oriented Programming (OOP)
A Graphical Application Programmer Interface
SPL – PS3 C++ Classes.
Chapter 5 Classes.
Presentation transcript:

Defining objects with attributes and behavior The Class Construct Defining objects with attributes and behavior JPC and JWD © 2002 McGraw-Hill, Inc.

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 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++

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 Client Program using a class Object behaviors Realized in C++ via member functions (methods) Object attributes Are known as data members in C++ 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++

Member Functions 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 SetSize() SetColor() Draw()

Member Functions Constructors Member functions that initialize an object during its definition RectangleShape R(W, x, y, c, w, h); Factoid Constructors do not have a type Considered superfluous

Member Functions Inspectors Member functions that act as a messenger that returns the value of an attribute Example RectangleShapes have an inspector GetColor() color CurrColor = R.GetColor();

Member Functions Mutators Changes the value of an attribute Example RectangleShapes have a mutator SetColor() R.SetColor(Black);

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

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

Simple RectangleShape Header File #ifndef RECT_SHAPE_H #define RECT_SHAPE_H #include "ezwin.h" class RectangleShape { public: // constructor RectangleShape(SimpleWindow &Window, float XCoord, float YCoord, const color &c, float Width, float Height); // facilitator void Draw(); Preprocessor directives Passed by reference, do not want a copy of the window Access right indicates no limitations on who can use these members ezwin.h get us definitions of SimpleWindow and color

Simple RectangleShape // inspectors color GetColor() const; float GetWidth() const; float GetHeight() const; void GetSize(float &Width, float &Height) const; void GetPosition(float &XCoord, float &YCoord) SimpleWindow& GetWindow() const; Indicates the member functions won’t change the object Reference return, brings actual window (not a copy)

Simple RectangleShape Lack of const indicate the member function might change the object // mutators void SetColor(const color &c); void SetPosition(float XCoord, float YCoord); void SetSize(float Width, float Height);

Simple RectangleShape Access right private: // data members SimpleWindow &Window; float thisXCenter; float thisYCenter; color thisColor; float thisWidth; float thisHeight; }; #endif A client cannot directly access either private or protected data members Close of #ifndef directive

Access Tests Consider SimpleWindow W("Testing", 20, 10); RectangleShape R(W, 2, 2, Blue, 4, 3); const RectangleShape S(W, 15, 10, Red, 5, 6); Can we do the following? color c = R.GetColor(); color d = S.GetColor(); color d = R.thisColor; R.DetColor(Yellow); S.SetColor(Black);

The RectangleShape Class Public access All clients and class members have access to the public members Private access Only class members have access to the private members

#include "rect.h” SimpleWindow ColorWindow("Color Palette", 8.0, 8.0); int ApiMain() { const int SideSize = 1; float XPosition = 1.5; const float YPosition = 4; ColorWindow.Open(); RectangleShape ColorPatch(ColorWindow, XPosition, YPosition, White, SideSize, SideSize); for (int c = Red; c <= Magenta; c = color(c + 1)) { ColorPatch.SetColor(color(c)); ColorPatch.SetPosition(XPosition, YPosition); ColorPatch.Draw(); XPosition += SideSize; } return 0;