反向函数不反转类实例化的引用字符串

reverse function doesn't reverse the referenced string of a class instantiation

本文关键字:引用 字符串 实例化 函数      更新时间:2023-10-16

我正在尝试从funnynumber类中扭转字符串。问题在于,当我在F2上的MAIN中调用方法反向时,它不会反转F2字符串。但是,当我从反向方法实现中打印出相反的字符串时,它可以起作用。我也超载了<<运算符以在Funnynumber类中继承的类号码中打印出字符串值。任何帮助,将不胜感激。

#ifndef NUMBER_H
#define NUMBER_H
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
class Number {
public:
  // Make a number with value 0
  Number();
  // Make a number with value val
  Number(string val);
  // Get the number's value
  virtual string getValue() const;
  // Print this number to the stream
  virtual void print(ostream& stream) const;
  // Read this number from the stream
  virtual void read(istream& stream);
  // Overload the insertion operator
  friend ostream& operator <<(ostream& outs, const Number& n);
  // Overload the extraction operator
  friend istream& operator >> (istream& ins, Number& n);
protected:
  string value;
};
#endif 

Number::Number()
{
  value = "";
}
Number::Number(string args)
{
  value = args;
}
string Number::getValue()const
{
  return value;
}
ostream& operator <<(ostream& outs, const Number& n)
{
  n.print(outs);
  return outs;
}
void Number::print(ostream& stream)const
{
  stream << getValue();
}
void Number::read(istream& stream)
{
  stream >> value;
}
istream& operator >> (istream& ins, Number& n)
{
  n.read(ins);
  return ins;
}
#ifndef FUNNYNUMBER_H
#define FUNNYNUMBER_H
#include<iostream>
#include<string>
#include"Number.h"
#include<algorithm>

using namespace std;
class FunnyNumber : public Number 
{
public:
    FunnyNumber();
    FunnyNumber(string val);
    virtual string operator+(const FunnyNumber &other)const;
    virtual bool operator==(const FunnyNumber &other)const;
    void reverse();
    int find_first_not_this(char a);
protected:
    string value;
};
#endif // !FUNNYNUMBERS_H
FunnyNumber::FunnyNumber()
{
    value = "";
}
FunnyNumber::FunnyNumber(string val) : Number(val)
{
    value = val;
}
string FunnyNumber::operator+ (const FunnyNumber& other)const
{
    return getValue() + other.getValue();
}
int FunnyNumber::find_first_not_this(char a)
{
    int pos = 0;
    for(int i = 0; i < value.length(); i++)
    {
        if(value[i] != a)
        {
            pos = i;
            return pos;
        }
    }
    return pos;
}
bool FunnyNumber::operator==(const FunnyNumber& other)const
{
    bool isEqual = true;
    for (int i = 0; i < other.getValue().length(); i++) 
    {
        bool found = false;
        for (int j = 0; j < getValue().length(); j++) 
        {
            if(getValue()[j] == other.getValue()[i])
            {
                found = true;
                break;
            }
        }
        if(!found)
        {
            isEqual = found;
            return isEqual;
        }
    }
    return isEqual;
}
void FunnyNumber::reverse()
{
    std::reverse(value.begin(), value.end());
    value.erase(0, find_first_not_this('0'));
}

#include <iostream>
#include<string.h>
#include "FunnyNumber.h"
using namespace std;
int main()
{
   FunnyNumber f2;
   f2 = FunnyNumber("223");
   f2.reverse();
   cout<<"Reversed value "<<f2<<endl;
   system("pause");
   return 0;
}

输出为223而不是322

您的FunnyNumber将值存储两次,一次在类型Number的子标题中,一次在string FunnyNumber::value中。

您的reverse功能会修改第二个功能,但对Number基本子对象没有任何影响。然后,您调用的唯一输出功能是在Number基本子对象上使用,并且对string FunnyNumber::value一无所知。这就是为什么印刷不是逆转的结果。

超载运算符&lt;&lt;是数字类上的朋友函数,而朋友函数未继承。

class Number {
public:
  // Make a number with value 0
  Number();
  // Make a number with value val
  Number(string &val);
  // Get the number's value
  virtual string getValue() const;
  // Print this number to the stream
  virtual void print(ostream& stream) const;
  // Read this number from the stream
  virtual void read(istream& stream);
  // Overload the insertion operator
  friend ostream& operator <<(ostream& outs, const Number& n);
  // Overload the extraction operator
  friend istream& operator >> (istream& ins, Number& n);
protected:
  string *value;
};
Number::Number()
{
    value = NULL;
}
Number::Number(string &args)
{
  value = &args;
}
string Number::getValue()const
{
  return *value;
}
ostream& operator <<(ostream& outs, const Number& n)
{
  n.print(outs);
  return outs;
}
void Number::print(ostream& stream)const
{
  stream << getValue();
}
void Number::read(istream& stream)
{
  stream >> *value;
}
istream& operator >> (istream& ins, Number& n)
{
  n.read(ins);
  return ins;
}