#include <iostream> using namespace std; class Pair { public: Pair(); Pair (int
ID: 3644965 • Letter: #
Question
#include <iostream>using namespace std;
class Pair
{
public:
Pair();
Pair (int first, int second);
Pair (int first);
int accessFirst();
int accessSecond();
friend Pair operator+ (const Pair&, const Pair&);
friend Pair operator* (const Pair&, int);
friend istream& operator>> (istream&, Pair&);
friend ostream& operator<< (ostream&, const Pair&);
private:
int f;
int s;
};
Pair operator+ (const Pair& 1hs, const Pair& rhs)
{
return Pair(1hs.f + rhs.f, 1hs.s + rhs.s);
Pair operator* (const Pair& 1hs, int rhs)
{
return Pair(1hs.f * rhs, 1hs.s * rhs);
}
istream& operator>> (istream& ins, Pair& second)
{
char ch;
ins >> ch;
ins >> second.f;
ins >> ch;
ins >> second.s;
ins >> ch;
return ins;
}
ostream& operator<< (ostream& outs, const Pair& second)
{
outs << '(';
outs << second.f;
outs << ",";
outs << second.s;
outs << ")";
return outs;
}
Pair:: Pair(int firstValue, int secondValue);
f(firstValue), s(secondValue)
{
}
Pair:: Pair(int firstValue) :F(firstValue), s(0)
{
}
Pair:: Pair(): f(0), s(0)
{}
int Pair::accessFirst()
{
return f;
}
int Pair::accessSecond()
{
return s;
}
int main()
{
Pair a(8,5);
Pair b(2,3);
Pair c(4,8);
Pair d,x,y;
cout<<"("<< c.accessFirst()<<","
<< c.accessSecond()<<")"<<endl;
cout<<endl;
cout<<"a" <<a << endl
<< "b" << b <<endl
<< "c" << c
<< endl << endl;
cout << "a + c : "<< a+c << endl;
cout << "b * 2 : "<< b * 2 << endl;
system ("pause");
return 0;
}
Explanation / Answer
Have corrected all the errors and now Program runs fine.... //Corrected Code #include using namespace std; class Pair { public: Pair(); Pair (int first, int second); Pair (int first); int accessFirst(); int accessSecond(); friend Pair operator+ (const Pair&, const Pair&); friend Pair operator* (const Pair&, int); friend istream& operator>> (istream&, Pair&); friend ostream& operator> (istream& ins, Pair& second) { char ch; ins >> ch; ins >> second.f; ins >> ch; ins >> second.s; ins >> ch; return ins; } ostream& operatorRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.