OBJECT ORIENTED PROGRAMMING

Similar documents
CS250 Intro to CS II. Spring CS250 - Intro to CS II 1

C++ Inheritance and Encapsulation

Java Object Oriented Design. CSC207 Fall 2014

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Programming, numerics and optimization

More C++ : Vectors, Classes, Inheritance, Templates

Chapter 6 Introduction to Defining Classes

More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com

Data Abstraction. Hwansoo Han

OBJECT ORİENTATİON ENCAPSULATİON

Lecture 5: Inheritance

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

Object Oriented Programming. Solved MCQs - Part 2

CMSC 132: Object-Oriented Programming II

Inheritance. OOP components. Another Example. Is a Vs Has a. Virtual Destructor rule. Virtual Functions 4/13/2017

CS250 Final Review Questions

C++ Programming Classes. Michael Griffiths Corporate Information and Computing Services The University of Sheffield

Inheritance, and Polymorphism.

Data Structures (list, dictionary, tuples, sets, strings)

ECE 3574: Dynamic Polymorphism using Inheritance

CSE 307: Principles of Programming Languages

OOP THROUGH C++(R16) int *x; float *f; char *c;

CS 162, Lecture 25: Exam II Review. 30 May 2018

C++ Inheritance and Encapsulation

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

Module 10 Inheritance, Virtual Functions, and Polymorphism

L4: Inheritance. Inheritance. Chapter 8 and 10 of Budd.

Object Oriented Paradigm

CS304 Object Oriented Programming Final Term

Data Structures and Other Objects Using C++

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

Inheritance and Polymorphism

C++ 8. Constructors and Destructors

C++ (Non for C Programmer) (BT307) 40 Hours

MaanavaN.Com CS1203 OBJECT ORIENTED PROGRAMMING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

What is Polymorphism? Quotes from Deitel & Deitel s. Why polymorphism? How? How? Polymorphism Part 1

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

Babaria Institute of Technology Computer Science and Engineering Department Practical List of Object Oriented Programming with C

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides

Chapter 10 Classes Continued. Fundamentals of Java

Polymorphism Part 1 1

Polymorphism. Miri Ben-Nissan (Kopel) Miri Kopel, Bar-Ilan University

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

Object Oriented Software Design II

Object Oriented Software Design II

Use the template below and fill in the areas in Red to complete it.

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

C++ (classes) Hwansoo Han

G Programming Languages - Fall 2012

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

EL2310 Scientific Programming

CS250 Final Review Questions

Java Primer. CITS2200 Data Structures and Algorithms. Topic 2

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

Extending Classes (contd.) (Chapter 15) Questions:

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

Introduction To C#.NET

CLASSES AND OBJECTS IN JAVA

(12-1) OOP: Polymorphism in C++ D & D Chapter 12. Instructor - Andrew S. O Fallon CptS 122 (April 3, 2019) Washington State University

(SSOL) Simple Shape Oriented Language

Introduction to Design Patterns

Inheritance, Polymorphism, and Interfaces

VALLIAMMAI ENGINEERING COLLEGE

Type Analysis. Type Checking vs. Type Inference

G52CPP C++ Programming Lecture 13

Chapter 14 Abstract Classes and Interfaces

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1

index.pdf January 21,

COMS W3101 Programming Language: C++ (Fall 2016) Ramana Isukapalli

Object-oriented Programming. Object-oriented Programming

CST242 Object-Oriented Programming Page 1

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

Object Orientated Analysis and Design. Benjamin Kenwright

ITI Introduction to Computing II

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli


Ch02. True/False Indicate whether the statement is true or false.

ITI Introduction to Computing II

Object Oriented Programming: Inheritance Polymorphism

Introducing Java. Introducing Java. Abstract Classes, Interfaces and Enhancement of Polymorphism. Abstract Classes

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

A506 / C201 Computer Programming II Placement Exam Sample Questions. For each of the following, choose the most appropriate answer (2pts each).

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

CMSC 132: Object-Oriented Programming II

COP 3530 Discussion Session #2 Ferhat Ay

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

VIRTUAL FUNCTIONS Chapter 10

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017

Midterm Exam 5 April 20, 2015

OBJECT ORIENTED PROGRAMMING

Chapter 15: Inheritance, Polymorphism, and Virtual Functions

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

Chapter 5 Object-Oriented Programming

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

Public Class ClassName (naming: CPoint)

Transcription:

OBJECT ORIENTED PROGRAMMING

OBJECT ORIENTED PROGRAMMING Design principles for organizing code into user-defined types Principles include: Encapsulation Inheritance Polymorphism http://en.wikipedia.org/wiki/encapsulation_(object-oriented_programming)

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 Shape 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 Circle Square Triangle

INHERITANCE EXAMPLE class Shape{ ; protected: Color m_c; public: Shape(Color c) : m_c(c) { void Draw(){ //do nothing Notice: How to inherit a class How to call the base class constructor Data is inherited 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

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 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();