Printing with "ostream &operator<<( ostream &out, const Vehicle &v )"

I am trying to get the data stored in my vehicle object to print like this:

Vehicle
Number of doors: 2
Number of cylinders: 6
Transmission type: 3
Colour: blue
Fuel level: 14.6


But I don't know how to call the function :

ostream &operator<<( ostream &out, const Vehicle &v ) at line22 in vehicle.cpp

from my main method.

How can I get the values of the vehicle object to print?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// vehicle.cpp
#include <iostream>

using std::cout;
using std::endl;

#include "vehicle.h"

// constructor
Vehicle::Vehicle( const int doors, const int cylinders,
   string color, double initialFuel, 
   const int transmission ):
   /* Write the body for Vehicle's constructor */
	   numberOfDoors(doors),
	   numberOfCylinders(cylinders),
	   vehicleColor(color),
	   fuelLevel(initialFuel),
	   transmissionType(transmission)
   {
   } // end class Vehicle constructor

// function operator<< definition
ostream &operator<<( ostream &out, const Vehicle &v )
{
   out << v.className << "\n"
	       << "\tNumber of doors: " << v.numberOfDoors
	    << "\n\tNumber of cylinders: " << v.numberOfCylinders
	    << "\n\tTransmission type: " << v.transmissionType
 	   << "\n\tColor: " << v.vehicleColor
	    << "\n\tFuel level: " << v.fuelLevel << endl;

   return out;

} // end function operator<<

/* Write definition for setColor */

void Vehicle::setColor(string col){
	vehicleColor =  "blue";
}

// function setFuelLevel definition
void Vehicle::setFuelLevel( double amount )
{
   // assume 20 gallons is full tank
   if ( amount > 0.0 && amount <= 20.0 ) 
      fuelLevel = amount;

   else
      fuelLevel = 5.0;

} // end function setFuelLevel

// return color
string Vehicle::getColor() const
{   
   return vehicleColor;

} // end function getColor
 
// return fuelLevel
double Vehicle::getFuelLevel() const
{
   return fuelLevel;

} // end function getFuelLevel

// return transmissionType
int Vehicle::getTransmissionType() const
{
   return transmissionType;

} // end function getTransmissionType
   
// return numberOfDoors
int Vehicle::getNumberOfDoors() const
{
   return numberOfDoors;

} // end function getNumberOfDoors

// return numberOfCylinders
int Vehicle::getNumberOfCylinders() const
{
   return numberOfCylinders;   

} // end function getNumberOfCylinders

// function setClassName definition
void Vehicle::setClassName( string newName )
{
   className = newName;

} // end function setClassName

// return className
string Vehicle::getClassName() const  
{
   return className;

} // end function getClassName 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// vehicle.h
#ifndef VEHICLE_H
#define VEHICLE_H

#include <iostream>

using std::ostream;

#include <string>

using std::string;

// class Vehicle definition
class Vehicle {
   friend ostream& operator<<( ostream &, const Vehicle & );

public:
   Vehicle( const int, const int, string, double, const int ); 
   
   void setColor( string );
   string getColor() const;

   void setFuelLevel( double );
   double getFuelLevel() const;

   void setClassName( string );
   string getClassName() const;

   int getTransmissionType() const;
   int getNumberOfDoors() const;
   int getNumberOfCylinders() const; 

private:
   const int numberOfDoors;
   const int numberOfCylinders;
   string vehicleColor;
   double fuelLevel;
   const int transmissionType;
   string className;

}; // end class Vehicle

#endif // VEHICLE_H



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// driver for inheritance hierarchy
#include <iostream>

using std::cout;
using std::endl;

#include "vehicle.h"
//#include "taxi.h"
//#include "truck.h"

int main()
{
   Vehicle car( 2, 6, "blue", 14.6, 3 );
   //Taxi cab( 3.3 );
   //Truck mack( 7.54 );

   /* Write code to indicate that mack is carrying cargo */
   //mack.setCargo(true);
   /* Write code to print all objects in the Vehicle
      hierarchy */
   operator<<( ostream &out, Vehicle );//not printing to console??
   return 0;

} // end main


Last edited on
const members can only be initialized in the initializer list, like so:
1
2
3
4
5
6
7
8
9
10
11
12
// constructor
Vehicle::Vehicle( const int doors, const int cylinders,
   string color, double initialFuel, 
   const int transmission ) : // Note
	   numberOfDoors(doors),
	   numberOfCylinders(cylinders),
	   vehicleColor(color),
	   fuelLevel(initialFuel),
	   transmissionType(transmission)
   /* Write the body for Vehicle's constructor */
{
} // end class Vehicle constructor 
Thanks for the speedy reply coder777. That worked great! :)
I am trying to get the data stored in my vehicle object to print like this:

Vehicle
Number of doors: 2
Number of cylinders: 6
Transmission type: 3
Colour: blue
Fuel level: 14.6


But I don't know how to call the function :

ostream &operator<<( ostream &out, const Vehicle &v ) at line22 in vehicle.cpp

from my main method
But I don't know how to call the function :

ostream &operator<<( ostream &out, const Vehicle &v ) at line22 in vehicle.cpp

from my main method
Report
Like so:
1
2
3
Vehicle car( 2, 6, "blue", 14.6, 3 );
...
cout << car << endl;
Thank you so much coder777. You saved the day again. :) I spent hours googling, reading and researching trying to figure that one line of code.
Topic archived. No new replies allowed.