Object-Oriented Programmingprogramming

Object-Oriented Programmingprogramming

Object-OrientedObject-Oriented ProgrammingProgramming Lecture 1 Dr Piotr Cybula <[email protected]> LiteratureLiterature Bruce Eckel: Thinking in C++. Second edition. Vol. 1. (http://mindview.net/Books/TICPP/ThinkingInCPP2e.html) Robert Lafore: Object-Oriented Programming in C++. Nicolai Josuttis: Object-Oriented Programming in C++. Stanley Lippman, Josée Lajoie, Barbara Moo: C++ Primer. Forth edition. Bjarne Stroustrup: Language C++. Stephen Prata: C++ Primer Plus. Michael Ben-Ari: Understanding programming languages. http://cppreference.com http://www.cplusplus.com 2/16 Object-Oriented Programming, Lecture 1, dr Piotr Cybula, University of Lodz ProgrammingProgramming paradigmsparadigms ● declarative programming (tell the computer what do you want) ● imperative programming (tell the computer how to do it) ● procedural programming (bottom-up design): ● global data structures with free access (risky) ● many loose functions building a program ● object programming (top-down design): ● protected & separated data structures (black-box solution) ● functions operating on small parts of data (encapsulation) ● generic programming (static polymorphism) ● object-oriented programming (object programming + dynamic polymorphism) ● modular programming, event programming, functional programming, ... 3/16 Object-Oriented Programming, Lecture 1, dr Piotr Cybula, University of Lodz Object-orientedObject-oriented paradigmparadigm Four fundamentals of the object-oriented programming (OOP) : ● Abstraction (abstract data types, ADTs): ● classes as the abstract and simplified model of real beings (structural and behavioral), objects as instances of classes ● Encapsulation (information enclosing and hiding): ● functions tied together with data (methods) ● data protection (public methods, sending messages, object interface) ● Inheritance (code reuse, interface extensions): ● building specialised objects based on general ones ● Polymorphism (object-orientedness): ● single abstract interface – multiple different forms of inherited objects 4/16 Object-Oriented Programming, Lecture 1, dr Piotr Cybula, University of Lodz AdvantagesAdvantages ofof object-orientednessobject-orientedness ● structural and behavioral design similar to the human's thinking of the reality ● increased code safety and reuse (data protection and inheritance) ● a code is easier to write and (the most important) it's a lot easier to read ● clear modular code organisation (encapsulation) ● the encapsulation prevents function name clashes ● the encapsulation prevents from «giving a monkey a razor» ● better team support with the code separation ● proper initialisation and cleanup (constructors, destructors) ● abstract, perspective and dynamic programming (late binding, polymorphism) ● better code stability by the exception handling (exception inheritance) 5/16 Object-Oriented Programming, Lecture 1, dr Piotr Cybula, University of Lodz HistoryHistory ofof object-orientednessobject-orientedness ● 1960s – Simula 67 : classes, static instances, ship simulations ● 1971 – Smalltalk : dynamic objects created «on-the-fly», object-orientedness ● 1983 – C++ : the extension of the procedural language C ● 1980s : extensions of other existing procedural languages (Ada, Fortran, Pascal , Basic, Eiffel, Lisp, Perl, OCaml) ● 1991 – Java : C++ based language for virtual machine with garbage collector (cross-platform) ● 1990s : primarily object-oriented, interpreted, dynamic-typed languages (Python, Ruby), object-oriented scripting languages (JavaScript, PHP) ● 2002 - .NET framework : cross-language inheritance (C#, VB.NET, J#) ● 2000s : newer object-oriented languages (Swift, Kotlin, TypeScript, ...) ● 2020 – C++20 : the newest standard of the C++ language 6/16 Object-Oriented Programming, Lecture 1, dr Piotr Cybula, University of Lodz ClassesClasses andand objectsobjects Structural approach (without the encapsulation): struct Obj //static data structure { int a, b; }; void set(Obj &o, int _a, int _b) //global function(& reference) { o.a = _a; o.b = _b; } Obj x; //variable set(x, 1, 2); //function call 7/16 Object-Oriented Programming, Lecture 1, dr Piotr Cybula, University of Lodz ClassesClasses andand objectsobjects Object approach (the encapsulation): struct Obj //abstract data type (the class) { int a, b; //fields void set(int _a, int _b) //method { a = _a; b = _b; } }; Obj x; //the object (an instance of the class) x.set(1, 2); //messege to the object 8/16 Object-Oriented Programming, Lecture 1, dr Piotr Cybula, University of Lodz InterfaceInterface andand implementationimplementation File obj.h (abstract type interface): struct Obj //abstract data type (the class) { int a, b; //fields void set(int _a, int _b); //method prototype }; File obj.cpp (abstract type implementation): #include ”obj.h” void Obj::set(int _a, int _b) //method body (:: scope operator) { a = _a; b = _b; } File main.cpp: #include ”obj.h” int main() { Obj x; //the object (an instance of the class) x.set(1, 2); //messege to the object } 9/16 Object-Oriented Programming, Lecture 1, dr Piotr Cybula, University of Lodz ObjectObject constructionconstruction ● the constructor - the special method for proper initialisation of the fields ● called automatically on every object creation ● the name of the constructor is the same as the name of the type (class) ● many constructors with arbitrary number of arguments, no return value! struct Obj { int a, b; //fields Obj(int _a = 0, int _b = 0) //constructor { a = _a; b = _b; } }; Obj x, y(1), z(1, 2), v = 3; //the implicit constructor calls Obj t[5], *s = new Obj, *p = new Obj[3]; //also here 10/16 Object-Oriented Programming, Lecture 1, dr Piotr Cybula, University of Lodz DefaultDefault constructorconstructor Default constructor: ● which may be called without arguments (any arguments with default values) ● automatically created if (and only if) there are no explicit constructors, but without any initialisation of the fields (risky when there are any dynamically allocated fields in the class) struct Obj { int a, b; //fields Obj() //default constructor { a = 0; b = 0; } }; Obj x, t[5]; //default constructor calls Obj *s = new Obj, *p = new Obj[3]; //also here 11/16 Object-Oriented Programming, Lecture 1, dr Piotr Cybula, University of Lodz DefaultDefault constructorconstructor Default constructor: ● if any constructors are defined, the default constructor is not created automatically ● important – always define the default constructor (to simplify future inhertitance from the class) struct Obj { int a, b; //fields Obj(int _a, int _b = 0) //1- or 2-parameter constructor { a = 0; b = 0; } }; Obj y(1), z(1, 2); //proper creation Obj x; //compile-time error without the default constructor 12/16 Object-Oriented Programming, Lecture 1, dr Piotr Cybula, University of Lodz CopyCopy constructorconstructor Copy constructor: ● takes as the only argument a reference to an existing object of the same type ● automatically created if (and only if) there is no explicit copy constructor, but copying the fields by simple bit-copy (risky when there are any pointer fields) ● important – always define the copy constructor for a class with pointer fields struct Obj { int a, b; //fields … //default constructor here Obj(const Obj &o) //copy constructor { a = o.a; b = o.b; } }; Obj x; Obj y(x), z = x; //the implicit copy constructor calls 13/16 Object-Oriented Programming, Lecture 1, dr Piotr Cybula, University of Lodz ConvertingConverting constructorconstructor Converting constructor: ● takes as the only argument a value of another type (converts this type to the type of a class) struct Obj { int a, b; //fields … //default constructor here Obj(int v) //converting constructor (int → Obj) { a = b = v; } bool equals(const Obj &o) //method with object argument { return (a == o.a) && (b == o.b); } }; Obj y(1), z = 2; //the implicit converting constructor calls if (y.equals(5)) ... //also here 14/16 Object-Oriented Programming, Lecture 1, dr Piotr Cybula, University of Lodz ObjectObject destructiondestruction ● the destructor - the special method for proper cleanup ● called automatically when the object goes out of scope and on explicit deletion ● the name of the destructor is the same as the name of the type (class) but with leading tilde (~) ● only one destructor without arguments, no return value! struct Obj { int a, b; //fields … //default constructor here ~Obj() //destructor with any cleaning operations { ... } }; { Obj x, *p = new Obj; //constructor calls delete p; //the explicit destructor call (object *p) } //the implicit destructor call (object x) 15/16 Object-Oriented Programming, Lecture 1, dr Piotr Cybula, University of Lodz ObjectObject destructiondestruction The destructor: ● automatically created if (and only if) there is no explicit destructor, but without any cleanup operations (risky when there are any dynamically allocated fields in the class) ● important – always define the destructor for a class with the constructor dynamically allocating its fields ● destructors for objects are called by the compiler in the reverse order of the object creation order { Obj x, *p = new Obj, z; { Obj y; } //y destructed delete p; //*p destructed } //z and then x destructed 16/16 Object-Oriented Programming, Lecture 1, dr Piotr Cybula, University of Lodz.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    16 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us