CS300: Object Oriented Analysis and Design
Classes and Objects
(Using C++)
Case study-Vending Machine
Vending Machine that allows users to buy snack items. In addition, a user can find out the caloric content of her choice.
Object Oriented Anslysis and Design (CS 212)
Specification
A Vending machine holds a number of snack items and displays the list of snack items and their prices through an user interface with a display screen and buttons for making selections. In addition, the vending machine has a receptacle for money and an item dispenser.
A user can make a selection and query for the number of calories of a snack item. The calories are displayed on pressing a button. A user can place the money in the receptacle and select an item.
Object Oriented Anslysis and Design (CS 212)
Step 1
A Vending machine holds a number of snack items and displays the list of snack items and their prices through an user interface with a display screen and buttons for making selections. In addition, the vending machine has a receptacle for money and an item dispenser.
A user can make a selection and query for the number of calories of a snack item. The calories are displayed on pressing a button. A user can place the money in the receptacle and select an item.
Object Oriented Anslysis and Design (CS 212)
Step 1
A Vending machine holds a number of snack items and displays the list of snack items and their prices through an user interface with a display screen and buttons for making selections. In addition, the vending machine has a receptacle for money and an item dispenser.
A user can make a selection and query for the number of calories of a snack item. The calories are displayed on pressing a button. A user can place the money in the receptacle and select an item.
Object Oriented Anslysis and Design (CS 212)
Example-Vending Machine
Write the CRC cards for wo problem specific classes.
Object Oriented Anslysis and Design (CS 212)
Example-Vending Machine
Problem-specific
Presentation-specific
Object Oriented Anslysis and Design (CS 212)
Two Programming Paradigms
Structural (Procedural) PROGRAM
FUNCTION
FUNCTION
FUNCTION
OBJECT
Operations
Data
OBJECT
Operations
Data
OBJECT
Operations
Data
Function calls
Messages passing
Object-Oriented PROGRAM
Object Oriented Anslysis and Design (CS 212)
Classes & Objects
Object Oriented Anslysis and Design (CS 212)
Classes & Objects
class Rectangle
{
private:
int width;
int length;
public:
void set(int w, int l);
int area();
}
Rectangle r1;
Rectangle r2;
Rectangle r3;
……
int a;
Object Oriented Anslysis and Design (CS 212)
Define a Class Type
class class_name
{
permission_label:
member;
permission_label:
member;
...
};
class Rectangle
{
private:
int width;
int length;
public:
void set(int w, int l);
int area();
};
Body
Header
Object Oriented Anslysis and Design (CS 212)
Class Definition-Data Members
Object Oriented Anslysis and Design (CS 212)
Static Data Member
class Rectangle
{
private:
int width;
int length;
static int count;
public:
void set(int w, int l);
int area();
}
Rectangle r1;
Rectangle r2;
Rectangle r3;
width
length
width
length
width
length
r1
r3
r2
count
Object Oriented Anslysis and Design (CS 212)
Memory Allocation for Objects
Common for all objects | ||||
| | Member function 1 | | |
| | | | |
| | | | |
| | Member function 2 | | |
| | | | |
| | | | |
Object 1 | | Object 2 | | Object 3 |
Member variable 1 | | Member variable 1 | | Member variable 1 |
| | | | |
| | | | |
Member variable 2 | | Member variable 2 | | Member variable 2 |
| | | | |
Memory created when functions defined
Memory created when objects defined
Object Oriented Anslysis and Design (CS 212)
Class Definition – Member Functions
Object Oriented Anslysis and Design (CS 212)
Member Function (Definition)
class Rectangle {
private:
int width, length;
public:
void set (int w, int l);
int area() {return width*length; }
}
void Rectangle :: set (int w, int l) {
width = w;
length = l;
}
inline
class name
member function name
scope resolution
operator
r1.set(5,8);
rp->set(8,10);
Object Oriented Anslysis and Design (CS 212)
Member Functions
Object Oriented Anslysis and Design (CS 212)
Member Function: Can be constant also
class Time
{
private :
int hrs, mins, secs ;
public :
void Write ( ) const ;
} ;
void Time :: Write( ) const
{
cout <<hrs << “:” << mins << “:” << secs << endl;
}
function declaration
function definition
Object Oriented Anslysis and Design (CS 212)
Class Definition: Access Control
Object Oriented Anslysis and Design (CS 212)
Example: Time Class
class Time {
public :
void Set ( int hours , int minutes , int seconds ) ;
void Increment ( ) ;
void Write ( ) const ;
Time ( int initHrs, int initMins, int initSecs ) ; // constructor
Time ( ) ; // default constructor
private :
int hrs ;
int mins ;
int secs ;
} ;
Object Oriented Anslysis and Design (CS 212)
What is an object?
OBJECT (Instance of a class)
Operations
Data
set of methods
(public member functions)
internal state
(values of private data members)
Object Oriented Anslysis and Design (CS 212)
Getters and Setters
Object Oriented Anslysis and Design (CS 212)
Class Interface
Object Oriented Anslysis and Design (CS 212)
Class Interface Diagram
Private data:
hrs
mins
secs
Set
Increment
Write
Time
Time
Time class
Object Oriented Anslysis and Design (CS 212)
Separating Interface from Implementation
Object Oriented Anslysis and Design (CS 212)
In a nutshell
Object Oriented Anslysis and Design (CS 212)