Communication OO Software Design and Construction Computer Science Dept Va Tech February 2001 ©2001 McQuain WD & Keller BJ 1 Communicating Objects Think.

Slides:



Advertisements
Similar presentations
Programming Languages and Paradigms
Advertisements

Exceptions OO Software Design and Construction Computer Science Dept Va Tech January 2002 ©2002 McQuain WD & Keller BJ 1 Exceptions exceptiona program.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Rossella Lau Lecture 6, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 6: More on class construction UML and an.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Chapter 5 - Arrays CSC 200 Matt Kayala 2/27/06. Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays.
CS31: Introduction to Computer Science I Discussion 1A 5/28/2010 Sungwon Yang
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
1 Class Constructors a class constructor is a member function whose purpose is to initialize the private data members of a class object the name of a constructor.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Designing Classes OO Software Design and Construction Computer Science Dept Va Tech January 2002 ©2002 McQuain WD & Keller BJ 1 Designing the Classes Once.
Classes Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd Spetember 2006.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
Pointer Data Type and Pointer Variables
Design & Evaluation OO Software Design and Construction Computer Science Dept Va Tech February 2000 ©2000 McQuain WD 1 Class Design and Evaluation Class.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
Functions Pass by Reference Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
SEN 909 OO Programming in C++ Final Exam Multiple choice, True/False and some minimal programming will be required.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
9. Types Intro Programming in C++ Computer Science Dept Va Tech August, 2002 © Barnette ND & McQuain WD 1 Data Types data type:a collection of.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
1 Advanced Issues on Classes Part 3 Reference variables (Tapestry pp.581, Horton 176 – 178) Const-reference variables (Horton 176 – 178) object sharing:
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review Part-I.
The Big Three Based on Weiss “Data Structures and algorithm Analysis CS240 Computer Science II.
Slide 1 Chapter 8 Operator Overloading, Friends, and References.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
CSE 332: Design Patterns Review: Design Pattern Structure A design pattern has a name –So when someone says “Adapter” you know what they mean –So you can.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Polymorphism OO Software Design and Construction Computer Science Dept Va Tech January 2002 ©2002 McQuain WD & Keller BJ 1 Definition polymorphismthe ability.
CMSC 202, Version 3/02 1 Copy Constructors and Overloaded Assignment.
Object Management. Constructors –Compiler-generated –The Initializer List –Copy Constructors –Single-arg (conversion ctors) The Assignment Operator.
Inheritance OO Software Design and Construction Computer Science Dept Va Tech January 2002 ©2002 McQuain WD & Keller BJ 1 Generalization versus Abstraction.
Slide 1 Chapter 5 Arrays. Slide 2 Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays  Arrays in memory.
Chapter 5 Arrays. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 5-2 Learning Objectives  Introduction to Arrays  Declaring and referencing.
C++ Inheritance Data Structures & OO Development I 1 Computer Science Dept Va Tech June 2007 © McQuain Generalization versus Abstraction Abstraction:simplify.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
The This Pointer Programming in C++ Fall 2008 Dr. David A. Gaitros
CPS120: Introduction to Computer Science Lecture 16 Data Structures, OOP & Advanced Strings.
6. Iteration Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 1 Iteration pass (or iteration)-one.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
Objects with Functions and Arrays. Objects can be Passed Class defines type – Can use as type of function or parameter.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Polymorphism Data Structures & OO Development I 1 Computer Science Dept Va Tech May 2006 ©2006 McQuain & Ribbens Definition polymorphismthe ability to.
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Composition OO Software Design and Construction Computer Science Dept Va Tech January 2000 ©2000 McQuain WD 1 Composition: Cooperating Classes Composition.
Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 1 Pass-by-Value - default passing mechanism except.
Chapter 2 Objects and Classes
Pointer to an Object Can define a pointer to an object:
CS 2704 Object Oriented Software Design and Construction
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Dr. Bhargavi Dept of CS CHRIST
Managing 2D Arrays Simple Dynamic Allocation
Pointers and dynamic objects
SPL – PS3 C++ Classes.
Presentation transcript:

Communication OO Software Design and Construction Computer Science Dept Va Tech February 2001 ©2001 McQuain WD & Keller BJ 1 Communicating Objects Think of the “Borg”on Star Trek. Borg crew member= 1 object Borg Collective= composition of objects To achieve the purpose of the Collective, each Borg must continually communicate with other Borg. A Borg can be a “sender” or a “receiver”, and may play both roles at different times. Similarly, objects can be senders or receivers. Objects can also serve as messages! The resulting software system is viewed as a collection of collaborating objects. Collaboration requires communication…

Communication OO Software Design and Construction Computer Science Dept Va Tech February 2001 ©2001 McQuain WD & Keller BJ 2 Communication Involving Objects Kinds: by nameimplicit communication that can occur when one object is in a scope where its name is visible to other object by parameter passinga method of a class take an object as a parameter by return valuea method returns an object Parameters and return values allow two-way interaction Object may be communicated by: Copying Identity Reference Pointer May want to control whether receiver can modify the object, and if so, whether the sender sees any changes made by the receiver.

Communication OO Software Design and Construction Computer Science Dept Va Tech February 2001 ©2001 McQuain WD & Keller BJ 3 Inter-Object Communication By name: sender “knows the name” of the receiver and uses the name to access the public interface of the receiver. DisplayableNumber D(42, &cout); D.Show(); // The function accesses D by name, // passing the object cout by address, // Show()accesses cout by a pointer member void DisplayableNumber::Show() const { *Out << Count << endl; } The "name" may be the identifier associated with the object, or a pointer to the object.

Communication OO Software Design and Construction Computer Science Dept Va Tech February 2001 ©2001 McQuain WD & Keller BJ 4 Passing an Object as a Parameter An object may be passed as a function parameter: DisplayableNumber D(42, &cout); ofstream oFile(“output.text”); D.ShowIn(&oFile); // D receives oFile as a parameter void DisplayableNumber::ShowIn(ostream* setOut) { Out = setOut; // store address of oFile } As is always the case in C++, by default an object parameter is passed by value to the called function.

Communication OO Software Design and Construction Computer Science Dept Va Tech February 2001 ©2001 McQuain WD & Keller BJ 5 Returning an Object An object may be the return value from a function: typedef DisplayableNumber Item; // define an alias const int Digits = 10; Array LCD(Digits, Item(0, &cout)); // array of DNs... DisplayableNumber Digit4 = LCD.Retrieve(4); // shallow copy... Using an object as the return value provides a mechanism for encapsulating a body of related heterogeneous data. Item Array::Retrieve(int Idx) const { if (Idx >= Usage) return -1; else return List[Idx]; }

Communication OO Software Design and Construction Computer Science Dept Va Tech February 2001 ©2001 McQuain WD & Keller BJ 6 Different Ways to Communicate Is object communicated by: copying reference pointer Can the receiver modify the object? If the receiver does modify the object, does the sender see the changes? What language syntax is used in receiver to access (. or -> )?

Communication OO Software Design and Construction Computer Science Dept Va Tech February 2001 ©2001 McQuain WD & Keller BJ 7 Characteristics of Communicated Objects TechniqueCopiedChangeableVisibleC++ Access Syntax by copyyesyesno. by referencenoyesyes. by pointernoyesyes -> by const referencenonono. by pointer to constnonono -> By Copy: 4 Sender is “isolated” from changes by receiver 8 No good if sender/receiver want to share object 8 Bad if object is large (why?) By Identity (pointer or reference): 8 No isolation 4 Permits sharing of objects 4 Improves memory cost for large objects

Communication OO Software Design and Construction Computer Science Dept Va Tech February 2001 ©2001 McQuain WD & Keller BJ 8 Example: Person Class Person class represents basic characteristics of a person. A variety of object communications take place in the Person interface. enum Gender {MALE, FEMALE, GENDERUNKNOWN}; class Person { private: Name Nom; // sub-object Address Addr; // sub-object Person* Spouse; // association link Gender Gen; // simple data member public:... void changeAddress(const Address& newAddr); Address getAddress() const;... void setSpouse(Person* const Sp); Person* getSpouse() const;... };

Communication OO Software Design and Construction Computer Science Dept Va Tech February 2001 ©2001 McQuain WD & Keller BJ 9 Example: an Object as a Parameter The Person member changeAddr() receives an Address object as a parameter; pass by constant reference is used to avoid copying while safeguarding the actual parameter: void Person::changeAddr(const Address& newAddr) { Addr = newAddr; } The Person member setSpouse() receives a Person object as a parameter; pass by constant pointer is used to avoid copying while safeguarding the actual pointer (but not its target): void Person::setSpouse(Person* const Sp) { Spouse = Sp; }

Communication OO Software Design and Construction Computer Science Dept Va Tech February 2001 ©2001 McQuain WD & Keller BJ 10 Example: an Object as a return Value The Person member getAddress() returns a sub-object: Address Person::getAddress() const { return Addr; } As we've seen, one thing this allows is "chaining" of member function calls: Name JBHName("Joe", "Bob", "Hokie"); Address JBHAddr("Oak Bridge Apts", "#13", "Blacksburg", "Virginia", "24060"); Person JBH(JBHName, JBHAddr, MALE); cout << JBH.changeAddress().getZip() << endl;

Communication OO Software Design and Construction Computer Science Dept Va Tech February 2001 ©2001 McQuain WD & Keller BJ 11 Example: returning a Reference Changing the Person member setAddress() : Person& Person::setAddress(const Address& newAddr) { Addr = newAddr; return (*this); } Returning a reference to the "implicit" object allows carrying out multiple operations in a single statement: Address MovedTo("3221 Bob Petit Blvd", "Apt 6", "Baton Rouge", "Louisiana", "78703"); Person JT(...); JBH.setAddress(MovedTo).setSpouse(&JT); When the return value is a reference, no copying is performed.

Communication OO Software Design and Construction Computer Science Dept Va Tech February 2001 ©2001 McQuain WD & Keller BJ 12 Anonymous Objects An nameless (i.e., unnamed) object. Useful: -for temporary use (parameter in a method call, return, expression term) -as default value for an object parameter Anonymous objects are created by a direct invocation of a class constructor. There's an example of this in the Aggregation notes (slide C08.16). Anonymous objects are frequently used in conjunction with mutators and constructors when aggregation is involved, providing a cleaner interface to the aggregating class.

Communication OO Software Design and Construction Computer Science Dept Va Tech February 2001 ©2001 McQuain WD & Keller BJ 13 Example: Anonymous Objects as Parameters Without anonymous objects, we have a mild mess: Name JBHName("Joe", "Bob", "Hokie"); Address JBHAddr("Oak Bridge Apts", "#13", "Blacksburg", "Virginia", "24060"); Person JBH(JBHName, JBHAddr, MALE);... With anonymous objects we reduce pollution of the local namespace: Person JBH(Name("Joe", "Bob", "Hokie"), Address("Oak Bridge Apts", "#13", "Blacksburg", "Virginia", "24060"), MALE);...

Communication OO Software Design and Construction Computer Science Dept Va Tech February 2001 ©2001 McQuain WD & Keller BJ 14 Example: Anonymous Objects as Defaults Used as default parameter values, anonymous objects provide a relatively simple way to control initialization and reduce class interface clutter: Person::Person(Name N = Name("I", "M", "Nobody"), Address A = Address("No Street", "No Number", "No City", "No State", "00000"), Gender G = GENDERUNKNOWN) { Nom = N; Addr = A; Spouse = NULL; Gen = G; }