answersLogoWhite

0

There is no need to use a class to swap two numbers. A template function is all you really need. The following example includes both a template function and a class template with a static method. The template function is clearly the easier of the two to use.

#include<iostream>

template<typename T>

void swap(T& x, T&y)

{

T temp=x; x=y; y=temp;

}

template<typename T>

class foo

{

public:

static void swap(T& x, T& y){T temp=x; x=y; y=temp;}

};

int main()

{

int a=40, b=2;

std::cout<<"a is "<<a<<", b is "<<b<<std::endl;

swap(a,b);

std::cout<<"a is "<<a<<", b is "<<b<<std::endl;

foo<int>::swap(a,b);

std::cout<<"a is "<<a<<", b is "<<b<<std::endl;

}

Example output:

a is 40, b is 2

a is 2, b is 40

a is 40, b is 2

User Avatar

Wiki User

11y ago

What else can I help you with?

Continue Learning about Engineering
Related Questions

Can somebody give me the program to find simple interest using friend function in c plus plus?

Here is an example program: class obj{ public: float p,n,r,si; friend void calc( obj temp); }; void calc( obj temp){ si = (p*n*r)/100; } The initialization and function calling is essential.


Explain the advantages and disadvantages of friend functions?

by: THE DJ AKwww.the-dj-ak.webs.comwww.thedjak.co.nrwww.thedjak.webs.comWhat is a Friend Function?A friend function is a special function in c++ which inspite of not being member fuctionof a class has privalage to access private and protected data of a class.A friend function is a non member function of a class, that is declared as a friend usingthe keyword "friend" inside the class. By declaring a function as a friend, all the accesspermissions are given to the function.A friend function is used for accessing the non-public members of a class.A class can allow non-member functions and other classes to access its ownprivate data, by making them friends. Thus, a friend function is an ordinaryfunction or a member of another class.Need for Friend Function:As discussed in the earlier sections on access specifiers, when a datais declared as private inside a class, then it is not accessible from outsidethe class. A function that is not a member or an external class will notbe able to access the private data. A programmer may have a situation wherehe or she would need to access private data from non-member functions andexternal classes. For handling such cases, the concept of Friend functionsis a useful tool.How to define and use Friend Function in C++:The friend function is written as any other normal function, exceptthe function declaration of these functions is preceded with the keywordfriend. The friend function must have the class to which it is declared asfriend passed to it in argument.Some important points to note while using friend functions in C++:* The keyword friend is placed only in the function declaration of the friendfunction and not in the function definition..* It is possible to declare a function as friend in any number of classes..* When a class is declared as a friend, the friend class has access to theprivate data of the class that made this a friend..* A friend function, even though it is not a member function, would have therights to access the private members of the class..* It is possible to declare the friend function as either private or public..* The function can be invoked without the use of an object. The friend functionhas its argument as objects, seen in example below.properties of friend function:1. if a function to be made friend of a class than it should be declared within bodyof the class priciding with keyword friend.2.freind function never breaks the security.3.it should not be defined in name of class nor scope resolution operator is used in it'sdefination even the keyword freind is also not used while defining friend function.4.when friend function is called nither name of object nor dot operator is used. howeverit may accept the object as argument who's value it want's to access.5.it doen't matter in which section of the class we have declared a freind function.Example to understand the friend function:#includeclass exforsys{private:int a,b;public:void test(){a=100;b=200;}friend int compute(exforsys e1)//Friend Function Declaration with keyword friend and with the object of class exforsys to which it is friend passedto it};int compute(exforsys e1){//Friend Function Definition which has access to private datareturn int(e1.a+e2.b)-5;}main(){exforsys e;e.test();cout


What is the difference between friend function and normal member function?

We can access a Friend function from any other class in which friend function is introduced or declared even if the other class is not a member of first class. But when we use normal member function, we can have its access only in the derived classes of the first class. This is the basic difference between a friend function and a normal member function.


What is a Superstition about bridge?

If you say good-bye to a friend on a bridge, you will never see each other again.


How to write a letter to a friend asking him about summer vacations outing program?

Writing a letter to a friend asking him about a summer vacations outing program is simple. Ask him when and where the program will be.


Can you get 3ds ambassador program from a friend?

no.


Can a friend function be friend with more than one class?

yes


What is the difference between friend function and inheritance in c plus plus?

There is no such thing. When declaring a friend function only the explicitly-scoped friend is granted private access. The friend function may well be declared virtual within its own class but none of its overrides are granted access unless they are explicitly granted access.


What is friend?

A TV program/Series!


Will you get banned from wow if you recruit a friend?

Of course not. Blizzard will not ban you for doing a program that they create. Recruit a friend is a Blizzard program, so there is no way they can ban you.


Which friend adder programs use friend id numbers for MySpace?

== ==


How do you make a program that works with Facebook?

:not answer: I have a friend who is rarely on is there a way i can make a program that can alert me when he is..?