Using c++ (fix the code ) and please provide screenshot of the output as prove I
ID: 3607737 • Letter: U
Question
Using c++ (fix the code ) and please provide screenshot of the output as prove
I getting the wrong output and the right output is :
box: (1,1)
box: (1,1)
box: (2,3)
box: (2,1)
box: (1,2)
box: (3,3)
box: (1,2)
box: (2,1)
______________________
main.cpp:
#include
#include "box.hpp"
using namespace std;
int main() {
box a(2,3);
box b;
cout<< b;//output ="box(1,1)"
box c;
cout << c; //output ="box(1,1)"
box d,x,y;
x = b++;
y = ++c;
d = b + c;
cout << a; //output = "box:(2,3)"
cout << b; //output = "box:(2,1)"
cout << c; //output = "box:(1,2)"
cout << d; //output = "box:(3,3)"
cout << x; //output = "box:(1,3)"
cout << y; //output = "box:(2,1)"
return 0;
}
______________________
box.hpp:
#ifndef box_hpp
#define box_hpp
#include
#include
class box {
private :
int width , height;
public:
box();
box(int w, int h);
box operator = (box);
box operator++(int);
box operator++();
box operator+(box);
friend std::ostream & operator<<(std::ostream&, box);
};
#endif /* box_hpp */
______________________
box.cpp:
#include "box.hpp"
#include
box::box()
{
width =1;
height =1;
};
box::box(int w, int h)
{
width=w;
height=h;
};
box box::operator++(int x)
{
width+=1;
return (*this);
};
box box::operator++()
{
height+=1;
return (*this);
};
box box::operator=(box x)
{
box temp(1,1);
temp.width= x.height;
temp.height=x.width;
return temp;
};
box box::operator+(box y)
{
box temp(*this);
temp.width=temp.width + y.width;
temp.height=temp.height + y.height;
return temp;
};
std::ostream& operator<<(std::ostream& out, const box D)
{
out <<std::string("box: (")<< D.width << std::string(",")<<D.height << std::string(")")<<std::endl;
return out;
}
Explanation / Answer
You were doing a mistake in assignment operator. Here is your corrected box.cpp.
main.cpp & box.hpp are unchanged.
/************************/
/*
* box.cpp
*
* Created on: 02-Nov-2017
* Author:
*/
#include "box.hpp"
box::box()
{
width =1;
height =1;
};
box::box(int w, int h)
{
width=w;
height=h;
};
box box::operator++(int x)
{
width+=1;
return (*this);
};
box box::operator++()
{
height+=1;
return (*this);
};
box box::operator=( box x)
{
this->width= x.height;
this->height=x.width;
return *this;
};
box box::operator+( box y)
{
box temp=*this;
temp.width += y.width;
temp.height += y.height;
return temp;
};
std::ostream& operator<<(std::ostream& out, const box D)
{
out <<std::string("box: (")<< D.width << std::string(",")<<D.height << std::string(")")<<std::endl;
return out;
}
/********************/ here is your output
box: (1,1)
box: (1,1)
box: (2,3)
box: (2,1)
box: (1,2)
box: (3,3)
box: (1,2)
box: (2,1)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.