Print function for class c++
Solution 1
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;
}
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.
Then you can do:
AutoData myAuto;
std::cout << myAuto << '\n';
Solution 2
What have you tried so far? My approach would be overloading operator<<
, like:
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;
}
Solution 3
You can read this artical to know about friend
and operator <<
,
http://www.cprogramming.com/tutorial/friends.html
In the class AutoData
, you should declare this function:
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;
}
Related videos on Youtube
Pseudo Sudo
Apprentice at the Testing Dojo. Focused on mastering the art of C#.
Updated on August 16, 2020Comments
-
Pseudo Sudo over 3 years
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
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
-
Mike about 10 yearsif you want to print to console just cout << mpg << ":" << cylinders << ":" etc - if you want to store in a string use a stringstream.
-
Pseudo Sudo about 10 yearsbut how would i print out an entire vector of these? so that its mpg1:cylinders1:etc... mpg2:cylinders2:etc...
-
stefaanv about 10 yearsJust iterate over the vector and add newlines between the entries
-
Mike about 10 yearswhat I said would be in its own method, then just iterate through each vector using a ranged based for loop like you would normally.
-
-
Holt about 10 yearsBe careful,
auto
is a reserved keyword in C++ 11. -
Massa about 10 years@Holt -- it slipped my mind... it's 06:30 here, I was probably half-asleep :D
-
Massa about 10 yearsI find the
const&
notation very confusing (distracts from the fact that is the underlyingAutoData
that isconst
, not the reference), but other than that, it seems we started to type the same answer at the same time :D but I was slower, because I tried to dooperator>>
too... Ah, and I would insertstd::endl
inoperator<<
so I could guarantee that I can readcarName
withstd::readline
inoperator>>
... -
Holt about 10 years@Massa I'm used to use
cons&
, but maybe you're right for a beginner it could be confusing. Concerningstd::endl
, I think it shouldn't be in theoperator<<
, because you could want to put multipleAutoData
on the same line, separated by coma / tab. It depends on what you really needs. -
Massa about 10 yearsmy reasoning is that if you don't define a (default?) terminator for
AutoData::carName
and enforce it onoperator<<
, thenoperator>>
cannot know where the record ends. That's why I would've settled for the simpler option of terminating the record withendl
and usingreadline
to read the final string. Other option would be to put the terminator in astatic
member ofAutoData
and matching reads and writes using it....