2. Object Oriented Approach
When we enters in OO approach, we
− Do not focus how problem divides into functions
− Do Focus how to divide into objects
This approach helps to model real world
− Everything in real world can be imagine to be divided
into objects
3. Object Oriented Approach
When we enters in OO approach, we
− Do not focus how problem divides into functions
− Do Focus how to divide into objects
This approach helps to model real world
− Everything in real world can be imagine to be divided
into objects
4. Objects
What kinds of things become objects in object-oriented
programs
− Depends on thinker’s imaginations
− Few Examples to take a start
Automobiles in a traffic-flow simulation
Countries in an economics model
Aircraft in an air traffic control system
Elements of the computer-user environment
− Menus
− Graphics objects (lines, rectangles, circles)
− The mouse, keyboard, disk drives, printer
Data-storage constructs
− Customized arrays
− Stacks
− Linked lists
− Binary trees
Human entities
− Employees
− Students
− Customers
− Salespeople
5.
Collections of data
− An inventory
− A personnel file
− A dictionary
User-defined data types
− Time
− Angles
− Complex numbers
− Points on the plane
Components in computer games
− Cars in an auto race
− Positions in a board game (chess, checkers)
− Animals in an ecological simulation
− Opponents and friends in adventure games
Objects cont.
6. Classes
In OOP we say that objects are members of
classes
A class is thus a description of a number of
similar objects
Class serves as a plan
It specifies what data and what functions will be
included in objects of that class
Defining the class doesn’t create any objects
An object is often called an “instance” of a class.
8. Defining Classes
Keywords private and public are access specifiers
Functions Are Public, Data Is Private
Usually the data within a class is private and the functions are public.
The data is hidden so it will be safe from accidental manipulation
The functions that operate on the data are public so they can be accessed from
outside the class.
9. Defining Classclass smallobj //define a class
{
private:
int somedata; //data member
public:
void setdata(int d) //member function to set data
{ somedata = d; }
void showdata() //member function to display data
{ cout << “Data is “ << somedata << endl; }
};
Data Members
The smallobj class contains one data item or data member(somedata), which is of type int.
There can be any number of data members in a class
Member Functions
Member functions are functions that are included within a class
There are two member functions in smallobj: setdata() and showdata().
The definition of the class does not create any objects
It only describes how objects of this class will look when they are created
10. Using the Class and Defining the
Objectsclass smallobj //define a class
{
private:
int somedata; //data member
public:
void setdata(int d) //member function to set data
{ somedata = d; }
void showdata() //member function to display data
{ cout << “Data is “ << somedata << endl; }
};
Defining Objects
smallobj s1, s2; //defines two objects, s1 and s2, of class smallobj
Defining an object is similar to defining a variable of any data type
Space is set aside for it in memory
An object is an instance of a class
Objects are sometimes called instance variables
11. Using the Objects and Calling
the Member functions
class smallobj //define a class
{
private:
int somedata; //data member
public:
void setdata(int d) //member function to set data
{ somedata = d; }
void showdata() //member function to display data
{ cout << “Data is “ << somedata << endl; }
};
Calling Member Functions
Call the member function setdata():
s1.setdata(1066);
s2.setdata(1776);
This strange syntax is used to call a member function that is associated with a specific
object
setdata(1066); // error
Defining Objects
smallobj s1, s2;
//defines two objects, s1
and s2, of class smallobj
12. Using the Objects and Calling
the Member functions
class smallobj //define a class
{
private:
int somedata; //data member
public:
void setdata(int d) //member function to set data
{ somedata = d; }
void showdata() //member function to display data
{ cout << “Data is “ << somedata << endl; }
};
Calling Member Functions
Call the member function setdata():
s1.setdata(1066);
s2.setdata(1776);
Defining Objects
smallobj s1, s2;
//defines two objects, s1
and s2, of class smallobj
13. A Simple Class
int main()
{
smallobj s1, s2; //define two objects of class smallobj
s1.setdata(1066); //call member function to set data
s2.setdata(1776);
s1.showdata(); //call member function to display data
s2.showdata();
return 0;
}
class smallobj //define a class
{
private:
int somedata; //data member
public:
void setdata(int d) //member function to set data
{ somedata = d; }
void showdata() //member function to display data
{ cout << “Data is “ << somedata << endl; }
};
14. The Unified Modeling Language
(UML)
The UML is a graphical “language” for modeling
computer programs
UML provides a way to visualize the higher-level
organization of programs without getting in the
details of actual code.
15. Why do we need the UML
The trouble with code is that it’s very detailed
We need a way to see a bigger picture that:
− depicts the major parts of the program and how they work
together
UML provides this picture.
UML is a set of different kinds of diagrams
− Class diagrams
show the relationships among classes
− Object diagrams
show how specific objects relate,
− Sequence diagrams
show the communication among objects over time
− Use case diagrams
show how a program’s users interact with the program, and so on