In: Computer Science
/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++,
Python, Java, PHP, Ruby, Perl,
C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly,
HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.
*******************************************************************************/
#include <iostream>
using namespace std;
class Vehicle{
int numWheels;
int numDoors;
public:
Vehicle(int numWheels=0,int numDoors=0)
{
this->numWheels=numWheels;
this->numDoors=numDoors;
}
Vehicle(const Vehicle& v)
{
this->numWheels=v.numWheels;
this->numDoors=v.numDoors;
}
//1
Vehicle operator=(const Vehicle& v)
{
this->numWheels=v.numWheels;
this->numDoors=v.numDoors;
return v;
}
//2
Vehicle operator==(const Vehicle & v)
{
if(this->numWheels==v.numWheels &&
this->numDoors==v.numDoors)
{
return 1;
}
else
{
return 0;
}
}
//3
Vehicle operator!=(const Vehicle & v)
{
if(this->numWheels!=v.numWheels &&
this->numDoors!=v.numDoors)
{
return 1;
}
else
{
return 0;
}
}
//4.a
Vehicle operator++()
{
Vehicle v;
v.numWheels =
++numWheels;
v.numDoors =
++numDoors;
return v;
}
//4.b
Vehicle operator++(int)
{
Vehicle v;
v.numWheels =
numWheels++;
v.numDoors =
numDoors++;
return v;
}
//4.c
Vehicle operator--()
{
Vehicle v;
v.numWheels =
--numWheels;
v.numDoors =
--numDoors;
return v;
}
//4.d
Vehicle operator--(int)
{
Vehicle v;
v.numWheels =
numWheels--;
v.numDoors =
numDoors--;
return v;
}
friend ostream& operator<<(
ostream& out, const Vehicle &v) ;
};
ostream& operator<<( ostream& out, const Vehicle
&v)
{
out<<"numWheels is :
"<<v.numWheels;
out<<"numDoors is :
"<<v.numDoors;
}
int main()
{
Vehicle original(2,4);
Vehicle copy(original); // copy constructor by reference
cout << "Original is: " << original << " copy is:
" << copy << endl;
cout << "Increment original: " << original++ <<
endl;
cout << "Increment copy:" << ++copy<< endl;
cout << "Decrement original:" << --original <<
endl;
cout << "Decrement copy:" << copy-- <<
endl;
// should be true :
cout << "Compare equality 1: " << (original == copy)
<< endl;
//should be false:
cout << "Compare equality 2: " << (--original ==
++copy) << endl;
//should be true:
cout << "Compare inequality: " << (original != copy)
<< endl;
//This should make original = copy, and then return a Vehicle for
output:
cout << "Assignment operator: " << (original = copy)
<< endl;
return 0;
}