C ++类的打印功能 [英] Print function for class c++
问题描述
我想为AutoData类编写一个打印函数,其中包含有关汽车的信息。使用此打印功能,理想情况下,我希望打印出包含许多不同类对象的向量。我已经为对象的每个元素编写了get函数,但是我仍然不确定如何使用这些函数编写函数以以下格式打印数据:
I want to write a print function for a class AutoData that has information about cars in it. With this print function, I would ideally like to print out a vector that contains many different class objects. I have already written get functions for each element of the objects, but I am still a bit unsure of how to go about using those to write a function to print out the data in the following format:
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;
};
任何人都可以提供的任何帮助/建议,将不胜感激!谢谢
Any help/advice anyone can provide would be very much appreciated!! Thanks
推荐答案
如果您想执行 std :: cout<< AutoData();
,您需要重载输出流运算符 operator<<
:
If you want to be able to do std::cout << AutoData();
, you need to overload the output stream operator operator<<
:
std::ostream& operator<< (std::ostream &out, AutoData const& data) {
out << data.getmpg() << ':';
out << data.getcylinders() << ':';
// and so on...
return out;
}
此函数不是成员函数,因为每个属性都有getter ,则不必将该函数声明为类的 friend
。
This function is not a member function, and since you have getters for each attribute, you do not have to declare this function as friend
of your class.
然后您可以这样做:
AutoData myAuto;
std::cout << myAuto << '\n';
这篇关于C ++类的打印功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!