Object Oriented Programming J G G Using C++: Classes Contd Classes Contd
Object Oriented Programming J G G Using C++: Classes Contd Classes Contd
j Oriented Programming
g g
using C++
Classes contd
contd.
Lecture‐06
1
What to Study?
• Classes, Objects and Memory
Classes
• Returning objects from functions
• Using
i this
hi pointer
i
• static class data
2
Classes, Objects and Memory
• Things are not quite so simple
• Each object has its own separate data items
• But all objects use same member functions
• M b ffunctions
Member i are created
d and
d placed
l d iin
memory only once
• No point in duplicating the member functions
for each object of a class
• See figure 6.8 in OOP using C++, 4th Edition by
Robert Lafore
3
Returning objects from functions
• The objects can also be returned from the
functions similar to the variables of normal
data types
• M
Mention
i theh return type as the
h name off theh
class in both declaration and definition of the
f
function
i
4
Returning objects from functions (Contd.)
Time add_time(Time); // declaration in class Time
// function definition, outside the class
Time Time::add_time(Time t1)
{
Time temp;
temp.min = min + t1.min;
temp.hr = hr + t1.hr;
return temp;
} 5
Using this pointer
The this pointer
• How does C++ ensure that the proper object is
referenced?
– C++ compiler maintains a pointer
pointer, called the this
pointer.
– When a member function is called
called, this pointer
contains the address of the object, for which the
function is invoked. So member functions can
access the data members using the pointer this
What happens when a member function is
called?
• Function Definition
void Date::display(date*
Date::display(date* this)
this)
// date*
d t * thi
this is
i hidd
hidden by
b programmer and
d
//automatically added by compiler.
• Function Calling
D1.display(&D1
D1.display( &D1);
);
// &D1 is hidden by programmer and automatically
//added by compiler.
What happens when a member
function is called?
void Date::display()
p y()
{
// These two statements do the same thing.
cout << month;
cout << this
thisÆmonth;
month;
}
Example
p
class Box {
– private:
• int height;
• int width;
• int length;
– public:
bli
• Box();
• Box(int,int,int);
Box(int int int);
• double volume();
• int compareVol (Box& box2 );
};
10
Example
• double Box::volume()
{ return length * width * height; }
• iintt Box::
B compareVol(
V l( Box&
B & box2)
b 2)
{
double v1 = this.volume(); ();
double v2 = box2.volume();
return v1 > v2 ? 1 : ( v1 < v2 ? ‐1 : 0);
}
11
Client program
int main()
{
Box b1(2, 3, 3);
Box b2(2,
b2(2 2,
2 2);
If (b1.compareVol(b2) == 1)
cout<<“ volume of first box is greater
cout<< greater”;;
else if (b1.compareVol(b2)== ‐1)
cout<<“ volume of second box is greater”;
else
cout<<“ volume of both boxes are equal”;
}
12
static members
Static Members
M m
• The keyword static preceding class data
member means that there is only one copy
of that member that is common to all
instances of the class
15
static Class Members
• static data members
– Initialized exactly once at file scope
• In the class implementation file
– Exist even if no objects of class exist
• Life span
span: program START to program END
16
static Class Members
• If static data member is private,
private a public static
member function is needed to access it.
• Static members are accessed using the class
name and the scope operator
• Static
S i d data members
b are iinitialised
i i li d separately
l
in the implementation of the class like global
variable.
i bl
17
static class data EXAMPLE
class foo {
private:
static int count; // declaration only
p
public:
foo()
{ count++;
count ; }
int getCount()
{ return count; }
};
Outside the class,
class before main():
int foo::count = 0; // definition of count 18
Accessing static
t ti class variables
19
Box Class – ObjectCount
B Class
Box Cl Declaration
D l ti
class Box {
private:
int height, width, length;
public:
Box ();
Box (int, int, int);
static int countBox; // Declaration
void display();
};
20
Box Class – ObjectCount
Class members
Cl b d
definitions
fi iti outside
t id
the class
int Box::countBox = 0; // Definition
Box::Box(){
countBox++;
height = width = length = 10;
}
….
….// Implementation of member functions continue
…….
21
Box Class – ObjectCount
Client Program
#include <iostream>
using namespace std;
void main()
()
{
Box B1;
cout << Box::countBox << endl;
Box B2;
Box::countBox=9;
cout << B2.countBox << endl;
}
22
What is next?
• const member functions and objects
• Pointers to classes
• Dynamici memory allocation
ll i andd classes
l
23