Object Oriented Programming Lect. Dr. Daniel POP Universitatea de Vest din Timişoara Facultatea de Matematică şi Informatică.

Slides:



Advertisements
Similar presentations
Chapter 8 Technicalities: Functions, etc. Bjarne Stroustrup
Advertisements

1 Classes and Objects in Java Basics of Classes in Java.
Final and Abstract Classes
Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Learning Objectives Structures Structure types Structures.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide STARTING OUT WITH Visual Basic 2008 FOURTH EDITION Tony Gaddis.
Classes, Exceptions, Collections, and Scrollable Controls
Chapter 11 Separate Compilation and Namespaces. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Separate Compilation.
Chapter 14 Inheritance. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Inheritance Basics Derived classes, with.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Object Oriented Programming with Java
Systems Analysis and Design with UML Version 2.0, Second Edition
Object Oriented Programming Lect. Dr. Daniel POP Universitatea de Vest din Timişoara Facultatea de Matematică şi Informatică.
Chapter 11 Component-Level Design
Chapter 13 – Introduction to Classes
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Lesson 13 Introduction to Classes CS1 Lesson Introduction to Classes1.
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Object Oriented Programming Lect. Dr. Daniel POP Universitatea de Vest din Timişoara Facultatea de Matematică şi Informatică.
Object Oriented Programming Lect. Dr. Daniel POP Universitatea de Vest din Timişoara Facultatea de Matematică şi Informatică.
Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
Department of Computer Engineering Faculty of Engineering, Prince of Songkla University 1 5 – Abstract Data Types.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Lecture 9 Concepts of Programming Languages
C++ fundamentals.
Classes in C++ Bryce Boe 2012/08/15 CS32, Summer 2012 B.
Object Oriented Software Development
Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Classes Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd Spetember 2006.
Course: Object Oriented Programming - Abstract Data Types Unit1: IntroductionSlide Number 1 Introduction Course: Object Oriented Programming Abstract Data.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
Object Oriented Programming Lect. Dr. Daniel POP Universitatea de Vest din Timişoara Facultatea de Matematică şi Informatică.
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.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Object Oriented Programming Lect. Dr. Daniel POP Universitatea de Vest din Timişoara Facultatea de Matematică şi Informatică.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Abstraction ADTs, Information Hiding and Encapsulation.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
1 OOP - An Introduction ISQS 6337 John R. Durrett.
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.
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.
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Introduction to Classes in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
1 Introduction to Object Oriented Programming Chapter 10.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Review: Two Programming Paradigms
About the Presentations
Introduction to Classes
Chapter 5 Classes.
Lecture 9 Concepts of Programming Languages
Introduction to Classes
CIS601: Object-Oriented Programming in C++
Programming Languages and Paradigms
(4 – 2) Introduction to Classes in C++
Lecture 9 Concepts of Programming Languages
Presentation transcript:

Object Oriented Programming Lect. Dr. Daniel POP Universitatea de Vest din Timişoara Facultatea de Matematică şi Informatică

2Programming IIObject Oriented Programming Course #2 Agenda Abstraction Abstraction definition. An example Encapsulation: data and function members Abstract Data Types Generic Abstract Data Types Abstract data types and object-oriented programming Basic object-oriented concepts: class, object, message Classes in C++ class declaration and object instantiation accessing class members namespaces and scopes

3Programming IIObject Oriented Programming Abstraction Abstraction: (real-life) Problem Model Model –Data –Operations DEFINITION [Abstraction] Abstraction means structuring a real-life problem into well-defined entities by defining their data and operations. Remark: Its a general process with no direct link with a programming language.

4Programming IIObject Oriented Programming Example Modeling employees of an institution Candidate properties of an employee: name, address, date of birth, hair color, CNP etc. Employee –Data Name Address Date of Birth CNP –Operations Create/Destroy Modify existing data (name, address) Assign to a particular department Access to data (name, address etc.)

5Programming IIObject 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; }

6Programming IIObject 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 types data structure; – Axioms and preconditions define the application domain of the type. Multiple instances of an ADT How 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)

7Programming IIObject Oriented Programming Example: complex (UML representation) complex + real(): double + imag(): double

8Programming IIObject Oriented Programming Example: complex (C++ representation) user.c void f(complex c) { double d1 = c.real(), d2 = c.imag(); } complex1.cpp class complex { double v[2]; // v[0] – real part, // v[1] – imaginary part public: double real() { return v[0]; } double imag() { return v[1]; } }; complex2.cpp class complex { double re, im; public: double real() { return re; } double imag() { return im; } };

9Programming IIObject 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 anYear; List is a generic abstract data type template class List { T *v ; int max_size, top; public: List (int s) { top = 0 ; v = new T [max_size = s]; } // constructor ˜List() { delete [] v ; } // destructor void insert ( T c ) { v[top++] = c ; } T get(int index) { return v[index]; } };

10Programming IIObject Oriented Programming Abstract data types and object-oriented programming Data abstractionObject-oriented programming Abstract data type(abstract) class Generic abstract data typeTemplate class Instance of an (G)ADTObject OperationsMember function (of a class) AttributesData members Object oriented programming is one possible implementation of ADT

11Programming IIObject 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 part public: 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.

12Programming IIObject Oriented Programming Basic object-oriented concepts: Object DEFINITION [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++?

13Programming IIObject Oriented Programming Basic object-oriented concepts: Message Q: 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); }

14Programming IIObject 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.

15Programming IIObject Oriented Programming Classes in C++ Classes are defined in C++ using struct or class keywords. Syntax: class X { // member variables // member functions }; struct X { // member variables // member functions }; Remark: In case of using struct, by default, all members are public. For class, they are private by default. The programmer is free to choose which keyword he/she prefers, with a recommendation to use struct for data types that doesnt provide behaviour. struct Date { int day, month, year; // Date initialization void init(int day, int month, int year); }; Example : X – generic name for a class

16Programming IIObject Oriented Programming Accessing class members Access to members of a class – the same syntax as for struct in C, i.e. using. or -> operators. One need to have an object through which members are incarnated and accessed (of course, there are some exceptions… but were saving them for later) Syntax: Object.member; PObject->member; void f() { Date today; Date *pdate = &today; today.init(7, 3, 2007); // March 7 th, 2007 printf(Today is %d/%d/%d., pdate->day, pdate->month, today.year); } Example :

17Programming IIObject Oriented Programming Accessing class members New operators in C++:.* and ->* // pointer to int data member of Date typedef int Date::*PM; // pointer to member function of Date taking 3 arguments of type int typedef void (Date::*PMF)(int, int, int); void f() { Date today; Date *pdate = &today; PM pm = &Date::day; PMF pmf = &Date::init; (today.*pmf)(7, 3, 2007); // today.init(7, 3, 2007); today.*pm = 8; // today.day = 8 pm = &Date::month; today.*pm = 8; // today.month = 8 (pdate->*pmf) (7, 3, 2007); // pdate->init(7, 3, 2007); pdate->*pm = 8; // pdate->month = 8 }

18Programming IIObject Oriented Programming Scopes DEFINITION [Scope] A scope is a specific part of the source code. Example: any block (delimited by { and }) defines a scope; each function defines a scope. Each class defines a new scope. The name of the scope is the same as class name. A name is defined in the global scope if it is defined outside any function, class or namespace. It is visible from the point of declaration up to the end of the file. Every declaration (variable of functions) introduces a name in a scope => this name is visible only in that scope and hides names declared in enclosing scopes or at global scope. The scope resolution operator (::) allows to access an identifier (name) defined in another namespace than the current one. Syntax: ScopeName :: Identifier int x; // global x void f() { int x; // local x hides the global x x = 1; // local x = 1 ::x = 2; // global x = 2 } int* px = &x; // address of global x Example: [Stroustrup, 1997 – Section 4.9.4] int x = 0; void f() { int y =x; int x = 1; y = x; } What happens here?

19Programming IIObject Oriented Programming Implementing member functions A member function is handled as any other ordinary C function. A member function is a name introduced in the scope defined by its parent class. Therefore, to access this name outside its scope of definition, we need to fully qualify the name using the scope resolution operator and the class name (namespace). void Date::init (int _day, int _month, int _year) { day = _day; month = _month; year = _year; } Example : This indicates that init is the identifier from Date class/namespace and not defined at global scope!

20Programming IIObject Oriented Programming Further Reading [Bruce, 2002] Kim B. Bruce – Foundations of Object-Oriented Languages, MIT Press, 2002 [Section 1.1, Page 4-7] [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) [Chapter 3-4] [Mueller, 1996] Peter Müller – Introduction to Object-Oriented Programming Using C++, Globewide Network Academy (GNA) [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 ISBN [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, Chapter 10][Stroustrup, 1997] Bjarne Stroustrup – The C++ Programming Language 3rd Edition, Addison Wesley, 1997 [Section 4.9.4, Chapter 10]