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

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.
1 Classes Object-oriented programming: Model the problem as a collection of objects that have certain attributes and interact with one another and/or the.
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.
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Abstract Data Types Development and Implementation.
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Templates and Polymorphism Generic functions and classes.
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Advanced Parameter Passing Reference parameters, const parameters, and default parameters.
 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.
C++ data types. Structs vs. Classes C++ Classes.
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Templates and Polymorphism Generic functions and classes
Miscellaneous JPC and JWD © 2002 McGraw-Hill, Inc.
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.
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.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Chapter 6: Programmer- defined Functions Development of simple functions using value and reference parameters JPC and JWD © 2002 McGraw-Hill, Inc. Modified.
Object-Oriented Programming. Procedural Programming All algorithms in a program are performed with functions and data can be viewed and changed directly.
Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
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.
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.
CLASSES AND OBJECTS Chapter 3 : constructor, Separate files, validating data.
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
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
Defining objects with attributes and behavior
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
Development and Implementation
CPS120: Introduction to Computer Science
Lecture 8 Object Oriented Programming (OOP)
A Graphical Application Programmer Interface
Chapter 5 Classes.
Presentation transcript:

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

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

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

#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(); Simple RectangleShape Header File Passed by reference, do not want a copy of the window ezwin.h get us definitions of SimpleWindow and color Access right indicates no limitations on who can use these members Preprocessor directives

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

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

private: // data members SimpleWindow &Window; float thisXCenter; float thisYCenter; color thisColor; float thisWidth; float thisHeight; }; #endif Simple RectangleShape A client cannot directly access either private or protected data members Access right 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; }