Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Introduction to Object Oriented Programming ( P ) Malik Jahan Khan 1."— Presentation transcript:

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

2 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

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

4 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

5 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

6 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

7 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

8 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

9 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

10 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

11 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

12 First Program Malik Jahan Khan 12

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

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(); } Class Definition 14

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(); } Attributes of Class (Data Members) 15

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; } }; 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

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 void main() { Person p1,p2,p3; p1.setID(10); p2.setID(20); p3.setID(30); p1.showID(); p2.showID(); p3.showID(); } 17

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 int main() { Person p1,p2,p3; p1.setID(10); p2.setID(20); p3.setID(30); p1.showID(); p2.showID(); p3.showID(); } Objects Definition 18

19 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

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

21 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

22 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

23 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


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

Similar presentations


Ads by Google