1 Basic Concepts of Object-Oriented Design. 2 What is this Object ? There is no real answer to the question, but we ’ ll call it a “ thinking cap ”. l.

Slides:



Advertisements
Similar presentations
Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
Advertisements

Department of Computer Engineering Faculty of Engineering, Prince of Songkla University 1 5 – Abstract Data Types.
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
 Chapter 2 introduces Object Oriented Programming.  OOP is a relatively new approach to programming which supports the creation of new data types and.
Chapter 10 THINKING IN OBJECTS 1 Object Oriented programming Instructor: Dr. Essam H. Houssein.
Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
C++ Plus Data Structures
1 Chapter 6 Object-Oriented Software Design and Implementation.
Important Definitions Class: A structured data type that is used to represent an abstract data type (ADT) Class member: A components of a class. It can.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
1 C++ Plus Data Structures Nell Dale Chapter 2 Data Design and Implementation Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
Lecture 5-6 OOP Overview. Outline A Review of C++ Classes (Lecture 5) OOP, ADTs and Classes Class Definition, Implementation and Use Constructors and.
1 Lecture 29 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
תכנות מונחה עצמים Object Oriented Programming (OOP) אתגר מחזור ב' Classes.
P Chapter 2 introduces Object Oriented Programming. p OOP is a relatively new approach to programming which supports the creation of new data types and.
1 Programming Style Files Lecture 1 Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus Modified by Reneta Barneva, SUNY-Fredonia.
C++ fundamentals.
OBJECT ORIENTED PROGRAMMING IN C++ LECTURE
Object Oriented Software Development
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Data Structures and Algorithms What The Course Is About Data structures is concerned with the representation and manipulation of data. All programs manipulate.
1. Reference  2  Algorithm :- Outline the essence of a computational procedure, step by step instructions.  Program :- an.
C++ Plus Data Structures Data Design and Implementation
1 Chapter-01 Programming Methodologies Procedural/Structured Design Objected-Oriented Design.
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles Modified from the Slides made by Sylvia Sorkin, Community College of Baltimore.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
1 C++ Syntax and Semantics, and the Program Development Process.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems.
1 Chapter Structured Types, Data Abstraction and Classes Dale/Weems.
Programming Life Cycle Problem analysisunderstand the problem Requirements definition specify what program will do High- and low-level designhow it meets.
Abstract Data Type and C++ Classes 1. Object-oriented Programming 2. Abstract Data Types 3. Classes and Objects 4. Examples 5. Member Functions and Member.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems.
1 2 Data Design and Implementation Chapter 2 Data Design and Implementation.
Design.ppt1 Top-down designs: 1. Define the Problem IPO 2. Identify tasks, Modularize 3. Use structure chart 4. Pseudocode for Mainline 5. Construct pseudocode.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More about.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Views of Data Data – nouns of programming world the objects that are manipulated information that is processed Humans like to group information Classes,
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Principles of Programming & Software Engineering.
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
CSI 1340 Introduction to Computer Science II Chapter 1 Software Engineering Principles.
Data Design and Implementation. Definitions Atomic or primitive type A data type whose elements are single, non-decomposable data items Composite type.
Chapter 1 Data Abstraction: The Walls CS Data Structures Mehmet H Gunes Modified from authors’ slides.
Chapter 1 Software Engineering Principles. Problem analysis Requirements elicitation Software specification High- and low-level design Implementation.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
Chapter 2 Principles of Programming and Software Engineering.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
 Chapter 2 introduces Object Oriented Programming.  OOP is a relatively new approach to programming which supports the creation of new data types and.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More about.
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
1 Chapter 12 Classes and Abstraction. 2 Chapter 12 Topics Meaning of an Abstract Data Type Declaring and Using a class Data Type Using Separate Specification.
Object Oriented Programming Data Structures and Other Objects Using C++ Dr. Donghyun Kim Department of Computer Science College of Computing and Software.
Chapter 12 Classes and Abstraction
C++ Plus Data Structures
Object Oriented Programming
Chapter 1 Data Abstraction: The Walls
About the Presentations
More about OOP and ADTs Classes
Stack ADT & Modularity 2 implementations of the Stack abstract data type: Array Linked List Program design: modularity, abstraction and information hiding.
Object Oriented Programming
Classes and Data Abstraction
More about OOP and ADTs Classes
COP 3330 Object-oriented Programming in C++
CPS120: Introduction to Computer Science
Presentation transcript:

1 Basic Concepts of Object-Oriented Design

2 What is this Object ? There is no real answer to the question, but we ’ ll call it a “ thinking cap ”. l The plan is to describe a thinking cap by telling you what actions can be done to it.

3 Using the Object’s Slots l You may put a piece of paper in each of the two slots (green and red), with a sentence written on each. You may push the green button and the thinking cap will speak the sentence from the green slot ’ s paper. l And same for the red button.

4 Example

5 That test was a breeze !

6 Example I should study harder !

7 Thinking Cap Implementation l We can implement the thinking cap using a data type called a class. class ThinkingCap {... };

8 Thinking Cap Implementation l The class will have two components called green_string and red_string. These compnents are strings which hold the information that is placed in the two slots. l Using a class permits two new features... class ThinkingCap {... char green_string[50]; char red_string[50]; };

9 Thinking Cap Implementation ÊThe two components will be private member variables. This ensures that nobody can directly access this information. The only access is through functions that we provide for the class. class ThinkingCap {... private: char green_string[50]; char red_string[50]; };

10 Thinking Cap Implementation ËIn a class, the functions which manipulate the class are also listed. class ThinkingCap { public:... private: char green_string[50]; char red_string[50]; }; Prototypes for the thinking cap functions go here, after the word public:

11 Thinking Cap Implementation ËIn a class, the functions which manipulate the class are also listed. class ThinkingCap { public:... private: char green_string[50]; char red_string[50]; }; Prototypes for the thinking cap member functions go here

12 Thinking Cap Implementation class ThinkingCap { public: void slots(char new_green[ ], char new_red[ ]); void push_green( ) const; void push_red( ) const; private: char green_string[50]; char red_string[50]; }; Our thinking cap has at least three member functions: Function bodies will be elsewhere.

13 Thinking Cap Implementation class ThinkingCap { public: void slots(char new_green[ ], char new_red[ ]); void push_green( ) const; void push_red( ) const; private: char green_string[50]; char red_string[50]; }; The keyword const appears after two prototypes: This means that these functions will not change the data stored in a ThinkingCap.

14 Files for the Thinking Cap l The ThinkingCap class definition, which we have just seen, is placed with documentation in a file called thinker.h, outlined here. l The implementations of the three member functions will be placed in a separate file called thinker.cxx, which we will examine in a few minutes. Documentation Class definition: ThinkingCap class definition which we have already seen

15 Using the Thinking Cap l A program that wants to use the thinking cap must include the thinker header file (along with its other header inclusions). #include #include "thinker.h"...

16 Using the Thinking Cap l Just for fun, the example program will declare two ThinkingCap variables named student and fan. #include #include "thinker.h" int main( ) { ThinkingCap student: ThinkingCap fan;

17 Using the Thinking Cap l Just for fun, the example program will declare two ThinkingCap objects named student and fan. #include #include "thinker.h" int main( ) { ThinkingCap student; ThinkingCap fan;

18 Using the Thinking Cap l The program starts by calling the slots member function for student. #include #include "thinker.h" int main( ) { ThinkingCap student; ThinkingCap fan; student. slots( "Hello", "Goodbye");

19 Using the Thinking Cap l The program starts by activating the slots member function for student. #include #include "thinker.h" int main( ) { ThinkingCap student: ThinkingCap fan; student. slots( "Hello", "Goodbye");

20 Using the Thinking Cap ÊThe member function activation consists of four parts, starting with the object name. int main( ) { ThinkingCap student; ThinkingCap fan; student. slots( "Hello", "Goodbye"); Name of the object

21 Using the Thinking Cap ËThe instance name is followed by a period. int main( ) { ThinkingCap student; ThinkingCap fan; student. slots( "Hello", "Goodbye"); A Period

22 Using the Thinking Cap ÌAfter the period is the name of the member function that you are activating. int main( ) { ThinkingCap student; ThinkingCap fan; student. slots( "Hello", "Goodbye"); Name of the Function

23 Using the Thinking Cap ÍFinally, the arguments for the member function. In this example the first argument (new_green) is "Hello" and the second argument (new_red) is "Goodbye". #include "thinker.h" int main( ) { ThinkingCap student; ThinkingCap fan; student. slots( "Hello", "Goodbye"); Arguments

24 A Quiz How would you activate student's push_green member function ? What would be the output of student's push_green member function at this point in the program ? int main( ) { ThinkingCap student; ThinkingCap fan; student. slots( "Hello", "Goodbye");

25 A Quiz Notice that the push_green member function has no arguments. At this point, activating student.push_green will print the string Hello. int main( ) { ThinkingCap student; ThinkingCap fan; student. slots( "Hello", "Goodbye"); student. push_green( );

26 A Quiz Trace through this program, and tell me the complete output. int main( ) { ThinkingCap student; ThinkingCap fan; student. slots( "Hello", "Goodbye"); fan. slots( "Go Cougars!", "Boo!"); student. push_green( ); fan. push_green( ); student. push_red( );...

27 A Quiz Hello Go Cougars! Goodbye int main( ) { ThinkingCap student; ThinkingCap fan; student. slots( "Hello", "Goodbye"); fan. slots( "Go Cougars!", "Boo!"); student. push_green( ); fan. push_green( ); student. push_red( );...

28 The Key Software Trend: Object Technology l Objects n Reusable software components that model real world items n Meaningful software units –Date objects, time objects, paycheck objects, invoice objects, audio objects, video objects, file objects, record objects, etc. –Any noun can be represented as an object n More understandable, better organized and easier to maintain than procedural programming n Favor modularity

Information Hiding l Hiding the details of a function or data structure with the goal of controlling access to the details of a module or structure. PURPOSE: To prevent high-level designs from depending on low-level design details that may be changed.

Two Approaches to Building Manageable Modules Divides the problem into more easily handled subtasks, until the functional modules (subproblems) can be coded. Identifies various objects composed of data and operations, that can be used together to solve the problem. FUNCTIONAL DECOMPOSITION OBJECT-ORIENTED DESIGN FOCUS ON: processes FOCUS ON: data objects

Find Weighted Average Print Weighted Average Functional Design Modules Main Print Data Print Heading Get Data Prepare File for Reading

Object-Oriented Design A technique for developing a program in which the solution is expressed in terms of objects -- self- contained entities composed of data and operations on that data. Private data << setf Private data >> get ignore cincout

33 OCD: Object-Centered Design l 1. Identify the objects in the problem's specification and their types. l 2. Identify the operations needed to solve the problem. l 3. Arrange the operations in a sequence of steps, called an algorithm, which, when applied to the objects, will solve the problem.

More about OOD l Languages supporting OOD include: C++, Java, Smalltalk, Eiffel, and Object-Pascal. l A class is a programmer-defined data type and objects are variables of that type. l In C++, cin is an object of a data type (class) named istream, and cout is an object of a class ostream. Header files iostream.h and fstream.h contain definitions of stream classes.

Procedural vs. Object-Oriented Code “Read the specification of the software you want to build. Underline the verbs if you are after procedural code, the nouns if you aim for an object-oriented program.” Brady Gooch, “What is and Isn’t Object Oriented Design,” 1989.

36 Data Abstraction l Separation of a data type’s logical properties from its implementation. LOGICAL PROPERTIESIMPLEMENTATION What are the possible values? How can this be done in C++? What operations will be needed? How can data types be used?

37 APPLICATION REPRESENTATION Data Encapsulation l is the separation of the representation of data from the applications that use the data at a logical level; a programming language feature that enforces information hiding. int y; y = 25;

38 Encapsulated C++ Data Type int Value range: INT_MIN.. INT_MAX Operations: + prefix - prefix + infix - infix * infix / infix % infix Relational Operators infix TYPE int (inside) Representation of int as 16 bits two’s complement + Implementation of Operations

39 Abstract Data Type (ADT) l A data type whose properties (domain and operations) are specified independently of any particular implementation.

40 l Application (or user) level: modeling real-life data in a specific context. l Logical (or ADT) level: abstract view of the domain and operations. WHAT l Implementation level: specific representation of the structure to hold the data items, and the coding for operations. HOW Data from 3 different levels

41 Viewing a library from 3 different levels l Application (or user) level: Library of Congress, or Baltimore County Public Library. l Logical (or ADT) level: domain is a collection of books; operations include: check book out, check book in, pay fine, reserve a book. l Implementation level: representation of the structure to hold the “books”, and the coding for operations.

42 Composite Data Type A composite data type is a type which l stores a collection of individual data components under one variable name, l and allows the individual data components to be accessed.

4 Basic Kinds of ADT Operations l Constructor -- creates a new instance (object) of an ADT. l Transformer -- changes the state of one or more of the data values of an instance. l Observer -- allows us to observe the state of one or more of the data values without changing them. l Iterator -- allows us to process all the components in a data structure sequentially. 43

Two Forms of Composite Data Types Components are not organized with respect to one another. The organization determines method used to access individual data components. UNSTRUCTURED STRUCTURED EXAMPLES: EXAMPLES: arrays classes and structs 44

45 C++ Built-In Data Types Composite array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double

46 Records A record is a composite data type made up of a finite collection of not necessarily homogeneous elements called members or fields. For example....year 1999.maker ‘h’ ‘o’ ‘n’ ‘d’ ‘a’ ‘\0’....price thisCar at Base Address 6000

47 struct CarType { int year ; char maker[10]; float price ; } ; CarType thisCar; //CarType variables CarType myCar;

48 Accessing struct members The member selection operator (period. ) is used between the variable name and the member identifier to access individual members of a record (struct or class) type variable. EXAMPLES myCar.year thisCar.maker[4]

49 Valid struct operations l Operations valid on an entire struct type variable: assignment to another struct variable of same type, pass as a parameter to a function (either by value or by reference), return as the value of a function.