C++ - Friend Function , Operator Overloading , Templates

FRIEND FUNCTIONS

 

·        The private members cannot be accessed from outside the class. (i.e.) a non-member function cannot have an access to the private data of a class.

 

·        It is a non-member function but can have access to the member of class through objects.

 

·        To make a non-member can have access to the members of a class declare it inside the class using the keyword friend.

Syntax

 

          Class class-name

          {

                   Friend return-type fun-name(objects);

}

 

Example

 

#include<iostream.h>

#include<conio.h>

class sample

{

int a;

public:

void get(int x1)

{

a=x1;

}

friend int cube(sample s);

};

int cube(sample s)

{

return(s.a*s.a*s.a);

}

void main()

{

sample x;

int a;

cout<<"Enter a value=";

cin>>a;

x.get(a);

cout<<"\n Cube="<<cube(x);

getch();

}

 

OPERATOR OVERLOADING

 

In c++, we can overload most operators so that they perform special operations relative to classes that we create

 

 

 

Syntax

 

Returntype classname :: operator op(ar-list)

{

Function body

}

 

Types of Operator Overloading

 

Unary Operator Overloading

 

In unary Operator Overloading argument list will be empty.

 

#include<iostream.h>

#include<conio.h>

class simple

{

int x,y,z;

public:

void getdata(int a,int b,int c);

void display(void);

void operator-();

};

 

void simple :: getdata(int a,int b,int c)

{

x=a;

y=b;

z=c;

}

 

void simple :: display()

{

cout<<x<<" ";

cout<<y<<" ";

cout<<z<<" ";

cout<<"\n";

}

void simple :: operator-()

{

x=-x;

y=-y;

z=-z;

}

 

void main()

{

simple s;

clrscr();

s.getdata(10,20,30);

s.display();

-s;

s.display();

getch();

}

 

Binary Operator Overloading

 

In binary operator overloading argument list will contain one parameter and one return object.

#include<iostream.h>

#include<conio.h>

class sum

{

int x,y;

public:

sum()

{

}

 

sum(int a,int b)

{

x=a;

y=b;

}

 

void display();

sum operator+(sum);

};

 

sum sum :: operator+(sum x1)

{

sum g;

g.x=x+x1.x;

g.y=y+x1.y;

return(g);

}

 

void sum :: display()

{

cout<<"\n X="<<x;

cout<<"\n Y="<<y;

}

 

void main()

{

sum s1(10,20);

sum s2(20,20);

clrscr();

sum s3;

cout<<"\n S1 Value";

s1.display();

cout<<"\n S2 Value";

s2.display();

s3=s1+s2;

cout<<"\n S3 Value";

s3.display();

getch();

}

 

TEMPLATES

 

Z It enables us to define generic classes and functions and thus provides support for generic programming.

 

Z A template can be used to create a family of classes or functions.

 

Z It can be used considered as a king of macro. When an object of a specific type is defined for actual use, the template definition for that class is substituted with the required data type.

 

Z Since a template is defined with a parameter that would be replaces by a specified data type at the time of actual use of the class or function, the templates are sometimes called parameterized classes or functions

 

 

Example

 

Class Template

 

#include<iostream.h>

#include<conio.h>

template<class gp>

class add

{

gp a,b,c;

public:

add(gp x,gp y)

{

a=x;

b=y;

}

void sum()

{

c=a+b;

cout<<"\n a="<<a;

cout<<"\n b="<<b;

cout<<"\n c="<<c;

}

};

void main()

{

clrscr();

add<int>a1(10,20);

add<float>a2(3.5,3.2);

a1.sum();

a2.sum();

getch();

}

 

Function Template

 

#include<iostream.h>

#include<conio.h>

template<class T>

void swap(T &x,T &y)

{

cT t=x;

x=y;

y=t;

}

void main()

{

int m,n;

float a,b;

clrscr();

cout<<"Enter the value of m and n=";

cin>>m>>n;

cout<<"\nEnter the value of a and b=";

cin>>a>>b;

cout<<"\n Before Swapping";

cout<<"\n M="<<m;

cout<<"\n N="<<n;

swap(m,n);

cout<<"\n After Swapping";

cout<<"\n M="<<m;

cout<<"\n N="<<n;

 

cout<<"\n Before Swapping";

cout<<"\n A="<<a;

cout<<"\n B="<<b;

 

swap(a,b);

 

cout<<"\n After Swapping";

cout<<"\n A="<<a;

cout<<"\n B="<<b;

getch();

}

No comments:

Post a Comment