1 of 26

CS300: Object Oriented Analysis and Design

Classes and Objects

(Using C++)

2 of 26

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)

3 of 26

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)

4 of 26

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)

5 of 26

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)

6 of 26

Example-Vending Machine

  • Most of the nouns are objects/classes.
  • Some nouns are attributes of these classes.
  • The verbs are actions that can be attached to these objects.
  • In order to focus on the problem-domain objects, let us separate the object/classes into presentation-specific (user-interface related) and problem-specific classes.

Write the CRC cards for wo problem specific classes.

Object Oriented Anslysis and Design (CS 212)

7 of 26

Example-Vending Machine

Problem-specific

  • Vending Machine
  • Snack item
  • Price
  • Calories
  • Selection
  • User

Presentation-specific

  • Display screen
  • Selection Buttons
  • Item Dispenser
  • Money receptacle

Object Oriented Anslysis and Design (CS 212)

8 of 26

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)

9 of 26

Classes & Objects

  • The class is the cornerstone of C++
    • It gives the C++ its identity from C
    • It makes possible encapsulation, data hiding and inheritance
  • Class:
    • Consists of both data and methods
    • Defines properties and behavior of a set of entities
  • Object:
    • An instance of a class
    • A variable identified by a unique name

Object Oriented Anslysis and Design (CS 212)

10 of 26

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)

11 of 26

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)

12 of 26

Class Definition-Data Members

  • Abstract the common attributes of a group of entities, their values determine the state of an object
  • Can be of any type, built-in or user-defined
  • non-static data member
    • Each class object has its own copy
    • Cannot be initialized explicitly in the class body
    • Can be initialized with member function, or class constructor
  • static data member
    • Acts as a global object, part of a class, not part of an object of that class
    • One copy per class type, not one copy per object

Object Oriented Anslysis and Design (CS 212)

13 of 26

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)

14 of 26

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)

15 of 26

Class Definition – Member Functions

  • Used to
    • access the values of the data members (accessor)
    • perform operations on the data members (implementor)

  • Are declared inside the class body, in the same way as declaring a function

  • Their definition can be placed inside the class body, or outside the class body

  • Can access both public and private members of the class
  • Can be referred to using dot or arrow member access operator

Object Oriented Anslysis and Design (CS 212)

16 of 26

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)

17 of 26

Member Functions

  • static member function
  • const member function
    • declaration
      • return_type func_name (para_list) const;
    • definition
      • return_type func_name (para_list) const { … }
      • return_type class_name :: func_name (para_list) const { … }
    • Makes no modification about the data members
    • Ensures safety of the member variables

Object Oriented Anslysis and Design (CS 212)

18 of 26

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)

19 of 26

Class Definition: Access Control

  • Information hiding
    • To prevent the direct access from outside the class
  • Access Specifiers (optional, repeatable, not ordered)
    • Public (Class Interface)
      • may be accessible from anywhere within a program
    • Private (default access specifier)
      • may be accessed only by the member functions, and friends of this class, not open for nonmember functions
    • protected
      • acts as public for derived classes (virtual)
      • behaves as private for the rest of the program
  • Difference between classes and structs in C++

Object Oriented Anslysis and Design (CS 212)

20 of 26

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)

21 of 26

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)

22 of 26

Getters and Setters

  • Private access specifier facilitates data hiding

  • Public set and get function allows clients to access data, but not indirectly

  • Class may store data in one way, however shows to the client in a different way

  • Get and set function helps the client to interact with the object

  • The private data member remains safely encapsulated

Object Oriented Anslysis and Design (CS 212)

23 of 26

Class Interface

  • Header files supports reusability

  • Interface: standardized way of interaction between a pair of objects (e.g. people, systems)

  • Defines what services a client can use and how to request those services

  • However, NOT how the class carry out those services

  • A classes interface consists of the public member functions

Object Oriented Anslysis and Design (CS 212)

24 of 26

Class Interface Diagram

Private data:

hrs

mins

secs

Set

Increment

Write

Time

Time

Time class

Object Oriented Anslysis and Design (CS 212)

25 of 26

Separating Interface from Implementation

  • It is better software engineering to define member functions outside the class definition
  • Their implementation can be hidden from the clients code
  • Client code independent of class’s implementation

Object Oriented Anslysis and Design (CS 212)

26 of 26

In a nutshell

  • Class can be considered as a user-defined data type

  • An object is just a variable of certain class.

  • There are three parts in the definition of a class:
  • data members,
  • member functions, and
  • access control.

Object Oriented Anslysis and Design (CS 212)