Classes and
Objects
LECTURE 2
Constant object and constant 2
member function
Constant object
Whose data member values cannot be changed
Can invoke constant member function only
Declared as,
const class_name object_name;
Constant object and constant 3
member function
Constant member function
Function in which data members remain constant
Uses ‘const’ as suffix in function declaration/definition
Return_type function_name(arg/s) const;
class alpha main()
{ int a; { 4
public: alpha s1 ;
alpha() : a(10) { } const alpha s2;
void display() s1.display();
{ //s2.display(); cannot invoke non-const func
a+=10; s1.show();
cout<<a<<endl; s2.show();
} }
void show()const
{
// a+=10; not permitted
cout<<a<<endl;
}
};
Static data member 5
Only one copy of that member is created for the entire class and
is shared by all the objects of that class, no matter how many
objects are created.
It is declared as,
static data_type member;
Accessible within class and have lifetime of entire program.
It is initialized outside the class because its scope is not limited
to class but entire program.
Data_type class_name :: member =value;
OR
Data_type class_name :: member; // takes zero value by default
int item :: count ; //count defined
class item
int main( )
{
static int count; //count is static
{ 6
item a,b,c;
int number;
a.get_count( );
public: The output would be
b.get_count( ); count:0
void getdata()
c.get_count( ); count:0
{
count:0
number=count++; After reading data
a.getdata( ); count: 3
}
b.getdata( ); count:3
void getcount(void)
count:3
c.getdata( );
{
cout<<”count:”;
cout«"after reading data : "«endl;
cout<<count<<endl;
a.get_count( );
}
b.gel_count( );
};
c.get count( );
return(0);
}
Static member function 7
Can access static data members only
Toinvoke static member function, we do not need to
access through object using member access
operator(.) but rather uses class name with scope
resolution operator because it refers to entire class
rather than specific object
Class_name:: static_function();
class test static void showcount()
{
int code;
{ cout<<"count="<<count<<endl; }
};
8
static int count; // static member variable
public: int test:: count; count=2
count=3
test()
object member : 1
{ int main() object member : 2
code=++count; object member : 3
{ count=3
test t1,t2; Object 3 destroyed
} Object 2 destroyed
test :: showcount ( ); Object 1 destroyed
void showcode() test t3;
{ test:: showcount( );
cout<<"object member : "<<code<<endl;
} t1.showcode( ) ;
~test() t2.showcode( );
{ t3.showcode( );
cout<<"Object "<<code<<" destroyed"<<endl; test:: showcount( );
count--; return(0);
} }
‘this’ pointer 9
When a member function(non static) is called, it is automatically
passed an implicit argument that is a pointer to the invoking object
(that is, the object on which the function is called). This pointer is
called this.
‘this’ pointer contains the address of that object
class alpha
{ public:
void showaddress()
{ cout<<this<<endl; }
};
main()
{ alpha s1 ;
s1.showaddress();
}
‘this’ pointer 10
The private variable ‘a’ can be used directly inside a
member function, like a=123;
We can also use the following statement to do the same
job.
this → a = 123
Also used to remove ambiguity among data members
class alpha
{ int a;
public:
void setdata(int a)
{ this-> a = a; }
‘this’ pointer 11
It acts as implicit argument to all member function. Hence
can be used to return object.
comp comp::add( comp c2)
{
real += c2.real;
this->imag += c2.imag;
return *this;
}
Which is invoked as c3=c1.add(c2)
Pointer to object and member access 12
As in basic data types and array, pointers can also point to
object as well
Class_name *object;
Uses indirection operator(->) for accessing members as,
object->member; // (*object).member
class alpha
{ public:
void showaddress()
{ cout<<this<<endl; }
};
main()
{ alpha s;
alpha *sp =&s;
sp->showaddress(); // or , (*sp).showaddress();
}
DMA for object and object array 13
For single object, For array of objects,
main() main()
{ alpha *sp1; { int n;
sp1=new alpha; cin>>n;
sp1->showaddress(); alpha *sp[20];
for(int i=0;i<n;i++)
alpha *sp2=new alpha; {
sp2->showaddress(); sp[i]=new alpha;
sp[i]->showaddress();
} }
}
Friend functions 14
Non-member function defined outside the class but
can access all data members of class
Prototype appears within the class(either in private or
public section)
Prototype is preceded by keyword “friend”
takes object as argument (or create object) as it
cannot access private data members directly
Invoked as normal function without object because it
is non-member function and does not belong to a
class
Friend functions 15
1. It can access private members of a class
float mean (sample s)
class sample {
{ return (float(s.a+s.b)/2.0);
}
int a, b;
public: main ( )
void setvalue( ) { a=25;b=40;} {
sample x;
friend float mean( sample s); x . setvalue( );
}; cout<<"mean value=“
<<mean(x)<<endl;
}
Friend functions 16
2. It can act as bridge between two or more classes
class abc; //forward declaration
void max( xyz m, abc n)
class xyz {
{ int x; if(m . x >= n.a)
public: cout<<m.x;
else
void setvalue(int i) { x = i; } cout<< n.a;
friend void max (xyz,abc); }
};
int main( )
class abc
{
{ int a; abc j;
public: j . setvalue( 10);
void setvalue( int i) {a=i; } xyz s;
s.setvalue(20);
friend void max(xyz,abc); max( s , j );
}; }
Friend functions 17
Member function of one class can be friend of another.
class B;
class A void A::show(B &x)
{ {
public: cout<<x.b;
void show(B&); }
};
main()
{
class B A m;
{ int b; B n;
public: m.show(n);
B():b(20){ } }
friend void A::show(B&);
};
Friend Class 18
A friend class can access all the private and protected data
members of the class to which it is friendly.
Member function of the friendly class becomes the friend
function to the class to which it is friendly.
Friend class is not mutual. That is, if the first class becomes
friend to second class, then it is not necessary that second
class is also friend to first class.
Friend Class 19
class B; void A::show(B &x)
class A {
cout<<a<<x.b;
{ int a;
}
public:
A():a(10){ }
main()
void show(B&);
{
}; A m;
B n;
m.show(n);
class B }
{ int b;
public: Note:1. friend class A can access
private member of B
B():b(20){} 2. Member function of class A
friend class A; becomes friend of B which can
access data of B.
};