OBJECT ORIENTED PROGRAMMING. Design principles for organizing code into user-defined types Principles include: Encapsulation Inheritance Polymorphism.

Slides:



Advertisements
Similar presentations
Understand and appreciate Object Oriented Programming (OOP) Objects are self-contained modules or subroutines that contain data as well as the functions.
Advertisements

CPA: C++ Polymorphism Copyright © 2007 Mohamed Iqbal Pallipurath Overview of C++ Polymorphism Two main kinds of types in C++: native and user-defined –User-defined.
C++ Review. User Defined Types Built-in data types are simple. Specific problems may demand aggregate/more complex data types. – Ex: polygons, matrices,
Object-Oriented PHP (1)
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
OOP Spring 2007 – Recitation 71 Object Oriented Programming Spring 2006 Recitation 8.
OOP Etgar 2008 – Recitation 71 Object Oriented Programming Etgar 2008 Recitation 7.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Abstraction: Polymorphism, pt. 1 Abstracting Objects.
UFCEUS-20-2 : Web Programming Lecture 5 : Object Oriented PHP (1)
Programming Languages and Paradigms Object-Oriented Programming.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
1 Virtual Functions and Polymorphism Chapter What You Will Learn What is polymorphism? How to declare and use virtual functions for abstract classes.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Virtual Functions Polymorphism Abstract base classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
C++ Review Classes and Object Oriented Programming Parasol Lab, Texas A&M University.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
CSCI 383 Object-Oriented Programming & Design Lecture 17 Martin van Bommel.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Abstraction ADTs, Information Hiding and Encapsulation.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Object-Oriented Programming Chapter Chapter
1 OOP - An Introduction ISQS 6337 John R. Durrett.
© 2007 Lawrenceville Press Slide 1 Chapter 8 Objects  A variable of a data type that is a class. Also called an instance of a class.  Stores data  Can.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
CPS Inheritance and the Yahtzee program l In version of Yahtzee given previously, scorecard.h held information about every score-card entry, e.g.,
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.
Csi2172 class 5 Midterm: June 12. constructor Special method used to create objects of the class Never has a return type. Is called automatically upon.
Principles of programming languages 10: Object oriented languages Isao Sasano Department of Information Science and Engineering.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Overview of C++ Polymorphism
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Principles of programming languages 10: Object oriented languages
Object-Oriented Programming Concepts
Classes (Part 1) Lecture 3
Classes C++ representation of an object
Inheritance and Polymorphism
Object-Oriented Programming
Inheritance and Run time Polymorphism
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Object-Oriented Programming
Polymorphism Polymorphism
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Object-Oriented Programming
Object-Oriented Programming
Overview of C++ Polymorphism
CIS 199 Final Review.
Classes C++ representation of an object
Object-Oriented PHP (1)
Dr. R Z Khan Handout-3 Classes
Lecture 10 Concepts of Programming Languages
Jim Fawcett CSE687 – Object Oriented Design Spring 2014
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Presentation transcript:

OBJECT ORIENTED PROGRAMMING

Design principles for organizing code into user-defined types Principles include: Encapsulation Inheritance Polymorphism

USER DEFINED TYPES When solving problems often times it becomes easier for programmers to express algorithms in terms of real world objects or complex types Ex. vectors, matrices, polygons, etc Ex. Cars, planes, and trains Types define both the internal data and operations which can be done on the data Ex. A vector might hold an x and a y data and define operations such vector addition and subtraction C++ allows for complex types in the form of classes and structs

CLASS EXAMPLE Can use struct or class to define a new type struct Point { float m_x, m_y; void Set(float x, float y){ m_x = x; m_y = y; } }; class Point { float m_x, m_y; public: Point(float x, float y) : m_x(x), m_y(y) {} void Set(float x, float y){ m_x = x; m_y = y; } };

ENCAPSULATION A construct that facilitates the bundling of data with the methods (or other functions) operating on that data A mechanism for restricting access to some of the object's components Advise: Helper functions and data should be “Encapsulated”, i.e., should be private or protected Use Accessors and Modifiers to “Get” and “Set” the data Bundle operations on an object inside the class, not as utility functions

CONSTRUCTORS AND DESTRUCTORS Constructors – “functions” which define how data is initialized Same name as the class itself Default constructor – takes no parameters, e.g., Point() Copy constructor – takes an object of the class itself to initialize the data, e.g., Point(oldPoint) Destructors – define how data is deleted when an object goes out of scope Same name as the class itself except with a ~, e.g., ~MyClass() Usually not called in code you write. The compiler automatically calls it when an object goes out of scope.

PUBLIC, PROTECTED, AND PRIVATE SCOPES Public: functions and data which any part of a program may access Ex. Data Accessors – GetData(), modifiers – SetData(newData), operations – ActionX() Protected: functions and data which only the class and any derived subclasses may access Ex. Helper functions for operations. Most data in class hierarchies Private: functions and data which only the class itself may access Ex. Helper functions and some data By default: classes have private scope while structs have public scope

QUICK NOTE ON “STATIC” Static data and functions are functions of a type which are not specific to an instance. Examples Intrinsic type properties – value of PI, shared data of classes (container of all instances) Functions on those properties

INHERITANCE Inheritance is a construct allowing reuse of code in existing objects and definition of subtypes Gives rise to class hierarchies Base class: class which is inherited from, e.g., Shape Derived class: class which does the inheriting, e.g., Circle By inheriting, derived classes have access to public and protected data and functions of the base class

ABSTRACT TYPES AND HIERARCHIES Often many types have things in common, e.g., all shapes can be drawn or all shapes have color However, you cannot concretely define a shape. In this case a shape is abstract We derive more concrete classes off of abstract ones, e.g., a circle is a concrete shape Shape CircleSquareTriangle

INHERITANCE EXAMPLE class Shape{ protected: Color m_c; public: Shape(Color c) : m_c(c) {} void Draw(){ //do nothing} }; class Circle : public Shape { float m_radius; public: Circle(Color c, float r) : Shape(c), m_radius(r) {} void Draw() { //draw circle of radius r } }; class Square : public Shape { float m_side; public: Square(Color c, float s) : Shape(c), m_side(s) {} void Draw() { //draw circle of radius r } }; Notice: How to inherit a class How to call the base class constructor Data is inherited

POLYMORPHISM Defining and constructing data and algorithms in terms of abstract classes At runtime the specific type of the class is decided and its behavior is executed Abstract classes provide an interface to express algorithmic use, whereas derived classes provide the concrete implementation Abstract classes use “virtual” functions to ensure subtypes implement the correct interface

ABSTRACT CLASSES Abstract classes describe an interface “virtual” is a keyword used to allow subtypes to overload the function in the manner they see fit “pure virtual” functions MUST be overloaded in the base class class Shape { virtual void DrawNotPureVirtual(){ //do nothing } virtual void DrawPureVirtual() = 0; }; class Circle{ //virtual void DrawNotPureVirtual() //does not need to be defined. By //inheritance it will do nothing virtual void DrawPureVirtual() { //draw my circle } };

POLYMORPHISM IN ALGORITHMS As stated before polymorphism allows algorithms to be expressed in terms of abstract classes Utilizes pointers (discussed in future review in more depth) and virtual lookup at runtimevirtual lookup at runtime At runtime the concrete type’s implementation is used void DrawAllShapes(Shape** shapes, int numShapes){ for(int i = 0; i<numShapes; i++){ //virtual function call. //Lookup of concrete //implementation occurs at //runtime shapes[i]->Draw(); }