0 like 0 dislike
4.2k views

Suppose we have the following two points in a 3D space:

The distance between point1 and point2 is:

Please create ThreeD class inherited from Point class which is used in previous exercise and then use ThreeD class to calculate the distance between two 3D points.

Hint:

  • Add two more public functions to Point class to get the value of X and Y
  • ThreeD initialize: 
  • class ThreeD:public Point
  • The following constructor of ThreeD class reuses the constructor of the Point class and the only way values can be passed to the Point constructor is via the use of a member initialization list.
  • ThreeD(double i, double j, double k):Point(i,j){z = k;}

     

Example input:

1 1 1
2 3 4

Example output:

Distance is: 3.74166
[Exercise] Coding (C) - asked in C++
ID: 24589 - Available when: Unlimited - Due to: Unlimited

edited by | 4.2k views
0 0
Called for Help

23 Answers

0 like 0 dislike
Hidden content!
#include * ** * ** * * ** ** * ** * * ** std::cout

#include ** * **

using namespace std;



class Point {
** * * * * * * ****** *
* **** ** * * **** * * *** * ** operation 1. a constructor with given x and y coordinates
* * * * **** ** * *** ** ** *** *** * xpos, double *** *****
* *** * **** * * * *** * ** ** getX(){return x;};
****** * * ******** ** ***** **** * * getY(){return y;};
** ** *** ***
********** * * * * **** *** **** * x;
** * * ** ** **** * *** * ** y;

};







class ThreeD:public Point

{
** *** * * ** ** ***** * *
**** **** *** * * * * * * * * **** * ** * * i, double j, double k):Point(i,j){z = k;}
* * ** * *** * **** ***** * * * * ******** *** * ** getZ(){return z;};
* ** ***** * ** ** *** ***** *
** ****** **** ** * * ** * * * ** ***** *** * * * z;

};



int main()

{
* * *** * * * * ******* ax,ay,az;
* * * * *** * * ** ** * * * * *** *** * * * *
* * * ***** *** A(ax,ay,az);


** *** * *** ********** ** * * * *** * *** ***
* **** *** *** ** ** B(ax,ay,az);


* * * ** * *** *** * ** dis;
* * * * **** *** *** ** * * ****** ** **** * ** * ** * *** * ** ***** * *** ** *** **
** *** * * * ** * ** * *** * ** *** is: **** ** ** * ****



}
answered by (-412 points)
0 like 0 dislike
Hidden content!
**** * ** * *
* * * * * ** ******
**** * * ** * * **
** * * * ** * **



using namespace std;



class Point {
* ***** ***** * *** * *
** * * * ** ** getx();
*** ** * *** * ***** * gety();
**** ** ** ** * * ** * ***** * * * ****** xpos, double ypos);

*** * ** * ** ** * ** *
** * *** *** ** *** * * * * *** x;
* * * * * * **** *** * * *** y;

};


** ** *** xpos, double ypos)

{
* ** * ** *** * **** *** *
*** *** * ****** **

}



double Point::getx()

{
** **** ** ** ** *** * x;

}



double Point::gety()

{
*** ** * * * ******* *** ** y;

}



class ThreeD:public Point

{
* * * * ** ** * *
* ***** ** ** * ** * ** * ***** * ** ** **** * ** getz();
* ** * *** * * * **** ** * ** * * ******* i, double j, double k):Point(i,j){z = k;}
** ***** * *** * *** **
* * * * * *** ** ** * * * ********** * *** * * * z;

};



double ThreeD::getz()

{
** * ** **** *** *** * ** z;

}







int main()

{
** ***** *** * ******* x1,x2,x3,distance;
* * * * *** ** **** ** *** ** * * *** ****
* * **** * * ** **** ** A(x1,x2,x3);
** ** ** * ***** *** **** *** * ** *** ** * *
** *** *** ** ** ** ***** B(x1,x2,x3);
** * ** * * *** * ** ** ******** * * **** * * ** ** * ** * ** * * * * *
**** * * *** ** * ***** * * * * ****** * ** * is: * *** * *** *** *

}
answered by (-264 points)
0 like 0 dislike
Hidden content!
**** ****** * *

#include <math.h>

using namespace std;



int main(void)

{
** ** * * ** * **** a1,a2,a3,b1,b2,b3,c1,c2,c3,dis;
* * ** *** ** ** * * * * * * * * * * *** * * * ** *


**** *** * ** * ** * ** * *** ** * *** * *** * * **


*** **** * **** * * * *** * ** * * ** * * * * * * **** * ***** * ** * * ** *** **


*** **** * ** *** **** ** *
** ** * * *** * * ** * **
** ** **** * * * * ** *** ******


** * ** ** ******* * *** * *


* * ***** *** * * ** ** * * **** * **** *** is: "<<dis;

}
answered by (-324 points)
0 like 0 dislike
Hidden content!
#include ** * * * **
* * ** * *
** *** *



using ** * std;



class Point {
* *** *** * * * **
***** * * *** * * * ** * ** * * ** ** 1. a ** *** * with given x and y * **
** *** * * * * * ** * ** * * * xpos, double ypos);
* * * ****** *** *** * *** * * * * * 2. get a string * ** * *
* * * * * ** * ** * * ** ** ** * *
** *** **** * *** ** * * * *** ** ** 3. check if two points have the same location
* * * *** **** * ** * * ***** * ** * * * other);
* * ** ** ***** * ** *** * * **** * operation 4. *** the point by swapping its * ** *
** **** ** ** *** * * ** ** * ***** *** **
** ** *** ** * ** * *** * ***** getX();
****** * * ***** ****** ** * * ** ** ** getY();
***** * * *** * **** **
***** * * * * ** * * * *** * x;
* * * * * *** * * ** ** ** *** y;

};


* * * *** xpos, double ypos)

{
***** * ** ** * * *** * = xpos;
** ** *** ** * * ** = ypos;

}



class ThreeD : public Point

{

public:
* * * *** * * ** * **** i, double j, double * * = i; y = j; z = k;}
* * * ** * *** *** ** other);


*
* ***** ** ** ** * * * ** x;
****** ** * *** ***** ***** ** y;
* *** * *** * * * * * * z;

};



void *** *** * **** other)

{
* * * * * * * * calc;
** *** ** * * ** ** * = * * * ** ** * *** ***** * ** **
* * * ** *** * * * ********* * **** ** *** ** ** is: ** *** ** ** *

}



int main()

{
** ** *** * * **** ****** * **


* *** * * * * ** **** ** * ** * * **** *** *** * ******* * * ***** * * **


**** ** *** ThreeD * **
*** ** ThreeD ** *


**** ** ** ** **


*** * ** return 0;



}
answered by (-189 points)
0 like 0 dislike
Hidden content!
* ** ** ** ** *
* * *** ****** * **
** ** ** * ** *

using namespace std;



class Point {
* * ** * ***** **
* *** * **** **** * **** * ** ** * **** * * ** getx();
** ** ** *** ** * * * ********* * *** * gety();


* * * * * ** ** * * * ** * ***** ** ** * * * *** xpos, double ypos);


* *** * * ** ****
*** ******* * * * * * *** * * *** *** x;
* ** ******** **** ** ** * * * *** y;

};


*** * xpos, double ypos)

{
***** * ** ** **** *** = xpos;
*** ** * * * *** ** * ** = ypos;

}



int Point::getx()

{
*** * **** * *** * ***** * ***** x;

}



int Point::gety()

{
* *** ** * ** * ***** ** y;

}



class ThreeD:public Point

{

public:
**** * * * ** * * i, double j, double * * * * = k;}
*** * *** * * ** * *** getz();

private:
* * ** *** * ** * * z;

};



int ThreeD::getz()

{
**** *** ** * ** * z;

}



int main()

{
* * * ** *** **** ** * * * * *
* ***** * ****** * * ** * * * * ****** * *
****** **** * **** * **** *** A(x_,y_,z_);
*** ** * * **** * ** *** * *** ** ** ** **** *****
******* *** *** * ** ** * B(x_,y_,z_);
*** ** ** ** * ** ***** * = * *** * * * * * **** * ** ***** *** * *
** * *** * * *** * * *** * * ** * * * * ** * ** ** * * * *** *** ** *****
* ** * *** ***** ****** 0;

}
answered by (-367 points)
0 like 0 dislike
Hidden content!
#include ****** ** *

#include * **** * *

#include * *******

#include **** ** **

using namespace std;



class Point {
* ** * * ** ** * ***
* ** * * *** * * *** ** * * ** ***** * * * set_xyz();
**** * ****** ** * *** * * * * operation 2. get a string * ****
** ** * * ** ***** ** **** to_string();
**** **** * * ** * * *** * * * operation 3. check if two points have the same location
**** * ** *** * ** ** * * **** x_double();
** * * ******* *** ***** * * *** ** y_double();
** * ** * **** * **** ** ** * ** * z_double();
* ** ** ** *** *
* * ** ** * * ** *** ** * x;
* * * * * ** * * ** * * ***** * y;
** **** * ***** ** * * * **** * ** ** z;

};



string * ****

{
* * ***** * ** **** * output;
***** ** * * ** *** * * *** * x_str, y_str;
** **** * * * * * * * ** x;
* ** * *** ** ** ** ** * * * *** ** * y;
**** ** * ** ** * ** **** **** * ***** ** * * *** * ** * ** ** ***** *****
*** * ** * * *** * *** output;

}



double *** * A, Point B)

{
* ** ****** * *** *** *** result=0;
* ** ******* ** ** ** ** * += ** * *** ** **
** * * * * * * * ** **** += ** *** *** * * *
* * * * ********* * += * *** * ** ** * *
*** ** **** * *** * * * = sqrt(result);
** * ** * * ** * result;

}



void * * ** *

{
*** * * *** * * * *** ** * ** x **** ** * y * * * z;

}



double * **** * *

{
* ** * *** * * * * * * * * ** x;

}



double *** * ***

{
**** ** * * ** ** * ** y;

}



double ** ***

{
** * * ** ** * *** * ** ** ** * z;

}



int main(){
* ** ****** * * * * Point_A, Point_B;


* * * * ** * ** ** * *
**** * * * * ** * ** *
* **** ********** * *** ** * **** * * * * * * * is: " ******* * * * * ** * * Point_B);

}
answered by (-581 points)
0 like 0 dislike
Hidden content!
#include<iostream>

#include<string>

#include<sstream>

#include<math.h>

using namespace std;

class Point {

   public:
* **** * ** ** **** * ** ** *** * ** operation 1. a constructor with given x and y coordinates
** * * * *** * ***** * ** ** * *** * xpos, double ypos, double zpos)
* *** *** * * * ** * * *** ** ** **** * * * **
* * * * ** ******** *** * * *** ** ** * ***** ** ** ** * * * * * **** ** * *** * ** * ** **** * * = xpos;
* ** ** ** * * ***** * **** * ** * ******* *** ***** * * * ** ** **** * * * * * * * * = ypos;
* *** ** * *** * ** ** * ** *** ** * * ** * * ** ** * *** **** *** * ** *** * * ** ** * ** * * = zpos;
** *** * ** ** ** * ** * **
** * * * ** ** ** ** ** ** ** operation 2. get a string representation
****** * ** **** *** *** * ** ** to_string()
********* * ** ** * * * * * * **
* ***** **** * **** *** *** ** ********* ** re;
****** ******* * * *** * **** ** * *** ** * *** ** * ** ** * ** * * ** ** ******** ** * * ** *** * *** * * *
*** * * ** **** * * * * ** ** ** ** * * re.str();
** *** * * * * * *** * * * * ***
* * * * ***** * * * * ***** * ** ** operation 3. check if two points have the same location
** * *** * * *** ** ** * * *** ** * equal_to(Point other)
* ** * * * * **** ** ** * * * ** * *
*** **** * ** ** ** * *** **** * * ** **** ** * * * ** *** * *** * **
* **** ** **** *** * * ** *** ** * ** * * *** ** ** ** * ****** ** * * *** * true;
** **** * ** * **** * *** * *** ** ** ** **** *** ****
* ** * * *** * ** *** ***** * * ***** * ** * * * * * ** ** * *** * false;
** * ** * ** * *** * * **
* * * * *** ***** * * ***** * *** operation 4. transform the point by swapping its coordinates
* *** ****** * * **** ***** * * **** *** reflect(Point other)
* * * * * *** **** * ** ***
****** * * * * * ** * * * ***** ** * * * ** ** * ** * **** dis=0;
*** * * ** * *** ** *** **** ** ** ** * **** * *** * *** **** * * ** * ** * * **** * * **** * *** * **** ** * *** *** *
* ** ** ** * *** * **** * *** * ** ****** ***** *** ** *
** * * * ** * ** ** * * **** *** * *** ******* * **** ** *** * * * is: "<<dis;
* ** * * ** * * * ***** * *



   private:
** * * *** * * ** **** ***** * x;
** ** ** ** * ** *** *** ** * * y;
*** ** * ** * * * ** * ** **** ** z;

};



int main(){
*** * **** *** ** ***** ax,ay,az,bx,by,bz;
* ** * ***** * ** * * ** * * ** ** * ** ** *** ***** * * *** **** *** ******* *** * *****
** *** *** ** ** * ** * A(ax,ay,az);

    Point B(bx,by,bz);
** * ** * **** *** **** * * *
* * * * ** *** * ****** ****** 0;

}
answered by (-249 points)
0 like 0 dislike
Hidden content!
#include * *** ** ***
* * * * ** **** *

#include ** * * *** **

#include * * * *



using * * std;



class Point {
** **** ** *** * * *
** ***** * * * ***** * * * *** xpos, double ypos, double zpos);
** **** ** *** *** ** *** *** * * dis(Point b);


* ** * ** * * *
* **** * * *** * * * * * * x;
** * * * * *** * *** * ** * * ** ** y;
*** * * ***** ** ********* * * * * ** z;

};


* * ** ** ** xpos, double ypos, double zpos)

{
*** * ** * ** *** = xpos;
* ***** * ** ** ** * = ypos;
* ** * * ** **** ** ** = zpos;

}



double * ** b)

{
** ** * * * ** ** * ** * * * ** * * * **** * * * 2));

}

int main(){
* ** * ** *** *** **** x, y, z;
* **** ** * *** * ** ** ** ** ** x * * *** * y ** * z;
* * ** * * * * * ** * A(x, y, z);
*** * ** ** ** * * *** *** x * * * y ** * z;
* ** * *** * * * ** B(x, y, z);
** ***** * * * * ** * ***** * * * **** is: *** ******** * * ** ** * **
* * ** * * * ***** * * * * 0;

}
answered by (-364 points)
0 like 0 dislike
Hidden content!
* *** * ****

#include<cmath>

using namespace std;

class Point {
* * * ** *** **
* *** ** * * *** *** ** *** *** xpos, double ypos);
** *** * ** * ** *** * ** *** * getx();
**** *** ** ** * ** * * * * * gety();
**** **** **** * ** *** **
* * * ** ** * *** * * *** ***** ** x;
***** * ** * * ** ** * * * * *** y;

};

Point::Point(double xpos, double ypos){
* ** * ** * ***** * ** ***
** * * * * * **** **** ** *

}

class ThreeD:public Point

{
*** *** ** ** * ** **** *
** **** ** ** ** * * * * **** ** * **** ** ** *** i, double j, double k):Point(i,j){z = k;}
** ** *** * ** *** * * **** *** **** ** * ** * getz();
* ** ** * * **** * *** * * *
** ** ** ** * * * ** **** *** ** * z;

};

double Point::getx(){
** **** * *** * * *** * ** x;

}

double Point::gety(){
** *** *** * ****** * * * * ** y;

}

double ThreeD::getz(){
* ** * * ** ***** ** z;

}



int main(){
* * * * ** ** * ***** * ** ax,ay,az;
* * * ** * ** ** ** * * dis;
* ** * **** * ** * * * *** ** * * * ** ** * ** *** ***
****** ** * * ****** ** A(ax,ay,az);
*** ** ** **** ** ** **** * ** **** * **** * * * *
* * ***** * ** **** ** ** B(ax,ay,az);
* ********** **** **** * * *** ** ** *** * * ** ** ** * * * ** * ** ** *
* * ** * * * * * * * * ******* * * * is: * * ** * * ***
** ** * *** * * * * *****
* ** ** *** ***** *******
*** *** * * * * **
** * *** * ** * * *** 0;

}
answered by (-384 points)
0 like 0 dislike
Hidden content!
#include * *** ***** ***
** * ** * * *
* * **** * ** * * ** *



using * ** std;



class Point{
*** * * *** *
******** * ** ***** ** * * xpos, double * ****** y(ypos)
*** ** * ** ** **
**** * ** * * ** * **
****** * ****** *** *** getX();
* ** * * **** * **** getY();
*** **** * *** * **
* *** ******* * *** *** * ** x;
* ** *** * **** y;

};



double * * *

{
** ** * ** * * ******* ***** ** x;

}



double * ** **

{
* * * * * * * ** * y;

}



class ** * * * Point{
* ** * * * *** **
** ** * ********* * * **** * xpos, double ypos, double * *** * ** * ypos)
* ** * *** *** * **
*** * * ** ** * ** ***** * * *** *** ** * = zpos;
* ** * ** ** **
* *** ** **** *** * ** getZ();
* ** ** *
*** *** * * * *** * * z;

};



double * *

{
* **** * * ** ** z;

}



int main()

{
* * ******* ****** ax, ay, az, bx, by, bz;
* * * *** ** * ** *** **** *** ax * ** * ay ***** * * * az bx ** ** * *** by ** ** ** bz;


* ** ********** * A(ax, ay, az);
** * ** * **** ** * * *** B(bx, by, bz);


* * *** ** ** * ***** dist;
* * * * * ***** * **** = * * ** ** ***** 2.0) + * * * - * * * * 2.0) + ** - ** ** 2.0));


* * ****** **** ** **** * * * * * * * *** ** is: *** * * ** dist ** * endl;


*** * * * * ** * * * 0;

}
answered by (5.2k points)
Welcome to Peer-Interaction Programming Learning System (PIPLS) LTLab, National DongHwa University
English 中文 Tiếng Việt
IP:172.70.126.16
©2016-2025

Related questions

0 like 0 dislike
26 answers
[Exercise] Coding (C) - asked May 11, 2017 in C++
ID: 24587 - Available when: Unlimited - Due to: Unlimited
| 4.3k views
0 like 0 dislike
20 answers
[Exercise] Essay (Open question) - asked May 4, 2017 in C++
ID: 24536 - Available when: Unlimited - Due to: Unlimited
| 3.8k views
12,783 questions
183,442 answers
172,219 comments
4,824 users