【问题标题】:Print function for class c++c++ 类的打印函数
【发布时间】:2014-06-08 00:16:42
【问题描述】:

我想为 AutoData 类编写一个打印函数,其中包含有关汽车的信息。使用此打印功能,理想情况下,我想打印出一个包含许多不同类对象的向量。我已经为对象的每个元素编写了 get 函数,但我仍然有点不确定如何使用这些函数编写一个函数来打印以下格式的数据:

mpg:cylinders:displacement:horsepower:weight:acceleration:modelYear:origin:carName

例如:

10.0:8:360.0:215.0:4615.:14.0:70:1:ford f250
10.0:8:307.0:200.0:4376.:15.0:70:1:chevy c20
11.0:8:318.0:210.0:4382.:13.5:70:1:dodge d200

班级是:

#include <string>
#include <vector>
#include <iostream>

using namespace std;

class AutoData {

public:
    AutoData()
    {
        mpg = 0;
        cylinders = 0;
        displacement = 0;
        horsepower = 0;
        weight = 0;
        acceleration = 0;
        modelYear = 0;
        origin = 0;
        carName = "";
    }

    AutoData( const AutoData & rhs)
    {
        setAuto(rhs.mpg, rhs.cylinders, rhs.displacement, rhs.horsepower, rhs.weight, rhs.acceleration, rhs.modelYear, rhs.origin, rhs.carName);
    }

    void setAuto(float mp, int cy, float di, float ho, float we, float ac, int mo, int o, string ca)
    {
        mpg = mp;
        cylinders = cy;
        displacement = di;
        horsepower = ho;
        weight = we;
        acceleration = ac;
        modelYear = mo;
        origin = o;
        carName = ca;
    }

    const float & getmpg( ) const
    {
        return mpg;
    }

    const int & getcylinders( ) const
    {
        return cylinders;
    }

    const float & getdisplacement( ) const
    {
        return displacement;
    }

    const float & gethorsepower( ) const
    {
        return horsepower;
    }

    const float & getweight( ) const
    {
        return weight;
    }

    const float & getacceleration( ) const
    {
        return acceleration;
    }

    const int & getmodelYear( ) const
    {
        return modelYear;
    }

    const int & getorigin( ) const
    {
        return origin;
    }

    const string & getcarName( ) const
    {
        return carName;
    }

    bool operator == (const AutoData & rhs ) const
    {
        if( getmpg( ) == rhs.getmpg( ) )
        {
            return gethorsepower( ) == rhs.gethorsepower( );
        }

        else
        {
            return false;
        }
    }

    bool operator > ( const AutoData & rhs ) const
    {
        if( rhs.getmpg( ) > getmpg( ) )
        {
            return true;
        }

        else if( getmpg( ) == rhs.getmpg( ) )
        {
            if( rhs.gethorsepower( ) > gethorsepower( ) )
            {
                return true;
            }
        }

        else
        {
            return false;
        }
    }


private:
    float mpg;
    int cylinders;
    float displacement;
    float horsepower;
    float weight;
    float acceleration;
    int modelYear;
    int origin;
    string carName;
};

任何人都可以提供的任何帮助/建议将不胜感激!!谢谢

【问题讨论】:

  • 如果你想打印到控制台,只需 cout
  • 但我将如何打印出这些的整个向量?这样它的 mpg1:cylinders1:etc... mpg2:cylinders2:etc...
  • 只需遍历向量并在条目之间添加换行符
  • 我说的是它自己的方法,然后像往常一样使用基于范围的 for 循环遍历每个向量。

标签: c++ class object vector iostream


【解决方案1】:

如果你想能够做到std::cout &lt;&lt; AutoData();,你需要重载输出流操作符operator&lt;&lt;

std::ostream& operator<< (std::ostream &out, AutoData const& data) {
    out << data.getmpg() << ':';
    out << data.getcylinders() << ':';
    // and so on... 
    return out;
}

这个函数不是一个成员函数,因为你有每个属性的getter,你不必将这个函数声明为你的类的friend

那么你可以这样做:

AutoData myAuto;
std::cout << myAuto << '\n';

【讨论】:

  • 我发现const&amp; 符号非常混乱(分散了底层AutoDataconst 的事实,而不是参考),但除此之外,似乎我们开始同时输入相同的答案:D 但我的速度较慢,因为我也尝试过operator&gt;&gt;...啊,我会在operator&lt;&lt; 中插入std::endl,这样我可以保证我可以阅读@987654332 @ 与std::readlineoperator&gt;&gt; ...
  • @Massa 我习惯使用cons&amp;,但也许你适合初学者,这可能会让人感到困惑。关于std::endl,我认为它不应该在operator&lt;&lt; 中,因为您可能希望将多个AutoData 放在同一行,用逗号/制表符分隔。这取决于你真正需要什么。
  • 我的理由是,如果您没有为AutoData::carName 定义(默认?)终止符并在operator&lt;&lt; 上强制执行,那么operator&gt;&gt; 将无法知道记录的结束位置。这就是为什么我会选择使用endl 终止记录并使用readline 读取最终字符串的更简单选项。其他选择是将终止符放在AutoDatastatic 成员中,并使用它匹配读取和写入......
【解决方案2】:

到目前为止,您尝试过什么?我的方法是重载operator&lt;&lt;,例如:

std::ostream& operator<<(std::ostream& out, const AutoData& dasAuto) {
  return out << dasAuto.getmpg() << ':' << dasAuto.getcylinders() <<
    /* insert everthing in the desired order here */
    std::endl;
}

“阅读”功能也是如此,例如:

std::istream& operator>>(std::istream& in, AutoData& dasAuto) {
  float mpg;
  int cylinders;
  float displacement;
  float horsepower;
  float weight;
  float acceleration;
  int modelYear;
  int origin;
  string carName;
  char separator;
  const char SEP = ':';

  if( !(in >> mpg >> separator) || (separator != SEP) ) return in;
  if( !(in >> cylinders >> separator) || (separator != SEP) ) return in;
    /* rinse, repeat */
  if( !std::getline(in, carName) ) return in;
  dasAuto.setAuto(mpg, cylinders /*, etc etc */);
  return in;
}

【讨论】:

  • 小心,auto 是 C++ 11 中的保留关键字。
  • @Holt -- 我突然忘记了......现在是 06:30,我可能半睡半醒 :D
【解决方案3】:

你可以阅读这篇文章来了解friendoperator &lt;&lt;http://www.cprogramming.com/tutorial/friends.html

AutoData类中,你应该声明这个函数:

friend ostream& operator<< (ostream& out, const AutoData& obj); 

在类之外,你应该像这样定义这个函数:

ostream& operator<< (ostream& out, const AutoData& obj)
{
    out<<obj.mpg<<":";
        //do what you want
    return out;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-06-14
    • 2010-10-07
    • 2020-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多