Object-Oriented Programming

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Object Oriented Programming
Object-Oriented PHP (1)
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Classes and Objects Systems Programming.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Classes in C++ Bryce Boe 2012/08/15 CS32, Summer 2012 B.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
An Object-Oriented Approach to Programming Logic and Design
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to 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.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
11/28/2015B.Ramamurthy1 Object-Oriented Design and Java B.Ramamurthy.
Chapter 10 Classes and Objects In-Depth. Chapter 10 A class provides the foundation for creating specific objects, each of which shares the general attributes,
Programming Paradigms Lecturer Hamza Azeem. What is PP ? Revision of Programming concepts learned in CPLB Learning how to perform “Object-Oriented Programming”
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Object Oriented Programming
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Object-Oriented Design Bina Ramamurthy SUNY at Buffalo.
An Introduction to Programming with C++ Fifth Edition Chapter 14 Classes and Objects.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Introduction to Classes and Objects CS-2303, C-Term C++ Program Structure Typical C++ Programs consist of:– main –A function main –One or more classes.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Introduction to Object-oriented Programming
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Classes (Part 1) Lecture 3
Chapter 13: Overloading and Templates
Analysis and Design with UML: Discovering Classes and Relationships
Review: Two Programming Paradigms
About the Presentations
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Analysis and Design with UML: Discovering Classes and Relationships
Object-Oriented Programming Using C++
Introduction to Object-oriented Program Design
Inheritance B.Ramamurthy 11/7/2018 B.Ramamurthy.
CS212: Object Oriented Analysis and Design
Analysis and Design with UML: Discovering Classes and Relationships
Defining Classes and Methods
Object-oriented Design in Processing
Object-Oriented Programming
Introduction to Classes and Objects
Object-oriented Design in Processing
Defining Classes and Methods
Problem Solving, Object-Oriented Design and Java
UNIT I OBJECT ORIENTED PROGRAMMING FUNDAMENTALS
Analysis and Design with UML: Classes and Relationships
COP 3330 Object-oriented Programming in C++
Fundaments of Game Design
Object-Oriented Programming Using C++
Object-Oriented Programming
CIS 199 Final Review.
Defining Classes and Methods
Defining Classes and Methods
Object-oriented Design in Processing
Review of Previous Lesson
Final and Abstract Classes
Extending Classes Through Inheritance
Object-oriented Design in Processing
Classes and Objects Systems Programming.
From Class Diagram to Contract Diagram
Introduction to Classes and Objects
Presentation transcript:

Object-Oriented Programming B.Ramamurthy 1/12/2019 Ramamurthy

Topics to be covered Why OO? OO concepts sampler Notion of class From problem to OO solution Access Specifiers Components of OO Design Files in Development Summary 1/12/2019 Ramamurthy

Object-Oriented Principles OOP Inheritance -- Hierarchy -- Reusability -- Extensibility -- Expressive power -- Reflects many real-world problems Polymorphism -- Many forms of same function -- Virtual functions -- Abstract Base Classes Encapsulation (class) -- Information Hiding -- Separation of Interface and Implementation -- Standardization -- Access Control mechanisms (private /public) 1/12/2019 Ramamurthy

Why Object-oriented paradigm? Separation of interface and implementation: Any implementation change should not affect user interface. To ease design process: Separation of Design definition, Implementation, Usage through data encapsulation. Software reuse : To allow (1) sharing, (2) upgrades (3) modification using inheritance and polymorphism. Parameterized classes in templates. 1/12/2019 Ramamurthy

OO Concepts sampler Consider the automobile (car) you drive. List the behaviors (functions) of this automobile. The ones you have control over define the user-interface of the automobile. The internal behavior defines the private functionality. As a user you have no control over the implementation of these. List the attributes (parts) of the automobile. You have access to some. Example: steering wheel, brakes, head-lights, wipers…. You do not have access to some others: Odometer 1/12/2019 Ramamurthy

The notion of class Things discussed above are in general applicable to any car or Shall we say to a class of cars? Some functionality and parts are to be hidden. An interface containing a set of functions and components have to be presented to the user of the car. 1/12/2019 Ramamurthy

From problem to OO solution Identify the objects (“nouns”) in the problem. Define and implement classes to represent the objects. Class name Data members : public, private, (protected) Function members Service functions (usually public) Utility functions (usually private) Predicate functions. Example: warnings, status indicators, error indicators, etc. Write a driver that carries out the interaction among the objects. 1/12/2019 Ramamurthy

Member functions Member functions are based on “use cases” Ask the question: What is an object of this class used for? And enumerate its uses. This list will indicate to the designed one or more functions that need to be implemented to fulfill the “use” case 1/12/2019 Ramamurthy

Encapsulation : class car public: //interface // behavior or functions startEngine; accelerate; brake; park; shiftGear; // attributes or parts color; seatBelt; private: //under the hood displaySpeed(); Odometer; Engine;}; 1/12/2019 Ramamurthy

Object instantiation If you are interested in a specific car: Then an instance of the class can be created: Example : car mycar, rentedCar; mycar and rentedCar are objects of class car. It is also possible to set a specific characteristic such as color, if it is public. Example: mycar.color = green; If an attribute is private then an access function is needed to set its values. 1/12/2019 Ramamurthy

Inheritance Suppose that you want define a class for all Ford cars. All Ford cars do have the inherent characteristics of a general car described in the class car. They also have special characteristics of their own. So how to reuse general class car? Inherit the general characteristics. class FordCar : Car // meaning it inherits from class car { //special characteristics…}; 1/12/2019 Ramamurthy

Basic syntax : class definition class class_name { public: list of class attributes (variables, types, constants, and so on) that may be accessed by name from outside the class. list of prototypes for each member function that may be accessed by name from outside the class. private: list of class attributes (variables, types, constants, and so on) that are intended to be hidden for reference from outside the class. list of prototypes for each member function intended to be hidden from outside of the class. }; 1/12/2019 Ramamurthy

Access specifiers- private and public The scope of a specifier begin with the keyword private or public and ends when at the end of the class or at another access specifier. There can be more than one private or public sections. But it is a good programming style to pool all the public items into a single public section and all private into a single private section. By default all items in a class are private! So don’t forget to “public”ize. The items in a private section of a class are available only to the member function of the class. 1/12/2019 Ramamurthy

Simple definition of class Counter public: Counter(); //constructor void setCount(int); void getCount(); void increment(); void decrement(); private: int count; }; 1/12/2019 Ramamurthy

Class constructor(s) A class constructor is used to create (instantiate) an object of the class. The constructor is automatically executed each time an object (of class type) is declared. A class constructor has the same name as the class. Ensures all objects start in a consistent state and contains the initialization code. A constructor cannot specify a return type or explicitly return a value. A class typically has more than one constructor: default constructor, copy constructor and (converter) or initializing constructor. 1/12/2019 Ramamurthy

constructor implementation :: operator is called the class resolution operator. The class name before :: defines the class context in which a function is defined. Counter::Counter()// default constructor { count = 0; } Counter::Counter(int value) // initializing constructor { count = value;} 1/12/2019 Ramamurthy

Member Function implementations syntax: type class_name::fname (list of formal parameter types and names) { function body } 1/12/2019 Ramamurthy

Object Instantiation and Invoking Member Functions Counter score; score.setCount(45); score.increment(); score.decrement(); int x = score.getCount(); 1/12/2019 Ramamurthy

Files Involved #include “Counter.h” #include “Counter.h” Driver.cc For testing purposes Counter.cpp Counter.h #include “Counter.h” Application.cc 1/12/2019 Ramamurthy

Summary Class definition, implementation, usage were illustrated Three principles of OO design: encapsulation, inheritance, polymorphism. We looked at examples for the first two. Problem statement to Object-oriented design. Notion of class and object instantiation were illustrated. Access control specifiers were introduced. 1/12/2019 Ramamurthy