You are on page 1of 25

Objects and Classes

Farooq Ahmed, FAST-NU, Lahore

Object

Object is an entity exhibiting 3 characteristics:

State
Behavior
Identity

An object represents an individual, identifiable


item, unit, or entity, either real or abstract, with
a well defined role in the problem domain

Farooq Ahmed, FAST-NU, Lahore

Object state

An object can be visualized as a collection of


properties. It represents an entity to which certain
characteristics can be attributed
Property can be assigned a value
State of an object is the collective set of all
properties and their values at a given point in time
Example

Circle centered at point (100,100) on a canvas and having a


radius 50
Transcript of a student named Ali and roll number 001, listing
grades earned as yet
Farooq Ahmed, FAST-NU, Lahore

200
obj : Circle
50

200

(100,100)

x: 100
y: 100
radius: 50

obj : Transcript
rollno: 001
name: Ali
grades: { {English Language, B},
{Physics-I, A},
{Introduction to Computing, A-},

{Computer Logic Design, I}


}

Farooq Ahmed, FAST-NU, Lahore

Object behavior

Transition of object from one state to another is the


behavior of object
Behavior is controlled through an action that is
performed on the object

Generally referred as a method

All actions collectively specify the behavior


An inquiry of a state is an action that doesn't
change the state

getter methods
Farooq Ahmed, FAST-NU, Lahore

init state
200
obj : Circle
50

200

x: 100
y: 100
radius: 50

(100,100)

obj.move (150, 100)


200
obj : Circle
50

200

(150,100)

x: 150
y: 100
radius: 50

Farooq Ahmed, FAST-NU, Lahore

init state
obj : Transcript
rollno: 001
name: Ali
grades: { {English Language, B},
{Physics-I, A},
{Introduction to Computing, A-},

{Computer Logic Design, I}


}
obj.updateGrade(Computer Logic Design, B)
obj : Transcript
rollno: 001
name: Ali
grades: { {English Language, B},
{Physics-I, A},
{Introduction to Computing, A-},

{Computer Logic Design, I}


}
Farooq Ahmed, FAST-NU, Lahore

Object Identity

Each object has a unique identity that


distinguishes it from others
Objects may have similar state, yet they may
not same
Impacts object cloning / copying

shallow vs deep

Object aliases

Pointers / References
Farooq Ahmed, FAST-NU, Lahore

init state
200
obj1 : Circle
50

200

(100,100)

obj2 : Circle

50
(150,100)

x: 100
y: 100
radius: 50

x: 150
y: 100
radius: 50

obj1.move (150, 100)


200

50

200

(150,100)

obj1 : Circle
x: 150
y: 100
radius: 50
Farooq Ahmed, FAST-NU, Lahore

obj2 : Circle
x: 150
y: 100
radius: 50

: Circle
x: 100
y: 100
radius: 50

: Circle
x: 0
y: 100
radius: 20

: Circle

class Circle {

x: 150
y: 100
radius: 50

: Circle

private:
int x;
int y;
int radius;
classified as

x: 150
y: 100
radius: 25

Farooq Ahmed, FAST-NU, Lahore

public:
Circle(...)
void move(int x,int y);

};

Class

Class classifies related objects into a single


abstraction / type

A meaningful name is assigned to related phenomenon


A purposeful suppression of details to bring clarity
Helps conceptualize a phenomenon
Helps understand how corresponding objects behave
A blue-print / template for its objects

Designing good abstractions is one of the principal issues in OOAD


Farooq Ahmed, FAST-NU, Lahore

Class

Class encapsulates attributes and methods


into a module

Data and procedural members are grouped together


Acts as a modular unit of implementation in OO
Hides implementation behind a well-defined interface
Easy to make changes to module

Related methods are easy to locate


Clients remain unaffected when internals change

Designing good encapsulations is one of the principal issues in OOAD


Farooq Ahmed, FAST-NU, Lahore

Class definition syntax


class Circle {

class Circle {

private:
int x;
int y;
int radius;
public:
Circle(...)
void move(int x,int y);

};
C++

class Circle:

private int x;
private int y;
private int radius;

x=0
y=0
radius = 0

public Circle(...) { }
public void move(int x,int y){

def __init__(self):

}
Java / C#

Farooq Ahmed, FAST-NU, Lahore

def move(self,x,y):

Python

UML Notation
Class name

Private member

Attributes

Public member

Methods

Farooq Ahmed, FAST-NU, Lahore

Notational Possibilities

Farooq Ahmed, FAST-NU, Lahore

Instantiating Objects in C++


Option 1
class Circle {

private:
int x;
int y;
int radius;

Circle c1;
Circle c2;

public:
Circle(...)
void move(int x,int y);

};

Circle * c1 = new Circle();


Circle * c2 = new Circle();

}
Option 2

Farooq Ahmed, FAST-NU, Lahore

Object lifeline

Object gets a life at time of instantiation

Object life shall not be tied up to scope

Object instantiation shall be explicit

Object instantiated on heap space

Object destruction can be controlled

Using explicit deletion


Using automatic garbage collection

Farooq Ahmed, FAST-NU, Lahore

Heap
{

Circle * c1 = new Circle(100,100, 50);


Circle * c2 = new Circle(100,100,100);

: Circle

c1

x: 100
y: 100
radius: 50

Aliases
Heap

Objects
: Circle

c2

x: 100
y: 100
radius: 100
Farooq Ahmed, FAST-NU, Lahore

Memory Leak
{

c2 = c1;

: Circle

c1

x: 100
y: 100
radius: 50

Aliases
Heap

Objects
: Circle

c2

x: 100
y: 100
radius: 100
Farooq Ahmed, FAST-NU, Lahore

Objects as Parameters
class Circle {

};
class Canvas {

public:
void drawCircle(Circle);
};

Circle circle(100,100,50);
Canvas canvas;
canvas.drawCircle(circle);

circle.move(150,150);
}

class Canvas {

public:
void drawCircle(Circle*);
};

Circle * circle = new Circle(100,100,50);


Canvas * canvas = new Canvas;
canvas->drawCircle(circle);

circle->move(150,150);
}
Farooq Ahmed, FAST-NU, Lahore

Dangling Pointers
class Circle {

};
class Canvas {

public:
void drawCircle(Circle*);
};
void Canvas::drawCircle(Circle* c){

delete c;
}

Circle * circle = new Circle(100,100,50);


Canvas * canvas = new Canvas;
canvas->drawCircle(circle);

circle->move(150,150);
}

Use smart pointers (e.g. auto_ptr, shared_ptr,etc.


in C++
STL) to handle dangling pointer issues
Farooq Ahmed, FAST-NU,
Lahore

Instantiating Objects in Java

class Circle {

private int x;
private int y;
private int radius;
public Circle(...) { }
public void move(int x,int y){

}
Circle c = new Circle(...);

No pointers in Java
Variable always acts
as an alias/reference

Object exists on heap

Alias leads to object

No memory leak or
dangling pointer
issues

Alias
Farooq Ahmed, FAST-NU, Lahore

Class Variables

Class variable (also called static variables) are shared by all instances
bound to class not the objects
can be visualized as global variables bounded in a class
May be useful for constants, defaults, etc.

class Circle {
private:
int x;
int y;
int radius;
static float PI = 3.14...
public:
Circle(...)
void move(int x,int y);

};
Farooq Ahmed, FAST-NU, Lahore

Method Binding

Methods are declared once in a class

Methods are bound to each object separately on call


class Circle {
private:
int x;
int y;
int radius;
static float PI = 3.14...
public:
Circle(...)
void move(int x,int y);
float area();

};

float Circle::area(){
return radius * radius * PI;
}

Circle * c1 = new Circle(100,100,50);


Circle * c2 = new Circle(100,100,100);
cout << c1->area(); // 7854
cout << c2->area(); // 31416
Use of this pointer
Farooq Ahmed, FAST-NU, Lahore

Static methods

Used to access static variables

Cannot access non-static variables

Cannot use this pointer

Farooq Ahmed, FAST-NU, Lahore

You might also like