In: Computer Science
WRITE IN C++
Add to the Coord class
Edit the provided code in C++
Write a member function named
int distance2origin()
that calculates the distance from the (x, y, z) point to the origin (0, 0, 0)
the ‘prototype’ in the class definition will be
int distance2origin()
outside the class definition
int Coord :: distance2origin()
{
// your code
}
_______________________________________________________________________________________________________
/************************************************** * * program name: Coord02 * Author: * date due: 10/19/20 * remarks: * * ***************************************************/ /****************************************** * library includes ******************************************/ #include // needed for cin and cout /****************************************** * pre-processor ******************************************/ #define PI 3.14159 using namespace std; class Coord { private: int xCoord; int yCoord; int zCoord; public: Coord(int xCoord, int yCoord, int zCoord){ this->xCoord=xCoord; this->yCoord=yCoord; this->zCoord=zCoord; } inline void setXCoord(int xCoord){ this->xCoord=xCoord; } inline void setYCoord(int yCoord){ this->yCoord=yCoord; } inline void setZCood(int zCoord){ this->zCoord=zCoord; } inline int getXCoord(){ return this->xCoord; } inline int getYCoord(){ return this-> yCoord; } inline int getZCoord(){ return this->zCoord; } void display(){ cout<xCoord<yCoord<zCoord<
Please Explain Everything in detail. Thank you.
Code:
Output:
Raw code:
#include <iostream>
#include <cmath>
#define PI 3.14159
using namespace std;
class Coord {
private:
int xCoord;
int yCoord;
int zCoord;
public:
// constructor to initialize it's class variables
Coord(int xCoord, int yCoord, int zCoord){
this->xCoord=xCoord;
this->yCoord=yCoord;
this->zCoord=zCoord;
}
// setter methods to set the values of class variables
void setXCoord(int xCoord){
this->xCoord=xCoord;
}
void setYCoord(int yCoord){
this->yCoord=yCoord;
}
void setZCood(int zCoord){
this->zCoord=zCoord;
}
// getter methods to return the values of the class variables
int getXCoord(){
return this->xCoord;
}
int getYCoord(){
return this->yCoord;
}
int getZCoord(){
return this->zCoord;
}
// displaying the point information
void display(){
cout << getXCoord() << getYCoord() << getZCoord();
}
// declaring the method
int distance2origin();
};
int Coord :: distance2origin(){
// finding the distance between a point to the origin using the formula
// p1 = (x1,y1,z1)
// p2 = (x2,y2,z2)
// distance between p1 and p2 = ( (x2-x1)^2 + (y2-y1)^2 + (z2-z1)^2 )^0.5
// Here the p2 is origin that means x2,y2,z2 = 0,0,0
// distance between p1 and origin = ( (0-x1)^2 + (0-y1)^2 + (0-z1)^2 )^0.5
// = ( (-x1)^2 + (-y1)^2 + (-z1)^2 )^0.5
// = ( (x1)^2 + (y1)^2 + (z1)^2 )^0.5
int distance = sqrt( pow(getXCoord(),2) + pow(getYCoord(),2) + pow(getZCoord(),2) );
return (distance);
}
int main(int argc, char const *argv[])
{
Coord c1 = Coord(13,42,5); // creating an object
cout << "Distance between a point (13,42,5) to origin (0,0,0) is : " << c1.distance2origin() << "\n"; // printing the result
return 0;
}
NOTE:
If You Have Any Doubts Feel Free To Comment In The
Comment Section.
Do Vote (LIKE).