OBJECT
ORIENTED
PO BRJ O G R A M M I N G
E C T S AND CLASSES
- II
I m r a n Si dd i q i
D e p t of C S
B a h r i a Un iver s i t y, I s l am ab ad
[email protected]
1
CONTENTS
O Dynamic Memory Management
O Static Data and Static Functions
O Const Member Functions
O U M L Representation of Classes
O Objects as Function Arguments
2
CONTENTS
O Dynamic Memory Management
O Static Data and Static Functions
O Const Member Functions
O U M L Representation of Classes
O Objects as Function Arguments
3
DYNAMIC M E M ORY MANAGEMENT
WITH
OPERATORS NEW AND DELETE
O Dynamic memory management
● Control allocation and deallocation of memory
● Operators new and delete
O Example
Time *timePtr;
timePtr = new Time;
● new operator
O Creates object of proper size for type Time
O Calls default constructor for object
O Returns pointer of specified type
● Providing initializers
Time *timePtr = new Time( 12, 0, 0 );
DYNAMIC M E M ORY MANAGEMENT
WITH
OPERATORS NEW AND DELETE
O delete
● Destroy dynamically allocated object and free space
● Consider
delete timePtr;
● Operator delete
O Calls destructor for object
O Deallocates memory associated with object
5
CONTENTS
O Dynamic Memory Management
O Static Data and Static Functions
O Const Member Functions
O U M L Representation of Classes
O Objects as Function Arguments
6
STATIC C L AS S DATA
O Each object created has its own separate data
O But all objects in a class use the same member
functions since the functions for each object are
identical
O The member functions are created and placed in
memory only once where they are defined
O However if a data item in a class is declared as
static, then only one such item is created for the
entire class no matter how many objects are
created
O Visible only within the class but lifetime is the
entire program 7
STATIC C L AS S DATA
class foo
{
private:
static int count; //declaration
only public:
foo()
{ count+
+; } int
getCount()
{
return class
count; }
};
int main()
{ int foo::count
= foo
0;//definition
f1,f2,f3;
outside the
cout<<f1.getCount()<<f2.getCount()<<f3.getCount();
return 0;
}
8
STATIC M E M B E R FUNCTIONS
O Defined using the keyword static
O Function call is made with the scope resolution
operator thus associating the function to the class
rather than to a particular object
O Nonstatic member functions can access the static
variables (int getCount() ) in the previous
example
O However, a static member function cannot access
nonstatic data
9
STATIC M E M B E R FUNCTIONS
class foo
{
private:
static int count; //declaration only
public:
foo()
{ count++; }
static int getTotalCount()
{ return count; }
};
int foo::count = 0;//definition outside the class
int main()
{
foo f1,f2,f3;
cout<<“Total
Count:”<<foo::getTotalCount()<<endl;
return 0;
} 10
CONTENTS
O Dynamic Memory Management
O Static Data and Static Functions
O Const Member Functions
O U M L Representation of Classes
O Objects as Function Arguments
11
CONST MEMBER FUNCTIONS
O A const member function ensures that it will not
modify any of the class member data
O The const keyword should be used in both the
declaration (if any) and the definition of the function
body
O Member functions that do nothing but acquire data
from an object (for instance the display or show
functions) are candidates for being made const
void showdist ( ) const
{
cout <<feet << “ \‘ –” << inches <<“\” “;
12
}
CONST MEMBER FUNCTIONS
class aClass
{
private:
int a;
public:
void
nonCons
t_Func
void const_Func ( ) const //const function
(
{ )a = 100;} // Error :can’t modify a member
//non
const
};functio
n 13
{ a
= 100;}
CONTENTS
O Dynamic Memory Management
O Static Data and Static Functions
O Const Member Functions
O U M L Representation of Classes
O Objects as Function Arguments
14
U M L DIAGRAM - CLASSES
O Unified Modeling Language (UML) provides a set
of standard diagrams for graphically depicting
object-oriented systems.
Class name goes
here
Fields are listed here
Methods are listed
15
here
U M L DIAGRAM FOR RECTANGLE
CLASS
Rectangle
length
width
setLength()
setWidth()
getLength()
getWidth()
getArea()
16
UML: DATA TYPE AND PARAMETER
NOTATION
O U M L diagrams are language independent.
O U M L diagrams use an independent notation to
show return types, access modifiers, etc.
Access modifiers Rectangle
are denoted as:
+ public
- private - width : double
+ setWidth(w : double) : void
17
UML: DATA TYPE AND PARAMETER
NOTATION
Variable types are
Rectangle placed after the variable
name, separated by a
colon.
- width : double
+ setWidth(w : double) : void
18
UML: DATA TYPE AND PARAMETER
NOTATION
Method return types are
Rectangle placed after the method
declaration name,
separated by a colon.
- width : double
+ setWidth(w : double) : void
19
UML: DATA TYPE AND PARAMETER
NOTATION
Method parameters
are shown inside the Rectangle
parentheses using the
same notation as
variables. - width : double
+ setWidth(w : double) : void
20
CONVERTING THE U M L DIAGRAM
TO
CODE
O Putting all of this information together, a class
can be built easily using the U M L diagram.
class
header
ClassNam
{ e Fields
Fields
Methods
Method
s
}
21
COD
E
The structure of the class can be
class Rectangle
{
compiled and tested without having private:
bodies for the methods. Just be sure to double width;
put in dummy return values for methods double length;
that have a return type other than void. public:
void
setWidth(double w)
{
Rectangle void
} setLength(double len)
{
- width : double }
- length : double double getWidth()
{ return 0.0;
+ setWidth(w : double) : void }
+ setLength(len : double): void double getLength()
+ getWidth() : double { return 0.0;
+ getLength() : double }
double getArea()
+ getArea() : double 22
{ return 0.0;
}
};
CONTENTS
O Dynamic Memory Management
O Static Data and Static Functions
O Const Member Functions
O U M L Representation of Classes
O Objects as Function Arguments
23
O B J E C T S AS FU NC TI ON
ARGUMENTS
class Point
{
private:
int x;
int y;
public:
Point( : x(0),y(0)
)
{ }
Point(int x1, int y1): x(x1),y(y1)
{ }
24
O B J E C T S AS FU NC TI ON
ARGUMENTS
void input ( )
{
cout <<“\nEnter X : “; cin >> x;
cout <<“\nEnter Y : “; cin >> y;
}
void print ( )
{
cout << x << “,” << y <<endl;
}
void addPoints(Point, Point);
}; 25
O B J E C T S AS FU NC TI ON
ARGUMENTS
void Point :: addPoints(Point pA, Point pB)
{
x = pA.x + pB.x;
y = pA.y + pB.y;
}
26
O B J E C T S AS FU NC TI ON
ARGUMENTS
void main( )
{
Point p1, p2(1,1), p3(4,4);
p1.addPoints(p2, p3);
cout <<“\n Point1 = “ ; p1.print( );
cout <<“\n Point2 = “ ; p2.print( );
cout <<“\n Point3 = “ ; p3.print( );
}
27
O B J E C T S AS FU NC TI ON
ARGUMENTS
class Distance
{
private:
int feet;
float inches
public:
Distance() : feet(0),inches(0.0)
{ }
Distance(int ft, float in): feet(ft),inches(in)
{ }
28
O B J E C T S AS FU NC TI ON
ARGUMENTS
void input_dist ( )
{
cout <<“\nEnter feet : “; cin >> feet;
cout <<“\nEnter inches : “; cin >> inches;
}
void showdist ( )
{
cout <<feet << “ \‘ - << inches <<“\” “;
}
void add_dist(Distance, Distance);
}; 29
O B J E C T S AS FU NC TI ON
ARGUMENTS
void Distance :: add_dist(Distance d2, Distance d3)
{ inches = d2.inches + d3.inches;
feet =0;
if (inches >= 12.0)
{ inches -= 12.0;
feet++;
}
feet += d2.feet + d3.feet;
}
30
O B J E C T S AS FU NC TI ON
ARGUMENTS
void main( )
{
Distance dist1, dist3;
Distance dist2(10, 3.5);
dist1.input_dist( );
dist3.add_dist(dist1, dist2);
cout <<“\ndist1 = “ ; dist1.showdist( );
cout <<“\ndist2 = “ ; dist2.showdist( );
cout <<“\ndist3 = “ ; dist3.showdist( );
} 31
RETURNING O B J E C T S FROM
{
FUNCTIONS
Distance Distance :: add_dist(Distance d2)
Distance temp;
temp.inches = inches + d2.inches;
if (temp.inches >= 12.0)
{ temp.inches -= 12.0;
temp.feet =1;
}
temp.feet += feet + d2.feet;
return temp;
}
int main()
{
Distance dist1,dist2;
Distance dist3(11,6.5);
dist1.getdist();
dist2 = dist1.add_dist(dist3);
return 0;
}
32