c ++类的打印功能

时间:2014-04-23 09:06:44

标签: c++ class object vector iostream

我想为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;
};

任何人都可以提供的帮助/建议将非常感谢!!感谢

3 个答案:

答案 0 :(得分:9)

如果您希望能够执行std::cout << AutoData();,则需要重载输出流运算符operator<<

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';

答案 1 :(得分:3)

到目前为止你尝试了什么?我的方法是重载operator<<,如:

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

&#34;阅读&#34;同样的事情功能,如:

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;
}

答案 2 :(得分:0)

您可以阅读此文章,了解friendoperator <<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;
}
相关问题