Introduction to Object Oriented Programming ( P ) Malik Jahan Khan 1.

Slides:



Advertisements
Similar presentations
CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 25 Thanks for Lecture Slides: Dr. Sadaf Tanveer Dr. Sadaf Tanveer,
Advertisements

1 Object-Oriented Programming OOP. 2 Object-oriented programming is a new paradigm for program development in which the focus is on the data instead of.
Objects and Classes First Programming Concepts. 14/10/2004Lecture 1a: Introduction 2 Fundamental Concepts object class method parameter data type.
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
1 Introduction to C++ Programming Concept Basic C++ C++ Extension from C.
C++ fundamentals.
Distribution of Marks Internal Sessional Evaluation Assignments – 10 Quizzes – 10 Class Participation Attendence – 5 Mid – Term Test – 25 External Evaluation.
OBJECT ORIENTED PROGRAMMING IN C++ LECTURE
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
Elements of a Java Program Bina Ramamurthy SUNY at Buffalo.
CS1201: Programming Language 2 Classes and objects By: Nouf Aljaffan Edited by : Nouf Almunyif.
Programming Paradigms Lecturer Hamza Azeem. What is PP ? Revision of Programming concepts learned in CPLB Learning how to perform “Object-Oriented Programming”
Salman Marvasti Sharif University of Technology Winter 2015.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Object Oriented Paradigm OOP’s. Problems with Structured Programming As programs grow ever larger and more complex, even the structured programming approach.
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.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Introduction to Object-oriented Programming
Andrew(amwallis) Classes!
Structures and Classes
EGR 2261 Unit 11 Pointers and Dynamic Variables
Review Array Array Elements Accessing array elements
Fucntions in C++ Malik Jahan Khan
Object-Orientated Programming
Object Oriented Programming Spring Semester
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING
Programming Logic and Design Seventh Edition
Overloaded Constructors and Multiple Classes
Phil Tayco Slide version 1.0 Created Sep 18, 2017
Objects, Classes, and Methods
Classes and OOP.
Institute of Business & Technology (BIZTEK)
Topic 7 Introduction to Classes and Objects
Review: Two Programming Paradigms
CS1201: Programming Language 2
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING (OOP) & CONCEPTS
INTRODUCTION TO OOP Objective:
Prepared By: G.UshaRani B.Pranalini A.S.Lalitha
OBJECT ORIENTED CONCEPTS
Introduction to Object-oriented Program Design
Advanced Programming Behnam Hatami Fall 2017.
Recursion 12-Nov-18.
Structures Lesson xx In this module, we’ll introduce you to structures.
Object-oriented Design in Processing
CMSC202 Computer Science II for Majors Lecture 04 – Pointers
Object-Oriented Programming
Recursion 2-Dec-18.
Object-Oriented Programming
Object-oriented Design in Processing
CPS120: Introduction to Computer Science
Object-Oriented Programming
IPC144 Introduction to Programming Using C Week 4 – Lesson 1
CS1201: Programming Language 2
Data Structures and Algorithms Introduction to Pointers
Recursion 23-Apr-19.
Defining Classes and Methods
Object-oriented Design in Processing
Review of Previous Lesson
CPS120: Introduction to Computer Science
Types of Computer Languages
IPC144 Introduction to Programming Using C Week 5 – Lesson 1
Object Oriented Programming (OOP)- Assist. Prof. Dr
OOP objects and Classes Dr. Ahmed Hashim Mohammed A Simple Class
Object-oriented Design in Processing
Chengyu Sun California State University, Los Angeles
Programming Fundamental
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

Introduction to Object Oriented Programming ( P ) Malik Jahan Khan 1

Problems at Hand! Functions have unrestricted access to all global data Unrelated functions and data Poor model of the real world – In real world, we have to deal with entities like person, item, car, book, course, university etc – None of these entities is like data – None of these entities is like functions 2

Solution Object Oriented Programming! But Why and How??? 3

Real World Modeling We deal with objects in real world like: – People – Vehicles – Computers – Inventory Items By now, we know about data attributes (variables and arrays etc) We also know about functions But the real word objects are neither like attributes nor functions In fact, real word objects have both attributes and some behavior (functions) 4

Attributes Attributes are the characteristics of an object Examples – People have eye color, name, job title, age, gender etc – Cars have horsepower, number of doors, model, color etc Attributes are like data members (variables etc.) in a program, e.g. – EyeColor (attribute) has value “blue” – Age (attribute) has value “25” – Model (attribute) has value “2003” 5

Behavior Behavior is something a real world object does in response to some stimulus Examples – If you ask your boss for a raise, she will generally say “YES” or “NO” – If you apply brakes in a car, it will stop Here, saying something and stopping are examples of behavior of two objects “boss” and “car” respectively Behavior is like function, you call a function to do something (e.g. stop the car, find factorial etc) 6

So….. Attributes are represented by data like variable, arrays, pointers etc Behavior is represented by functions But neither data nor functions, by themselves, represent or model real world objects 7

Object-Oriented Approach Fundamental idea behind OO approach is to combine into a single unit both data and functions that operate on that data Such a unit is called “object” Group of similar objects is called “class” 8

C++ and C C++ is derived from the language C C++ is a superset of C, that means almost every correct statement in C is also correct in C++ The most important elements added to C are concerned with classes, objects and object-oriented programming 9

OO Example Tahira and Maryam are two persons – Full Name of Person 1 = Tahira Urooj – Age of Person 1 = 20 – Address of Person 1: KC, Lahore – Full Name of Person 2 = Maryam Azhar – Age of Person 2 = 19 – Address of Person 2: LUMS We have 2 cars – Make of Car 1 = Suzuki – Model of Car 1 = 2005 – Make of Car 2 = Toyota – Model of Car 2 = 2000 How many objects, do we have? How many classes, do we have? What kind of behavior these objects may exhibit? 10

A Simple Example Each person in Pakistan is identified by a unique ID For sake of simplicity, assume that each ID is a simple integer We don’t need any other attribute of the person The simplest behavior which a person can exhibit is that it may be assigned (set) a new ID or it may display its ID when requested 11

First Program Malik Jahan Khan 12

First OO Program #include using namespace std; class Person { private: int id; public: void setID(int d) { id=d; } void showID() { cout<<"\n My ID is "<<id; } }; void main() { Person p1,p2,p3; p1.setID(10); p2.setID(20); p3.setID(30); p1.showID(); p2.showID(); p3.showID(); } 13

First OO Program #include using namespace std; class Person { private: int id; public: void setID(int d) { id=d; } void showID() { cout<<"\n My ID is "<<id; } }; void main() { Person p1,p2,p3; p1.setID(10); p2.setID(20); p3.setID(30); p1.showID(); p2.showID(); p3.showID(); } Class Definition 14

First OO Program #include using namespace std; class Person { private: int id; public: void setID(int d) { id=d; } void showID() { cout<<"\n My ID is "<<id; } }; void main() { Person p1,p2,p3; p1.setID(10); p2.setID(20); p3.setID(30); p1.showID(); p2.showID(); p3.showID(); } Attributes of Class (Data Members) 15

First OO Program #include using namespace std; class Person { private: int id; public: void setID(int d) { id=d; } void showID() { cout<<"\n My ID is "<<id; } }; void main() { Person p1,p2,p3; p1.setID(10); p2.setID(20); p3.setID(30); p1.showID(); p2.showID(); p3.showID(); } Class Behavior (Member Functions) 16

First OO Program #include using namespace std; class Person { private: int id; public: void setID(int d) { id=d; } void showID() { cout<<"\n My ID is "<<id; } }; //Class Definition must end with a semicolon void main() { Person p1,p2,p3; p1.setID(10); p2.setID(20); p3.setID(30); p1.showID(); p2.showID(); p3.showID(); } 17

First OO Program #include using namespace std; class Person { private: int id; public: void setID(int d) { id=d; } void showID() { cout<<"\n My ID is "<<id; } }; //Class Definition must end with a semicolon int main() { Person p1,p2,p3; p1.setID(10); p2.setID(20); p3.setID(30); p1.showID(); p2.showID(); p3.showID(); } Objects Definition 18

First OO Program #include using namespace std; class Person { private: int id; public: void setID(int d) { id=d; } void showID() { cout<<"\n My ID is "<<id; } }; //Class Definition must end with a semicolon void main() { Person p1,p2,p3; p1.setID(10); p2.setID(20); p3.setID(30); p1.showID(); p2.showID(); p3.showID(); } Using Member Functions 19

Instantiation of Objects Person data: ID methods: setID(id), showID() Class person 2 data: 20 person 1 data: 10 person 3 data: 30 20

Private and Public Data is concealed (hidden or masked) within a class so that it cannot be accessed mistakenly by functions out side the class The primary mechanism for hiding data is to put it in class and make it private Private data or functions can only be accessed from within the class Public data or functions are accessible from outside the class Usually, functions are public and data is private 21

Graphics Example #include "msoftcon.h" class circle { private: int xCo, yCo, radius; color fillcolor; fstyle fillstyle; public: void set(int x,int y,int r, color fc, fstyle fs) { xCo=x; yCo=y; radius=r; fillcolor=fc; fillstyle=fs; } void draw() { set_color(fillcolor); set_fill_style(fillstyle); draw_circle(xCo,yCo,radius); } }; int main() { init_graphics(); circle c1,c2,c3; c1.set(15,7,5,cBLUE,X_FILL); c2.set(41,12,7,cRED,O_FILL); c3.set(65,18,4,cGREEN,MEDIUM_FILL); c1.draw(); c2.draw(); c3.draw(); return 0; } 22

Self-Reading Previous example of Graphics (also given in textbook) Example of Widget Parts (page 223) C++ objects as data types (page 226) – Example of Distance There may be a surprise quiz next time from the stuff which we covered so far ;) 23