0

The assignmento I have to write is to Derive a class DistSign from class Distance which contains inches and feet as a protected data, to add unary + or – to the distance. Write no argument and two argument constructor.

Here is my program. I don’t know how to add + operator.

#include<iostream.h>
class Distance
{
protected:
int feet;
float inches;

public:
Distance()
{
 feet=0;
 inches=0;
}
Distance(int f,float in)
{
 feet=f;
 inches=in;
}
void display()
{
 cout<<"\nfeet:"<<feet;
 cout<<"\ninches:"<<inches;
 }
};

class DistSign:public Distance
{
  public:

  DistSign(int f,float in):Distance(f,in)
   {}

  void operator-()
  {
     feet*=-1;
    inches*=-1;

  }
3
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by MrSpigot
0

>I don’t know how to add + operator.
Liar. Aside from not terminating the DistSign class (which I assume is simply a copy/paste error), your overloaded operator works just peachy. It can't be used as an rvalue because it returns void, but that's a design decision, not an "I don't know how to do it" issue.

0

If it's just the unary + operator you're worried about I'd say it doesn't have to do anything, so unless you want some non-standard functionality:

void operator+(){}

should be a good partner to your operator-.

...and just stop lying.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.