Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Question 1 : Suppose we would like to manipulate points in a 2D space. It is nat

ID: 3550446 • Letter: Q

Question

Question 1 :
Suppose we would like to manipulate points in a 2D space. It is natural for
us to define a class for this purpose. Let's call this class twoD. The
following is the definition for this class:



class twoD // base class
{
protected:
double x, y; // x and y coordinates
public:
// inline implementation of constructor
twoD(double i, double j) {x = i, y =j}
// inline implementation of member functions
void setX(double NewX){x = NewX;}
void setY(double NewY){y = NewY;}
double getX() const {return x;}
double getY() const {return y;}
};


Suppose later on we decide to implement a class to deal with points in a
3D place. An intuitive way of implementing this threeD class would be:


// definition of threeD without using inheritance
class threeD
{


protected:
double x, y, z; // x, y, z oordinates


public:
// inline implementation of constructor
threeD(double i, double j, double k) {x=i, y=j, z=k}


// inline implementation of member functions
void setX(double NewX){x = NewX;}
void setY(double NewY){y = NewY;}
void setZ(double NewZ){z = NewZ;}
double getX() const {return x;}
double getY() const {return y;}
double getZ() const {return z;}
};


The code marked with red color shows the differences between the
definition of the two classes. Actually, the only differences are that
the threeD class has an additional member variable z and two additional
member functions: setZ() and getZ() to set and print out the values of
the z axis. In other words, the definition of the threeD class adds little new
code to the definition of the twoD class.


In the definition of the threeD class, we have written some repetitive code
and that's not wise. This can be fixed by using inheritance.Hence, please
re-write the code and give the definition for threeD class using
inheritance.



Hint: the definition for threeD using inheritance should be like
class threeD:public twoD{
}

Explanation / Answer

class twoD // base class
{
protected:
double x, y; // x and y coordinates
public:
// inline implementation of constructor
twoD(double i, double j) {x = i, y =j}
// inline implementation of member functions
void setX(double NewX){x = NewX;}
void setY(double NewY){y = NewY;}
double getX() const {return x;}
double getY() const {return y;}
};

//definition of threeD with using inheritance
class threeD : public twoD
{
protected:
double z; // x, y, z oordinates
public:
// inline implementation of constructor
threeD(double i, double j, double k):twoD(i,j) {z=k;}
// inline implementation of member functions
void setZ(double NewZ){z = NewZ;}
double getZ() const {return z;}
};

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote