object oriented programming
DESCRIPTION
Universitatea de Vest din Timişoara Facultatea de Matematică şi Informatică. Object Oriented Programming. Lect. Dr. Daniel POP. Course #2 Agenda. Abstraction Abstraction definition. An example E ncapsulation: data and function members - PowerPoint PPT PresentationTRANSCRIPT
Object Oriented Programming
Lect. Dr. Daniel POP
Universitatea de Vest din Timişoara Facultatea de Matematică şi Informatică
2Programming II Object Oriented Programming
Course #2 Agenda
Abstraction Abstraction definition. An example Encapsulation: data and function members
• Basic object-oriented concepts: • Class• Object• Message
3Programming II Object Oriented Programming
AbstractionAbstraction: (real-life/human) Problem (computer/machine) Model Model– Data– Operations
DEFINITION [Abstraction] Abstraction means structuring a real-life problem into well-defined entities by defining their data and operations.
Remark: It’s a general process with no direct link with a programming language.
How to represent an abstraction: – using mechanism provided by a programming language (e.g. class in C++/Java)– using graphical representations offered by a modeling language (e.g. rectangle in UML)– using textual/non-standard description
4Programming II Object Oriented Programming
Example
Modeling employees of an institutionCandidate properties of an employee: name, address, date of birth, hair color, CNP etc.Employee (Textual representation)– Data
• Name• Address• Date of Birth• CNP
– Operations• CRUD: Create/Read/Update/Delete• Assign to a particular department• Access to data (name, address etc.)
5Programming II Object Oriented Programming
Example: Employee (UML representation)
Employee- name: String- address: String- CNP: String- DoB: Date
+ create(): Employee+ update(): Employee+ delete(Employee)+ assign(Department)
6Programming II Object Oriented Programming
Example: Employee (C++ representation)
Employee.h
class Employee { String name; String address; String CNP; Date DoB; Department department;
public: Employee create(); Employee update(); void delete(); void assign(Department& d);};
Employee.cpp
Employee Employee::create() { name = “”; address = “”; CNP=“0000000000000”; DoB.init(“1/1/1900”);}
Employee Employee::update() { // statements}
void Employee::delete() { department.remove(this);}
void Employee::assign( Department& d) { d.add(this); department = d;}
7Programming II Object Oriented Programming
Data type
DEFINITION [Data type] Data type = – possible values of the type– The way values are stored (internal representation in
memory)– operations applicable
Examples: predefined (built-in): int, float, double, char, void etc, or user-defined: Employee, complex etc.
8Programming II Object Oriented Programming
Encapsulation
DEFINITION [Encapsulation] The principle of hiding the data structures and to provide a well-defined, public interface to access them.
Example: representation of complex numbers
user.c
void f(complex c) { double d1 = c.re, d2 = c.im;}
complex.h
struct complex { double re, im;};
complex.h
struct complex { double v[2]; // v[0] – real part,
// v[1] – imaginary part};
user.c
void f(complex c) { // Error double d1 = c.re, d2 = c.im;}
9Programming II Object Oriented Programming
Basic object-oriented concepts: Class
DEFINITION [Class] A class is the implementation of a data type (concrete, abstract or generic). It defines attributes and functions(*) which implement the data structure and operations of the data type, respectively.Example:
complex.cpp
class complex { double v[2]; // v[0] – real part,
// v[1] – imaginary partpublic: double real() { return v[0]; } double imag() { return v[1]; }};
(*) Remark: Functions are also called methods. In [Stroustrup, 1997] a clear distinction is made between functions and methods. A method is a virtual function. This terminology will be followed throughout this course as well.
Remark: Classes define properties and behaviour of sets of objects.
10Programming II Object Oriented Programming
Basic object-oriented concepts: ObjectDEFINITION [Object] An object is an instance of a class. It can be uniquely identified by its name, it defines a state which is represented by the values of its attributes at a particular time and it exposes a behaviour defined by the set of functions (methods) that can be applied to it.
An OO Program = collection of objects that interact one with another.
Example:void f() { complex a = 2.3; complex b = 1/a; complex c = a+b*complex(1,2.3); c = -(a/b)+2; c.print();}
How are objects internally represented in C++?
Object3 Object1
Object4
Object2
11Programming II Object Oriented Programming
Basic object-oriented concepts: MessageQ: How does objects interact? This is the question ;)
A: By sending messages from one object to another asking the recipient to apply a method on itself.
DEFINITION [Message] A message is a request to an object to invoke one of its functions. A message contains:
– the name of the function;– the arguments of the function.
Sending a message is achieved in C++ by: ‘.’ or ‘->’ or any other overloadable operator
void f() { complex c = a+b*complex(1,2.3); // or, equivalent to c.set(a.add(b.multiply(complex(1,2.3))));}
How objects interact in C++?
void container::draw(RenderingDevice rd) { for(int i=0; i < count; i++) shapes[i].draw(rd);}
12Programming II Object Oriented Programming
Exercise
Choose one of your daily tasks (not too complex) and describe it in procedural and object-oriented forms. Try to design it as a world of interacting objects.
13Programming II Object Oriented Programming
Further Reading
[Bruce, 2002] Kim B. Bruce – Foundations of Object-Oriented Languages, MIT Press, 2002 [Section 1.1, Page 4-7]
[Mueller, 1996] Peter Müller – Introduction to Object-Oriented Programming Using C++, Globewide Network Academy (GNA) www.gnacademy.org, 1996 [Chapter 3-4]
[Stroustrup, 1999] Bjarne Stroustrup - An Overview of the C++ Programming Language in “The Handbook of Object Technology” (Editor: Saba Zamir). CRC Press LLC, Boca Raton. 1999. ISBN 0-8493-3135-8 [Section 4.2, Page 10-11]
[Goguen, 1978] J.A. Goguen, J.W. Thatcher, and E.G. Wagner - An initial algebra approach to the specification, correctness, and implementation of abstract data types. In R.T. Yeh, editor, Current Trends in Programming Methodology, volume 4. Prentice-Hall, 1978
[Guttag, 1977] J.V. Guttag - Abstract data types and the development of data structures. Communications of ACM, 20(6):396–404, 1977
[Pop, 2003] Daniel Pop – C++ Programming Language Lecture Notes
[Stroustrup, 1997] Bjarne Stroustrup – The C++ Programming Language 3rd Edition, Addison Wesley, 1997 [Section 4.9.4]
14Programming II Object Oriented Programming
Backup Slides
15Programming II Object Oriented Programming
Abstract Data Types
Introduced by Goguen 1978 and Guttag 1977 [Goguen 1978, Guttag 1977] DEFINITION [Abstract Data Type] An ADT is characterized by the following properties:– It exports a type;– It exports a set of operations (the interface) that are the one and only
access mechanism to the type’s data structure;– Axioms and preconditions define the application domain of the type.
Multiple instances of an ADTHow to represent an ADT:
– using mechanism provided by a programming language (e.g. class in C++/Java)– using graphical representations offered by a modeling language (e.g. rectangle in UML)– using textual/non-standard description (e.g. see Employee on previous slide)
16Programming II Object Oriented Programming
Generic Abstract Data Types
List of cars, facts, events, students etc.Additional parameter that specifies the type of objects stored in a list => generic parameter specified when a new variable is created. Ex: List<Student> anYear;List is a generic abstract data type
template <class T> class List {T *v ;int max_size, top;
public:List (int s) { top = 0 ; v = new T[max_size = s]; } // constructor˜List() { delete [] v ; } // destructorvoid insert (T c ) { v[top++] = c ; }T get(int index) { return v[index]; }
};
17Programming II Object Oriented Programming
Abstract data types and object-oriented programming
Data abstraction Object-oriented programming
Abstract data type (abstract) classGeneric abstract data type Template classInstance of an (G)ADT ObjectOperations Member function (of a class)Attributes Data members
Object oriented programming is one possible “implementation” of ADT