May 10, 2014 at 11:08am UTC
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
class Car
{
private:
string reportingMark;
int carNumber;
string kind;
bool loaded;
string destination;
public:
// make a defult constructor
Car();
Car(const string rm, const int nb, const string kd, const bool ld, const string dt);
// copy constructor p817
Car ( const Car &obj)
{
reportingMark = obj.reportingMark;
carNumber = obj.carNumber;
kind = obj.kind;
loaded = obj.loaded;
destination = obj.destination;
}
~Car();
void setUp (const string ,const int ,const string ,
const bool , const string);
bool operator ==(const Car &) const;
void output();
};
Car::Car ()
{
reportingMark = " ";
carNumber = 0;
kind = "other";
loaded = false;
destination = "NONE";
}
Car::Car (const string rm, const int nb, const string kd, const bool ld, const string dt)
{
setUp(rm ,nb ,kd ,ld ,dt );
}
Car::~Car()
{
cout<<"";
}
int main()
{
Car car1("SP", 34567, "box", true, "Salt Lake City");
Car car2(car1);
Car car3;
cout << "*********************************" <<endl;
cout << "Contents of car1: ";
car1.output();
cout << "*********************************" <<endl;
cout << "Contents of car2: ";
car2.output();
cout << "*********************************" <<endl;
cout << "Contents of car3: ";
car3.output();
cout << "*********************************" <<endl;
if (car1 == car2)
cout << "car1 is the same car as car2\n";
else
cout << "car1 is not the same car as car2\n";
if (car2 == car3)
cout << "car2 is the same car as car3\n";
else
cout << "car2 is not the same car as car3\n";
return 0;
}
/******output********************************************
********************************************************/
void Car::output()
{
cout<<"\n\nreportingMark "<<left<<setw(6)<<reportingMark<<endl;
cout<<"carNumber "<<left<<setw(6)<<carNumber<<endl;
cout<<"kind "<<left<<setw(6)<<kind<<endl;
if (loaded == true)
cout<<"loaded "<<left<<setw(6)<<"true"<<endl;
else
cout<<"loaded "<<left<<setw(6)<<"false"<<endl;
cout<<"destination "<<left<<setw(6)<<destination<<endl;
}
/******setup********************************************
107
********************************************************/
void Car::setUp(const string rm, const int nb, const string kd, const bool ld, const string dt)
{
reportingMark = rm;
carNumber = nb;
kind = kd;
loaded = ld;
destination = dt;
}
bool operator ==(Car A,Car B)/>
{
if(A.reportingMark =B.reportingMark && A.carNumber = B.carNumber)
return true;
else
return false;
}
May 10, 2014 at 11:19am UTC
Please use code tags when posting to this site - find the '<>' button, select all your code and press it.
Your problem is partly the '/>' you have at the end of your operator== function (doing XML half way through?) and also the fact that operator== isn't allowed to access the private variables of your class. Either add it as a friend to your class or add the function to your class, either way.
May 10, 2014 at 11:31am UTC
could u please write for me what should i change? am confused u explain but if u can write the code it is better .
May 10, 2014 at 4:46pm UTC
@sarak786
And leave off both of these 'A.' in if (A.reportingMark =B.reportingMark && A.carNumber = B.carNumber)
Also, you should be using double equals, since you are comparing, not assigning.
Last edited on May 10, 2014 at 4:51pm UTC