Skip to main content

this Pointer and Friend Function


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

Popular posts from this blog

Characteristics of a Good Programming Language

Till now there are many high level languages which are very popular, and there are others, which could not become so popular in-spite of being very powerful. There might be many reasons for the success of a language, but one obvious reason is the characteristics of the language. Several characteristics believed to be important with respect to making a programming language good are briefly discussed below. Simplicity A good programming language must be simple and easy to learn and use. For example, BASIC is liked by many programmers only because of its simplicity. Thus, a good programming language should provide a programmer with a clear, simple and unified set of concepts which can be easily grasped. It is also easy to develop and implement a compiler or an interpreter for a programming language that is simple. However, the power needed for the language should not be sacrificed for simplicity. The overall simplicity of a programming language strongly affects the readability of the pr...

Angular 4 and Firebase Authentication: Setup

If you have come to this article, it means you are keen to learn new technology and that too Firebase. I love Firebase because it provides all basic but important features of any web application. Take for example, authentication, which is cumbersome and risky if not implemented with utmost care. And when we have the integration of Angular and Firebase, it becomes a lot easier for a developer to build such crucial modules in less time with minimal efforts. In this article we will create a simple Angular application using Firebase. I am going ahead with the understanding that you know the benefits of Firebase and have little knowledge about it. There are plenty of posts out on the web if you are behind. Step 1: Install Angular CLI (if not installed). npm install -g angular-cli Step 2: Create new Angular 4 project. By default now angular CLI will create Angular 4 project so you need not fret. ng new firebase-authentication Step 3: Check whether the ne...

Switch-case, simple calculator . . .

11.      Write a program to perform some mathematic functions with the help of “switch-case” statement. #include<stdio.h> #include<conio.h> void main() { int a,b,ans; char c; clrscr(); printf("\ Enter a and b:"); scanf("%d%d",&a,&b); printf("\n Enter operator:"); fflush(stdin); scanf("%c",&c); switch(c) { case'+':ans=a+b;       break; case'-':ans=a-b;       break; case'*':ans=a*b;       break; case'/':ans=a/b;       break; } printf("\n ans %d",ans); getch(); } 12.      Print the Fibonacci series #include<stdio.h> #include<conio.h> void main() { int a=1,b=1,s=3,c,ch; clrscr(); printf("how many steps="); scanf("%d",&ch); printf("%5d%5d",a,b); for(s=3;s<=ch;s++) {       c=a+b;       printf("%5d",c);       a=b;    ...