Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 1 Basic Concepts of Object-Oriented Design

2 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 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 4 Example

5 5 That test was a breeze !

6 6 Example I should study harder !

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

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

29 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.

30 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

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

32 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 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.

34 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.

35 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 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 37 APPLICATION 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1 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 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 39 Abstract Data Type (ADT) l A data type whose properties (domain and operations) are specified independently of any particular implementation.

40 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 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 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.

43 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

44 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 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 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 18678.92 thisCar at Base Address 6000

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

48 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 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.


Download ppt "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."

Similar presentations


Ads by Google