C++类的打印函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23239646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Print function for class c++
提问by Pseudo Sudo
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:
我想为 AutoData 类编写一个打印函数,其中包含有关汽车的信息。使用此打印功能,我希望打印出一个包含许多不同类对象的向量。我已经为对象的每个元素编写了 get 函数,但我仍然有点不确定如何使用这些函数来编写一个函数以按以下格式打印数据:
mpg:cylinders:displacement:horsepower:weight:acceleration:modelYear:origin:carName
For example:
例如:
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
The class is:
班级是:
#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
任何人都可以提供的任何帮助/建议将不胜感激!!谢谢
回答by Holt
If you want to be able to do std::cout << AutoData();
, you need to overload the output stream operator operator<<
:
如果你想能够做到std::cout << AutoData();
,你需要重载输出流操作符operator<<
:
std::ostream& operator<< (std::ostream &out, AutoData const& data) {
out << data.getmpg() << ':';
out << data.getcylinders() << ':';
// and so on...
return out;
}
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.
这个函数不是一个成员函数,因为你有每个属性的 getter,你不必friend
在你的类中声明这个函数。
Then you can do:
然后你可以这样做:
AutoData myAuto;
std::cout << myAuto << '\n';
回答by Massa
What have you tried so far? My approach would be overloading operator<<
, like:
你都尝试了些什么?我的方法是重载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;
}
And the same thing for the "reading" function, like:
“阅读”功能也是如此,例如:
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;
}
回答by BlackMamba
You can read this artical to know about friend
and operator <<
,
http://www.cprogramming.com/tutorial/friends.html
你可以阅读这篇文章来了解friend
和operator <<
,
http://www.cprogramming.com/tutorial/friends.html
In the class AutoData
, you should declare this function:
在类中AutoData
,你应该声明这个函数:
friend ostream& operator<< (ostream& out, const AutoData& obj);
outside the class, you should define this function like this:
在类之外,你应该像这样定义这个函数:
ostream& operator<< (ostream& out, const AutoData& obj)
{
out<<obj.mpg<<":";
//do what you want
return out;
}