15
Object Oriented Programming Objects and Classes

Classes and objects

Embed Size (px)

Citation preview

Object Oriented Programming

Objects and Classes

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

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

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

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.

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.

Relationship of Class and Object

Defining Classes

Keywords private and public are access specifiers

Functions Are Public, Data Is PrivateUsually the data within a class is private and the functions are public. The data is hidden so it will be safe from accidental manipulationThe functions that operate on the data are public so they can be accessed from outside the class.

Defining Classclass smallobj //define a class{private:

int somedata; //data memberpublic:void setdata(int d) //member function to set data { somedata = d; }void showdata() //member function to display data { cout << “Data is “ << somedata << endl; }};

Data MembersThe 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 FunctionsMember functions are functions that are included within a classThere are two member functions in smallobj: setdata() and showdata().

The definition of the class does not create any objectsIt only describes how objects of this class will look when they are created

Using the Class and Defining the Objects

class smallobj //define a class{private:

int somedata; //data memberpublic: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 typeSpace is set aside for it in memoryAn object is an instance of a classObjects are sometimes called instance variables

Using the Objects and Calling the Member functions

class smallobj //define a class{private:

int somedata; //data memberpublic: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 FunctionsCall 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 objectsetdata(1066); // error

Defining Objectssmallobj s1, s2; //defines two objects, s1 and s2, of class smallobj

Using the Objects and Calling the Member functions

class smallobj //define a class{private:

int somedata; //data memberpublic: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 FunctionsCall the member function setdata():s1.setdata(1066);s2.setdata(1776);

Defining Objectssmallobj s1, s2; //defines two objects, s1 and s2, of class smallobj

A Simple Class

int main(){smallobj s1, s2; //define two objects of class smallobjs1.setdata(1066); //call member function to set datas2.setdata(1776);s1.showdata(); //call member function to display datas2.showdata();return 0;}

class smallobj //define a class{private:

int somedata; //data memberpublic:void setdata(int d) //member function to set data { somedata = d; }void showdata() //member function to display data { cout << “Data is “ << somedata << endl; }};

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.

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