this POINTER
C++ use a unique keyword called ‘this’ to represent an object that invoke a member function. A member function of every class object has access to a pointer named ‘this’ which points to object itself. ‘this’ pointer stores the address of object. It can also be used to access the data.
When any class member function executes, it automatically contains a hidden pointer called this, which contains the address of the object for which the function was called.
For example if you write:
cout << x.volume();
the pointer this in the function volume() will contain the address of x. When you call the function for another object, this will be set to contain the address of that object.
This means that when the data member length is accessed in the function volume() during execution, it is actually referring to this->length, which is the fully-specified reference to the object member that is being used. The compiler takes care of adding the necessary pointer name this to the member names in the function. In other words, the compiler will implement the function as:
double Box::volume()
{
return this->length * this->breadth * this->height;
}
Program 11:
#include<iostream.h>
#include<conio.h>
class example
{
int a,b;
public:
int setvalue(int a,int b)
{
this->a = a;
this->b = b;
}
void sum()
{
int c=a+b;
cout << "sum is " << c;
}
};
void main()
{
clrscr();
int a,b;
example e1;
cout << "enter a and b";
cin >> a >> b;
e1.setvalue(a,b);
e1.sum();
getch();
}
FRIEND FUNCTION
When a data is declared as private inside the class then it is not accessible from outside the class. A function that is not a member or an external class will not be able to access the private data. There are some situations where he/she would need to access private data from non-member functions and external class. For handling such cases the concept of friend function is a useful tool.
What is a friend function?
A friend function is used for accessing the private member of the class. A class can allow non-member functions to access its own private data by making them friends.
How to define and use friend function in C++?
The friend function is written as any other normal function. Declaration of friend function starts with friend keyword. The function definition does not use either the keyword friend or the scope operator ::. The functions that are declared with the keyword friend are known as friend functions. A function can be declared as a friend in any number of classes. A friend function, although not a member function, has full access rights to the private members of the class.
Characteristics of member function
· It is not in the scope of the class to which it has been declared as friend.
· Since it is not in the scope of the class, it cannot be called using the object of that class.
· It can be invoked like a normal function without the help of any object.
· Unlike member functions, it cannot access the member names directly and has to use an object name and dot membership operator with each member name. (e.g. A.x).
· It can be declared either in the public or the private part of a class without affecting its meaning.
· Usually, it has the objects as arguments.
Program 12:
#include<iostream.h>
#include<conio.h>
class sample
{
int a,b;
public:
void setvalue()
{
a=20;
b=10;
}
friend float mean (sample s);
};
float mean(sample s)
{
float ans=(s.a+s.b)/2;
return ans;
}
void main()
{
sample x;
x.setvalue();
cout << "mean is " << mean(x);
getch();}
Program 13:
#include<iostream.h>
#include<conio.h>
class TWO;
class ONE
{
int a;
public:
void first()
{
a=10;
}
friend int great(ONE,TWO);
};
class TWO
{
int b;
public:
void second()
{
b=20;
}
friend int great(ONE,TWO);
};
int great(ONE a1,TWO b1)
{
if(a1.a>b1.b)
return a1.a;
else
return b1.b;
}
void main()
{
ONE one;
TWO two;
one.first();
two.second();
cout << "greater of two numbers is " << great(one,two);
getch();
}
Compiled By: Chaudhary Amit V.
Comments
Post a Comment