7 Question 7 . We know that it is possible to write assignments of int to double
ID: 3918255 • Letter: 7
Question
7 Question 7 . We know that it is possible to write assignments of int to double and vice-versa. . We also know that the sums int + double and double + int both return double. int i-1, j-2 ; double x*3.3, x = i; y-4.4; // double int // int -double // int double is double /l double int is double y: . You are given the forward declarations of two classes PointDbl and PointInt. . State which methods in the classes below are accessors . State which methods in the classes below are mutators. class PointDbl: class PointInt; class PointDbl ( public: PointDbl () {x=0; y=0; } void set (double a, double b) tx a y b; double getx) const ( return x; double gety) const (return y; h // operator-. // to be written private: double x, y: class PointInt ( public: PointInt) x 0; y 0; void set(int a, int b) x a; y b int getxO const t return x; int gety) const ( return y: // operator- // to be written private: int x, y J:Explanation / Answer
#include <iostream>
using namespace std;
//Class Forward declarations
class PointDbl;
class PointInt;
//class definition PointDbl
class PointDbl{
public:
//Constructor
PointDbl(){ x = 0; y = 0; }
//Mutator member function
void set(double a, double b) { x = a; y = b;}
//Accessors member functions
double getx() const {return x;}
double gety() const {return y;}
//operator=
PointDbl& operator= (const PointInt &rhs)
{
x = rhs.getx();
y = rhs.gety();
}
private:
double x, y;
};
//class definition PointInt
class PointInt{
public:
//Constructor
PointInt() {x = 0; y = 0;}
//Mutator member function
void set(int a, int b) {x = a; y = b;}
//Accessors member functions
int getx() const {return x;}
int gety() const {return y;}
//operator=
PointInt& operator= (const PointDbl &rhs)
{
x = rhs.getx();
y = rhs.gety();
}
private:
int x, y;
};
//operator+
PointDbl operator+(PointDbl pD, PointInt pI){
PointDbl temp;
double x = pD.getx() + pI.getx();
double y = pD.gety() + pI.gety();
temp.set(x, y);
return temp;
}
PointDbl operator+(PointInt pI, PointDbl pD){
PointDbl temp;
double x = pD.getx() + pI.getx();
double y = pD.gety() + pI.gety();
temp.set(x, y);
return temp;
}
int main()
{
PointDbl dp, pdbl;
PointInt ip, pint;
pdbl.set(1.1, 2.2);
pint.set(3, 4);
dp = pint;
ip = pdbl;
PointDbl s1 = pdbl + pint;
PointDbl s2 = pint + pdbl;
cout<<"dp "<<dp.getx()<<" "<<dp.gety()<<endl;
cout<<"ip "<<ip.getx()<<" "<<ip.gety()<<endl;
cout<<"s1 "<<s1.getx()<<" "<<s1.gety()<<endl;
cout<<"s2 "<<s2.getx()<<" "<<s2.gety()<<endl;
return 0;
}
Output:
dp 3 4
ip 1 2
s1 4.1 6.2
s2 4.1 6.2
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.