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;

  }

Recommended Answers

All 2 Replies

>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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.