Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 17

CLASS & OBJECT

A class is encapsulation of:


•Data
•The function operating on that data

Object
An Object is an instance of a class. A class is a data
type and an object is its variable.
*The object occupies space in memory during runtime

but the class does not


#include<iostream.h>
Example
#include<conio.h>
class Counter
{
private:
int count;

public:
void initialize()
{count=0;}

void increment()
{count++;}

void decrement()
{ count--;}

int get_count()
{return count;}

}; //end of class

int main()
{
Counter c;
c.initialize();

c.increment();
c.increment();
c.increment();

cout<<endl<<c.get_count();

c.decrement();

cout<<endl<<c.get_count();
return 0;
}
Passing Object as Function
Argument & Returning Object
This Pointer
• Contains the address of current object
• Current object is the object with which the member function is called

Compiler does the following things automatically in a program:

It puts the address of the object as the argument with which the member
function is called

One parameter in each member function as the type of pointer of object


of that class is added. The name of the parameter is fixed , that is “this”

The “this ->” is placed before each member data in the definition of the
function, specified without any object name
Example
#include<iostream.h>
Class Where
{
private:
char charray[10]; // occupies 10 bytes
public:
void reveal()//displays the address of current object // void reveal(Where* this){…..}
{ cout<<“\nMY object’s address is “<<this;}
};
// main function
int main()
{
Where w1,w2,w3; //make these objects
W1.reveal(); // this is the pointer of w1 // W1.reveal(&W1);
W2.reveal();// this is the pointer of w2
W3.reveal();// this is the pointer of w3
cout<<endl;
return 0;
}
Uses of this pointer
• When an argument to a class function has the same
identifier as a class field

void Employee::setId(const int employeeIdNum)


{
this->employeeIdNum=employeeIdNum;
}

• Return the object from the member function


Static Member Data
• Each object contains its own separate data
members
• Member functions are shared amongst all
objects
• Exception: if a data member of a class is
declared as static; only one such item is
created for the entire class
Static Member Function
• You need a static function in order to access
a static field when you choose not to use an
object
• A static member function is that function
which access only static member data
• No non-static or object member data is
accessed inside the static member function
Example
#include<iostream.h>
class Counter{
static int count;
public:
static int ccount;
Counter(){count++;}
int getCount(){
return this->count; // return count;
}
static int getsCount(){
return count; // return this.count……Error as this is not permitted for a class function
}
}; // Class Ends
int Counter::count=0;
int Counter::ccount=0;
void main(){
Counter c1,c2,c3;
cout<<c1.getsCount()<<c1.getCount()<<Counter::getsCount()
<<Counter::ccount<<c1.ccount;
}
friend Function
• Only member functions can access the
private data of a class
• Functions outside the class can also have
access to the private data of a class---Friend
Functions
• friend function acts as a bridge between two
classes
Example
#include<iostream.h>
class Jill; // Forward Reference
class Jack{
private:
int i;
public:
Jack(){i=0;}
void setVar(int var){i=var;}
int getVar(){return i;}
friend void getId(Jack,Jill);
};
Example…
class Jill{
private:
int j;
public:
Jill(){j=0;}
void setVar(int var){i=var;}
int getVar(){return j;}
friend void getId(Jack,Jill);
};
Example…
void getId(Jack ja, Jill ji){
cout<<ja.i<<endl;
cout<<ji.j;
}

void main(){
Jack ja;
Jill ji;
ja.setVar(56);
ji.setVar(57);
cout<<“ja.getVar()=“<<ja.getVar();
cout<<“ji.getVar()=“<<ji.getVar();
getId(ja,ji);
}
To be noted…
• Forward Reference
• friend Function declaration is necessary in
both the classes
• This declaration can be placed either in the
private section or the public section of the
class
Use of friend Function
• To access private data of a class from a
non-member function

• To increase the versatility of overloaded


operators
friend Classes
• If we make the entire class as a friend then
automatically all the member functions of
the class become friends
Example
class two;
class one
{
private:
int i;
public:
One()
{
i=10;
}

friend two;
};
class two
{
public:
fun1(one o)
{ cout<<endl<<o.i;}

fun2(one o){cout<<endl<<o.i;}
};

void main()
{
one a;
two b;

b.fun1(a);
b.fun2(a);
}

You might also like