Introduction to Object Oriented Programming P Malik Jahan
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. – Eye. Color (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
using namespace std; class Person { private: int id; public: void set. ID(int d) { id=d; } void show. ID() { cout<<"n My ID is "<<id; } }; void main() { Person p 1, p 2, p 3; p 1. set. ID(10); p 2. set. ID(20); p 3. set. ID(30); p 1. show. ID(); p 2. show. ID(); p 3. show. ID(); } First OO Program 13
#include<iostream> using namespace std; class Person { private: int id; public: void set. ID(int d) { id=d; } void show. ID() { cout<<"n My ID is "<<id; } }; void main() { Person p 1, p 2, p 3; p 1. set. ID(10); p 2. set. ID(20); p 3. set. ID(30); p 1. show. ID(); p 2. show. ID(); p 3. show. ID(); First OO Program Class Definition 14
First OO Program #include<iostream> using namespace std; class Person { private: int id; public: void set. ID(int d) { id=d; } void show. ID() { cout<<"n My ID is "<<id; } }; void main() { Person p 1, p 2, p 3; p 1. set. ID(10); p 2. set. ID(20); p 3. set. ID(30); p 1. show. ID(); p 2. show. ID(); p 3. show. ID(); } Attributes of Class (Data Members) 15
First OO Program #include<iostream> using namespace std; class Person { private: int id; public: void set. ID(int d) { id=d; } void show. ID() { cout<<"n My ID is "<<id; } }; void main() { Person p 1, p 2, p 3; p 1. set. ID(10); p 2. set. ID(20); p 3. set. ID(30); p 1. show. ID(); p 2. show. ID(); p 3. show. ID(); } Class Behavior (Member Functions) 16
First OO Program #include<iostream> using namespace std; class Person { private: int id; public: void set. ID(int d) { id=d; } void show. ID() { cout<<"n My ID is "<<id; } }; //Class Definition must end with a semicolon void main() { Person p 1, p 2, p 3; p 1. set. ID(10); p 2. set. ID(20); p 3. set. ID(30); p 1. show. ID(); p 2. show. ID(); p 3. show. ID(); } 17
First OO Program #include<iostream> using namespace std; class Person { private: int id; public: void set. ID(int d) { id=d; } void show. ID() { cout<<"n My ID is "<<id; } }; //Class Definition must end with a semicolon int main() { Person p 1, p 2, p 3; p 1. set. ID(10); p 2. set. ID(20); p 3. set. ID(30); p 1. show. ID(); p 2. show. ID(); p 3. show. ID(); } Objects Definition 18
First OO Program #include<iostream> using namespace std; class Person { private: int id; public: void set. ID(int d) { id=d; } void show. ID() { cout<<"n My ID is "<<id; } }; //Class Definition must end with a semicolon void main() { Person p 1, p 2, p 3; p 1. set. ID(10); p 2. set. ID(20); p 3. set. ID(30); p 1. show. ID(); p 2. show. ID(); p 3. show. ID(); } Using Member Functions 19
Instantiation of Objects Person data: ID methods: set. ID(id), show. ID() Class person 1 data: 10 person 2 data: 20 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 x. Co, y. Co, radius; color fillcolor; fstyle fillstyle; public: void set(int x, int y, int r, color fc, fstyle fs) { x. Co=x; y. Co=y; radius=r; fillcolor=fc; fillstyle=fs; } void draw() { set_color(fillcolor); set_fill_style(fillstyle); draw_circle(x. Co, y. Co, radius); } }; int main() { init_graphics(); circle c 1, c 2, c 3; c 1. set(15, 7, 5, c. BLUE, X_FILL); c 2. set(41, 12, 7, c. RED, O_FILL); c 3. set(65, 18, 4, c. GREEN, MEDIUM_FILL); c 1. draw(); c 2. draw(); c 3. 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
- Slides: 23