Given the following class definition (Pair.hpp), Implement the assignment operat
ID: 3856680 • Letter: G
Question
Given the following class definition (Pair.hpp), Implement the assignment operator that should be in the Pair.cpp:
#ifndef _PAIR_HPP_
#define _PAIR_HPP_
#include <iostream>
#include <cstdlib>
using namespace std;
class Pair {
public:
Pair();
Pair(int n1, int n2);
Pair(const Pair& orig);
~Pair();
Pair& operator=(const Pair&other);
friend ostream& operator<<(ostream&strm, const Pair &p);
private:
int x, y;
};
}
#endif
Explanation / Answer
#include "Pair.hpp"
using namespace std;
pair::Pair()
{ // the empty pair is the default value
}
Pair:: Pair(int n1, int n2)
{
N1 =x; // copy constructor
N2=y;
}
Pair:: Pair(const Pair& orig)
{
x.n1=pair;
pair=y.n2;
}
~Pair()
{
If(pair ==0)
Delete pair;
}
Pair& operator=(const Pair&other)
{ If(pair ==0)
Delete pair;
Return *this;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.