Object Oriented programming

Slides:



Advertisements
Similar presentations
Lesson 13 Introduction to Classes CS1 Lesson Introduction to Classes1.
Advertisements

C++ Classes & Data Abstraction
Road Map Introduction to object oriented programming. Classes
Important Definitions Class: A structured data type that is used to represent an abstract data type (ADT) Class member: A components of a class. It can.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Introduction To Classes Chapter Procedural And Object Oriented Programming Procedural programming focuses on the process/actions that occur in a.
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.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Agenda Object Oriented Programming Reading: Chapter 14.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
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.
1 OOP - An Introduction ISQS 6337 John R. Durrett.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
CPS120: Introduction to Computer Science Lecture 16 Data Structures, OOP & Advanced Strings.
Slide 1 Chapter 6 Structures and Classes. Slide 2 Learning Objectives  Structures  Structure types  Structures as function arguments  Initializing.
Introduction to Classes in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 24, 2009.
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.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Chapter 12 Classes and Abstraction
Regarding assignment 1 Style standards Program organization
EGR 2261 Unit 13 Classes Read Malik, Chapter 10.
Andrew(amwallis) Classes!
Procedural and Object-Oriented Programming
Functions + Overloading + Scope
Programming what is C++
Classes C++ representation of an object
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Chapter 16: Classes and Data Abstraction
Review What is an object? What is a class?
Andy Wang Object Oriented Programming in C++ COP 3330
Review: Two Programming Paradigms
Chapter 3: Using Methods, Classes, and Objects
Introduction to Classes
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Road Map Introduction to object oriented programming. Classes
of functions, methods & operators
(5 - 1) Object-Oriented Programming (OOP) and C++
Introduction to Classes
Classes and Data Abstraction
Object-Oriented Programming
Classes & Objects: Examples
Functions.
Chapter 9 Objects and Classes
Object-Oriented Programming
Tonga Institute of Higher Education
(5 - 1) Object-Oriented Programming (OOP) and C++
UNIT I OBJECT ORIENTED PROGRAMMING FUNDAMENTALS
COP 3330 Object-oriented Programming in C++
Object-Oriented Programming
Submitted By : Veenu Saini Lecturer (IT)
Classes C++ representation of an object
CPS120: Introduction to Computer Science
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Lecture 8 Object Oriented Programming (OOP)
Classes and Objects Systems Programming.
(4 – 2) Introduction to Classes in C++
C++ Object Oriented 1.
Object Oriented Programming
Introduction to Classes and Objects
Presentation transcript:

Object Oriented programming

Paradigm A Paradigm (para-dime) is a framework containing the basic assumptions, ways of thinking, and methodology that are commonly accepted by members of a scientific community. World View Standard Operating Procedure Process Model

Procedure Oriented Programming So far this semester, we've been concentrating on the POP Paradigm POP focuses on ACTION What does the Program DO?

Procedure Oriented Programming POP Design Involves: - Start with one procedure (function) that represents the entire program - Identify/specify a few sub-steps of the one procedure - Repeat this refinement process for each procedure until each procedure is simplistic (performs one specific task). Each procedure does something, and that action is usually reflected in its name as a verb.

Procedure Oriented Programming

Object Oriented Programming A software design paradigm that focuses on building models of Objects to solve a programming problem (instead of focusing on Action)

Object Oriented Programming OOP Design Process: 1. Identify the "natural" Objects in the project description 2. For each Object, identify its: - Name - Data Elements - Operations to/for/with/on ONE object 3. Design the interaction/communication between objects.

An Object is composed of: Data Elements and Operations

Data Elements Data Members - in C++ Members - in Java May be simple pieces of data (number, character, string, etc.) OR other objects. They are called: Data Members - in C++ Members - in Java

Operations perform actions on the data elements and/or communicate with other objects They are called: Function Members - in C++ Methods - in Java

Class is a description of an Object, including: - Name - List of Data Members - List of Methods - Access levels on each Member/Method: these define what "the outside world" may directly access.

A description of an object Class A description of an object A Class is to an Object as a Type is to a Variable Blueprint is to a House

Class Purpose: Abstraction! Hide the details from "the outside world" to make the object: - easier to use - protected from misuse by "the outside"

Class Declaration is divided into two parts: Interface - declarative information usually written in .h file (called a Header File) Implementation - Function Member declarations usually written in a .cpp file

Interface Syntax class name { public: list of public data members list of public function member prototypes private: list of private data members list of private function member prototypes };

Interface Syntax Data Member declaration: is the same as a variable declaration class customer { public: string firstName, lastname; private: int age; float shoesize; };

Interface Syntax Function Member Prototype: same as the header line of a function declaration, with a semicolon on the end. class name { public: void print(int x); private: int getStuff(); float calcStuff(float x, float y); };

Interface Syntax Example: in a file called fraction.h class fraction { public: void setNumer(int n); void setDenom(int d); int getNumer(); int getDenom(); float getDecimal(); private: int numer, denom; };

Implementation Syntax Remember from functions that a Prototype is a promise to the compiler that we will write a function, but it is declared elsewhere. The Implementation "fulfills" the promises made in the Interface.

Implementation Syntax For each function member prototyped in the interface: returnType className :: funMemName(args) { }

Implementation Syntax Example: in a file called fraction.cpp void fraction::set(int n, int d) { } void fraction::getNumer(){ void fraction::getDenom(){ float fraction::getDecimal(){ void fraction::reduce() {

Implementation Syntax Within all Function Members of a class, all Data Members have Global Scope (but also can be Masked!)

Implementation Syntax void fraction::set(int n, int d) { numer = n; // data mem = arg denom = d; // data mem = arg } float fraction::getDecimal(){ // localVar = expression w/ data mems float dec = (1.0 * numer)/denom; return dec;

Free Function A function that is not a Method of a class. It does not have classname:: in front of its name. int largest(int a, int b) { if (a > b) return a; else return b; }

Object Declaration After all that work...what have we accomplished?? Essentially, we've invented a new Data Type...except it is more complex than a Type, and so we call it a Class. Now the class name can be used the exact same way as a Type (ex: any way int is used).

Object Declaration void funstuff() { // allocate 3 variables of type double, nothing new double a,b,c; // allocate 3 objects of class fraction fraction f1, f2, f3; }

Object Declaration Semantics

Accessing Members: Dot Operator Syntax: object.publicMember object.publicMethod()

Accessing Members: Dot Operator void funstuff() { fraction f1; f1.numer=1; //error: numer is private f1.denom=0; //error: denom is private f1.set(2,3); float d = f1.getDecimal(); cout << d; // prints 0.66666 }

Constructors Construtors are (special) function members where: - their name is exactly the same as the class name - they do not have a return type - they must always be public - they are automatically invoked whenever an object is allocated (declared). Purpose: to initialize data members at the time they are created.

Constructors class fraction { public: fraction(); // constructor prototype private: int numer; int denom; }; fraction::fraction() { // constructor declaration numer = 0; denom = 1; }

Constructors void main() { fraction f; // constructor executed NOW! // f.numer = 0, f.denom = 1 fraction fr[3]; // 5 constructors exec. NOW! // fr[0].numer=0, fr[0].denom=1 // fr[1].numer=0, fr[1].denom=1 // fr[2].numer=0, fr[2].denom=1 }

All Data Members should be private OOP Design Principle All Data Members should be private (unless there is a very good reason to make them public) Code set() and get() Function Members for each Data Member to control access from the "outside world"

get() Function Members get() function members are used when the outside world needs the value of a private data member Solution: Return a COPY of the data member

get() Function Members class fraction { public: int getNumer(); // return type is the same private: int numer; // as the data member }; int fraction::getNumer() { return numer; }

set() Function Members set() function members are used when the outside world needs to change the value of a private data member Solution: Pass the new value in as an argument Assign the argument to the data member if the argument's value is valid

set() Function Members class fraction { public: void setDenom(int newDen); // argument type private: int denom; // same as member type }; void fraction::setDenom(int newDen) { if (newDen > 0) { // denom should be > 0 denom = newDen; } // else print error, do something else, or do nothing

Complete example: class fraction interface //in a file called fraction.h class fraction { public: fraction(); void setNumer(int n); void setDenom(int d); int getNumer(); int getDenom(); private: int numer, denom; };

Complete example: class fraction implementation //in a file called fraction.cpp fraction::fraction() { numer=0; denom=1; } int getNumer() { return numer; } int getDenom() { return denom; } void setNumer(int n){ numer = n; } void setDenom(int d) { if (d > 0) { denom = d; }

Vocabulary Term Definition Class a description of an object, including a name, list of data members, list of function members (methods), and the access levels of each. Object allocated instantiation of a Class, containing the members and methods defined in the Class with the designated access levels. Public designates data members and methods that CAN be directly accessed by the code using the object Private designates data members and methods that can NOT be directly access by the code using the object. Paradigm A framework containing the basic assumptions, ways of thinking, and methodology that are commonly accepted by members of a scientific community. Members Data elements of a class/object. Methods Functions/operations of a class/object. Interface Declarative portion of a class: defines the name, members, list of methods, and their access levels. Implementation Coding of the Methods of a class; where the methods are written. Constructor Function member with the same name as the class, used to initialize objects of the class. Free Function A function that is not part of a class.