Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.

# operator overloadin plus friend function

4 replies to this topic

### #1 comet@earth

comet@earth

• Members
• 170 posts
• OFFLINE
•
• Local time:01:13 AM

Posted 04 August 2012 - 06:42 AM

Define two classes to store distance. One of the class should store distance in meters and cms and other should store distance in feet and inches. Take two distances one for each class from user and compute sum and difference between them as per users’ choice. Display the answer in Unit provided by user. Use friend functions, function overloading. Pass objects as
arguments and return object as a result.

```#include<iostream>
#include<conio.h>

using namespace std;
class Distance2;
class Distance1
{
private:

int feet,inches,cms,m;

public:
friend class Distance2;

Distance1()
{
feet=0;
inches=0;
cms=0;
m=0;
}

Distance1(int f,int i)
{
feet=f;
inches=i;
}

void convert()
{
int temp;
temp=feet*12+inches;
cms=int(temp/2.54);
while(cms>=100)
{
m=m+1;
cms-=100;
}

}

int returncms()
{
return cms;
}

int returnm()
{
return m;
}

void display()
{
cout<<feet<<"feet"<<inches<<"inches";
}

friend Distance1 operator+(Distance2);
};

Distance1 operator+(Distance2 d1)
{
Distance1 d2;
d2.inches=inches+d1.returninches();
d2.feet=feet+d1.returnfeet();

return d2;

}

class Distance2
{

private:

int feet,inches,cms,m;

public:

Distance2()
{
feet=0;
inches=0;
cms=0;
m=0;
}

Distance2(int m,int cms)
{
this.m=m;
this.cms=cms;
}

void convert()
{
int temp;
temp=m*100+cms;
inches=int(temp*2.54);
while(inches>=12)
{
inches-=12;
feet=feet+1;
}

}

int returnfeet()
{
return feet;
}

int returninches()
{
return inches;
}

void display()
{
cout<<m<<"meters"<<cms<<"centimeters";
}

friend Distance2 operator+(Distance1);

};

Distance2 operator+(Distance1 d1)
{
Distance2 d2;
d2.cms=cms+d1.returncms();
d2.m=m+d1.returnm();

return d2;

}

int main(void)
{
int a,b;

cout<<"Enter two values";
cin>>a;
cin>>b;

Distance1 d1(a,<img src='http://www.bleepingcomputer.com/forums/public/style_emoticons/<#EMO_DIR#>/cool.gif' class='bbc_emoticon' alt='B)' />;
d1.convert();
Distance2 d2(a,<img src='http://www.bleepingcomputer.com/forums/public/style_emoticons/<#EMO_DIR#>/cool.gif' class='bbc_emoticon' alt='B)' />,d3;

d3=d2+d1;
d3.display();

Distance2 d4(a,<img src='http://www.bleepingcomputer.com/forums/public/style_emoticons/<#EMO_DIR#>/cool.gif' class='bbc_emoticon' alt='B)' />;
d1.convert();
Distance1 d5(a,<img src='http://www.bleepingcomputer.com/forums/public/style_emoticons/<#EMO_DIR#>/cool.gif' class='bbc_emoticon' alt='B)' />,d6;

d6=d5+d4;
d6.display();

getch();
return 0;
}

```

Found this question in a manual.Please guide me through this.Will be really grateful

Edited by comet@earth, 04 August 2012 - 06:44 AM.

### #2 Billy O'Neal

Billy O'Neal

Visual C++ STL Maintainer

• Malware Response Team
• 12,304 posts
• OFFLINE
•
• Gender:Male
• Location:Redmond, Washington
• Local time:12:43 PM

Posted 04 August 2012 - 10:21 AM

Erm, that code seems to do what the question asked (but I didn't look too closely). What are you really asking here?

Billy3
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)

### #3 comet@earth

comet@earth
• Topic Starter

• Members
• 170 posts
• OFFLINE
•
• Local time:01:13 AM

Posted 04 August 2012 - 11:10 AM

There are a few errors(around 10 of them).If possible please run the code in dev c++ and help me debug the program.Always grateful.Thank you for replying.

### #4 Billy O'Neal

Billy O'Neal

Visual C++ STL Maintainer

• Malware Response Team
• 12,304 posts
• OFFLINE
•
• Gender:Male
• Location:Redmond, Washington
• Local time:12:43 PM

Posted 04 August 2012 - 05:50 PM

Dev C++ is not a compiler. It ships with an extremely old copy of GCC which should not be used for new code. It's entirely possible that using a recent compiler will either compile the code (if it is standard compliant), or give you better error messages to help you ferret out the problem.

We're not going to do your homework for you -- take some time to work through the error messages. If you're stuck on particular messages post the message you get here, and we might be able to help. But first please stop using the copy of MinGW that Dev CPP ships with -- either get your hands on a more recent copy of MinGW or try Microsoft's compiler (both of which are free).

(While you're at it you might want to switch to Code::Blocks or something like that as an editor given that Dev CPP hasn't been touched in some 10 years and is still a very buggy editor....)

Billy3
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)

### #5 comet@earth

comet@earth
• Topic Starter

• Members
• 170 posts
• OFFLINE
•
• Local time:01:13 AM

Posted 14 August 2012 - 09:44 AM

I got it.Thank You for replying

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users