You are on page 1of 24

Chapter 02 Class and Objects

Class declaration

class class_name
{
private:
variables declarations;
functions declarations;
public:
variables declarations;
functions declarations;
};

Object Oriented Programming (SYIF) 2008-2009 by Kute T. B. -1-


Chapter 02 Class and Objects

Class

private area
No Data
Entry in
private
Functions
X

Data
Entry
allowed Functions
in public
public area

Data hiding in class

Object Oriented Programming (SYIF) 2008-2009 by Kute T. B. -2-


Chapter 02 Class and Objects

Defining member function outside


of the class

return-type class-name :: function-name(arguments)


{
// function body
} Scope resolution
operator
e. g.

class Bowler
{
int runsGiven;
float overs;
public:
void readValue(int rg, float ov );
float economyRate( );
};
void Bowler :: readValue(int rg, float ov)
{
runsGiven = rg;
overs = ov;
}
float Bowler :: economyRate( )
{
return(runsGiven/overs);
}

Object Oriented Programming (SYIF) 2008-2009 by Kute T. B. -3-


Chapter 02 Class and Objects

Nesting of member functions


#include<iostream.h>
class Finder
{
int num1, num2;
public:
void input(int m, int n);
int largest();
void display();
};
void Finder::input(int m, int n)
{
num1 = m;
num2 = n;
}
int Finder::largest( )
{
if(num1>num2) return num1;
else return num2;
}
void Finder::display( )
{
cout<<"Largest: "<<largest();
}
int main( )
{
Finder obj;
obj.input(45,12);
obj.display();
return(0);
}

Object Oriented Programming (SYIF) 2008-2009 by Kute T. B. -4-


Chapter 02 Class and Objects

Private member functions

class Reader
{
int val;
void read();
public:
void update();
void write();
};
Reader rd; // rd is object

then

rd.read( ) // won’t work


However the function read() can be called by the
functions update( ) or by write(). Such as,

void Reader :: update()


{
read(); //direct call
//no object used
}

Object Oriented Programming (SYIF) 2008-2009 by Kute T. B. -5-


Chapter 02 Class and Objects

#include<iostream.h>
class Sorter
{
int num[10];
public:
void input( );
void sort( );
void largest( );
};
void Sorter::input( )
{
for(int x=0;x<10;x++) cin>>num[x];
}
void Sorter::sort( )
{
for(int x=0;x<10;x++)
for(int y=x;y<10;y++)
if(num[x]>num[y])
{
num[x] = num[x] + num[y];
num[y] = num[x] - num[y];
num[x] = num[x] - num[y];
}
for(x=0;x<10;x++)
cout<<"\n"<<num[x];
}
void Sorter::largest( )
{
cout<<"\nLargest number:"<<num[9];
}
int main( )
{
Sorter sobj;
sobj.input( );
sobj.sort( );
sobj.largest( );
return(0);
}

Object Oriented Programming (SYIF) 2008-2009 by Kute T. B. -6-


Chapter 02 Class and Objects

Common for all objects

Data members
Memory
created when
Member functions functions
defined

Object1 Object2 Object3

Data members Data members Data members

Member functions Member functions Member functions

Memory created when


objects defined

Objects in Memory

Object Oriented Programming (SYIF) 2008-2009 by Kute T. B. -7-


Chapter 02 Class and Objects

Static data members


class Counter
{
static int count;
public:
void get(int x);
void getCount();
};
void Counter :: get(int x)
{
count = x;
}
void Counter :: getCount()
{
count++;
cout<<"\nCount: "<<count;
}
int Counter::count;
int main()
{
Counter a, b, c;
a.getCount();
b.get(15);
a.getCount();
c.getCount();
return(0);
}

Object Oriented Programming (SYIF) 2008-2009 by Kute T. B. -8-


Chapter 02 Class and Objects

Object ‘a’ Object ‘b’ Object ‘c’

get( ) get( ) get( )


getCount( ) getCount( ) getCount( )

count

Common for all three objects

Sharing of static data members

C haracteristics:
1. It is initialized to zero when the first
object of its class is created. No other
initialization is permitted.
2. Only one copy of that member is
created and shared by all the objects of
the class.
3. It is visible only within the class but its
lifetime is the entire program.

Object Oriented Programming (SYIF) 2008-2009 by Kute T. B. -9-


Chapter 02 Class and Objects

Static member function


class Test
{
int num;
static int count;
public:
void set(int x);
static void showCount( );
};
void Test :: set(int x)
{
count = x;
}
void Test :: showCount( )
{
count++;
cout<<"\nCount: "<<count;
}
int Test::count;
int main( )
{
Test a,b,c;
a.showCount( );
b.set(15);
a.showCount( );
c.showCount( );
Test::showCount( );
return(0);
}

Object Oriented Programming (SYIF) 2008-2009 by Kute T. B. - 10 -


Chapter 02 Class and Objects

Array of objects
class Student
{
char name[20];
float marks;
public:
void accept( );
void print( );
};
void Student :: accept( )
{
cout<<"\nEnter name: ";
cin>>name;
cout<<"\nEnter marks: ";
cin>>marks;
}
void Student :: print( )
{
cout<<"\nName: "<<name;
cout<<"\nMarks: "<<marks;
}
int main( )
{
Student SYIT[5];
for(int i=0;i<5;i++)
SYIT[i].accept( );
cout<<"\n\nYour data:\n";
for(i=0;i<5;i++)
SYIT[i].print( );
return(0);
}
Object Oriented Programming (SYIF) 2008-2009 by Kute T. B. - 11 -
Chapter 02 Class and Objects

Storage of data items of an object array

name accept( )
SYIT[0]
marks print( )
name accept( )
SYIT[1]
marks print( )
name accept( )
SYIT[2]
marks print( )
name accept( )
SYIT[3]
marks print( )
name accept( )
SYIT[4]
marks print( )

Object Oriented Programming (SYIF) 2008-2009 by Kute T. B. - 12 -


Chapter 02 Class and Objects

Object as function arguments

class Square
{
public:
int number;
void input(int);
int largeNum(Square, Square);
};
void Square::input(int num)
{
number = num;
}
int Square::largeNum(Square s, Square t)
{
if(s.number > t.number)
return(s.number);
return(t.number);
}
int main( )
{
Square s1,s2,s3;
s1.input(45);
s2.input(29);
cout<<"Large:"<<s3.largeNum(s1,s2);
return(0);
}

Object Oriented Programming (SYIF) 2008-2009 by Kute T. B. - 13 -


Chapter 02 Class and Objects

Object as function arguments

class Time
{
int hrs;
int min;
public:
void getTime(int h, int m)
{
hrs = h; min = m;
}
void putTime( )
{
cout<<hrs<<" hrs and ";
cout<<min<<" minutes\n";
}
void addTime(Time t1, Time t2)
{
min = t1.min + t2.min;
hrs = min / 60;
min = min % 60;
hrs = hrs + t1.hrs + t2.hrs;
}
};
int main( )
{
Time x, y, z;
x.getTime(2, 45);
y.getTime(3, 30);
z.addTime(x, y);
cout<<"Addition:";
z.putTime( );
return(0);
}
Object Oriented Programming (SYIF) 2008-2009 by Kute T. B. - 14 -
Chapter 02 Class and Objects

hrs x.hrs y.hrs


6 2 3

15 45 30
min x.min y.min
(x + y)

z.addTime(x, y)

Accessing members of object within a called


function

Object Oriented Programming (SYIF) 2008-2009 by Kute T. B. - 15 -


Chapter 02 Class and Objects

Before studying friend functions

class MyClass
{
int num1, num2;
public:
void initialize( );
int average(MyClass s);
};
void MyClass::initialize( )
{
num1 = 10;
num2 = 20;
}
int average(MyClass s)
{
return((s.num1+s.num2)/2);
}
int main()
{
MyClass x;
x.initialize();
cout<<"Average:"<<average(x);
return(0);
}

Object Oriented Programming (SYIF) 2008-2009 by Kute T. B. - 16 -


Chapter 02 Class and Objects

Characteristics of friend function

1. It is not in the scope of the class to


which it has been declared as ‘friend’.
2. Since it is not in the scope of the class,
it can not be called using object of the
class.
3. It can be invoked like a normal function
without the help of any object.
4. Unlike member functions, it can not
access the member names directly and
has to use an object name and dot
membership operator with each member
name (e.g. obj.x).
5. It can be declared either in public or the
private part of the class without
affecting its meaning.
6. Usually, it has objects as arguments.

Object Oriented Programming (SYIF) 2008-2009 by Kute T. B. - 17 -


Chapter 02 Class and Objects

class Slave;
class Master
{
int x;
public:
void initialize(int i)
{
x = i;
}
friend void findMax(Master, Slave);
};
class Slave
{
int y;
public:
void initialize(int j)
{
y = j;
}
friend void findMax(Master, Slave);
};
void findMax(Master m, Slave s)
{
if(m.x > s.y) cout<<"Max:"<<m.x;
else cout<<"Max:"<<s.y;
}
int main( )
{
Master a;
Slave b;
a.initialize(13);
b.initialize(19);
findMax(a,b);
return 0;
}

Object Oriented Programming (SYIF) 2008-2009 by Kute T. B. - 18 -


Chapter 02 Class and Objects

Function returning object


class Nation
{
float x,y;
public:
void enter(float m, float n)
{
x = m; y = n;
}
void show(Nation ind)
{
cout<<"\nFirst:"<<ind.x;
cout<<"\nSecond:"<<ind.y;
}
friend Nation findProd(Nation, Nation);
};
Nation findProd(Nation a, Nation b)
{
Nation temp;
temp.x = a.x * b.x;
temp.y = a.y * b.y;
return(temp);
}
int main( )
{
Nation lan, wan, man;
lan.enter(0.3, 1.2);
wan.enter(5.1, 2.7);
man = findProd(lan, wan);
man.show(man); //1.53 3.24
return 0;
}

Object Oriented Programming (SYIF) 2008-2009 by Kute T. B. - 19 -


Chapter 02 Class and Objects

Operator Overloading

return-type classname :: operator(op-arglist)


{
//function body
}

We can’t overload these operators:

1. Class member selection (. and .*)


2. Scope resolution operator (::)
3. Size of operator (sizeof)
4. Conditional operator( : ? )

Object Oriented Programming (SYIF) 2008-2009 by Kute T. B. - 20 -


Chapter 02 Class and Objects

Process of defining overloaded operators:


1. Create a class that defines the data
type that is to be used in the
overloading operation.
2. Declare the operator function in the
public part of the class. It may be either
a member function or a friend function.
3. Define the operator function to
implement the required operations.

Object Oriented Programming (SYIF) 2008-2009 by Kute T. B. - 21 -


Chapter 02 Class and Objects

#include<iostream.h>
class Space
{
int x,y;
public:
void get(int a, int b)
{
x = a;
y = b;
}
void show()
{
cout<<"\n X = "<<x;
cout<<"\n Y = "<<y;
}
void operator–()
{
x = –x;
y = –y;
}
};
int main()
{
Space s;
s.get(10,52);
cout<<"\nBefore overloading:";
s.show();
cout<<"\nAfter overloading:";
–s;
s.show();
return(0);
}

Object Oriented Programming (SYIF) 2008-2009 by Kute T. B. - 22 -


Chapter 02 Class and Objects

#include<iostream.h>
class Index
{
int val;
public:
Index(){}
Index(int z)
{
val = z;
}
void show()
{
cout<<"\n val = "<<val;
}
void operator++()
{
val = val + 10;
}
};
int main()
{
Index sit(9);
cout<<"\nBefore overloading:";
sit.show();
cout<<"\nAfter overloading:";
++sit;
sit.show();
return(0);
}

Object Oriented Programming (SYIF) 2008-2009 by Kute T. B. - 23 -


Chapter 02 Class and Objects

class Overload
{
int n1,n2,n3;
public:
Overload(){}
Overload(int a,int b,int c)
{
n1 = a; n2 = b; n3 = c;
}
void show()
{
cout<<"\n n1 = "<<n1;
cout<<"\n n2 = "<<n2;
cout<<"\n n3 = "<<n3;
}
v o i d o p e r a t o r *()
{
n1 = n2 = n3 = 0;
}
};
int main()
{
Overload check(6,3,4);
cout<<"\nBefore overloading:";
check.show();
cout<<"\nAfter overloading:";
*check;
check.show();
return(0);
}

Object Oriented Programming (SYIF) 2008-2009 by Kute T. B. - 24 -

You might also like