0% found this document useful (0 votes)
0 views

Classes and Objects

The document discusses Object-Oriented Programming (OOP) principles, focusing on the object-oriented approach, class and object definitions, and the Unified Modeling Language (UML). It emphasizes the importance of modeling real-world entities as objects and provides examples of various types of objects and classes. Additionally, it introduces UML as a tool for visualizing program structure and relationships without delving into code details.

Uploaded by

ayeshanoor777325
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Classes and Objects

The document discusses Object-Oriented Programming (OOP) principles, focusing on the object-oriented approach, class and object definitions, and the Unified Modeling Language (UML). It emphasizes the importance of modeling real-world entities as objects and provides examples of various types of objects and classes. Additionally, it introduces UML as a tool for visualizing program structure and relationships without delving into code details.

Uploaded by

ayeshanoor777325
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Object Oriented Programming

Lecture 03

 Object Oriented Approach


 Defining Classes and their Objects
 Unified Modeling Language(UML)

Book: Object-Oriented Programming in C++ by Robert Lafore, 4th Ed.


Chatper-1
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
Objects cont.
• 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
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 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.
Defining Class
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; }
};
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
Using the Class and Defining the
class smallobj //define a class
Objects
{
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
Using the Objects and Calling the
Member functions
class smallobj //define a class
{
private: Defining Objects
int somedata; //data member smallobj s1, s2;
public: //defines two objects, s1
void setdata(int d) //member function to set data and s2, of class smallobj
{ 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
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; }
};

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

Calling Member Functions


Call the member function setdata():
s1.setdata(1066);
s2.setdata(1776);
A Simple Class
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; }
};

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

You might also like