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

C++ classes/ calculating distance formula Main.cpp : So we will define our class

ID: 663728 • Letter: C

Question

C++ classes/ calculating distance formula

Main.cpp :

So we will define our class in Point.h and define our class functions in Point.cpp

- write a class that includes the x, y, and z coordinates, then write the declaration of these functions.

-a default constructor called Point() that DOES NOT take in any arguments and just resets all coordinates to 0.0

-setCoordinates() that takes in 3 variables to set the coordinates to.

-calcDistance() that has no arguments, but returns a double (distance from origin 0,0,0)

-calcDistance() that DOES take in an argument, a Point object, and returns a double (distance) between parameter point and itself.

input.txt contains :

1 2 3 4 5 6

7 8 9 0 1 2

9 8 7 1 2 3

11 12 13 14 15 16

99 99 99 99 99 99

-output should look like:

Distances from origin:

3.74166 8.77496

13.9284 2.23607

13.9284 3.74166

20.8327 26.0192

171.473 171.473

Distances from each other:

5.19615

12.1244

10.7703

5.19615

0

#include #include #include using namespace std; // Forward declaration for calcDistance function double calcDistance(double xl, double yl, double z1, double x2, double y2, double z2); int main() // File input/output variables ifstream fin("input.txt"); ofstream fout("output.txt"); /1 If we can't find the input file, quit with error message. if(!fin) cout

Explanation / Answer

lab3.cpp

#include <cmath>
#include <iostream>
class Point
#include "Point.h"

using namespace std;

/*// Forward declaration for calcDistance function
double calcDistance(double x1, double y1, double z1, double x2, double y2, double z2);*/


int main()
{
    // File input/output variables
ifstream fin("input.txt");
ofstream fout("output.txt");

    // If we can't find the input file, quit with error message.
if(!fin)
{
  cout << "Input file not opened!" << endl;
  exit(1);
}


//Declare array to store the numbers from the Input file
Point myPoint1[20];
Point myPoint2[20];

// Declare a integer to increment though the arrays
int i = 0;

//Declare values to accept the user input
double x1;
double y1;
double z1;
double x2;
double y2;
double z2;

do          
{
  //Get the first six values from the file
  fin >> x1 >> y1 >> z1 >> x2 >> y2 >> z2;

  //Assign values to x,y,z
  myPoint1[i].setCoordinates(x1,y1,z1);
  myPoint2[i].setCoordinates(x2,y2,z2);
     
  //Calculate the Distance on the myPoint1 and myPoint2
  double dist = myPoint1[i].calcDistance(myPoint2[i]);
  fout << dist << endl; //Write to the Output file the distance for the first six numbers
  i = i + 1;    //Move to the next line of six numbers

} while(fin.good());  //Run as long as the Input file still has values

  cout << "You can check the output file now!" << endl; //Let the user know the process is complete
  system("pause");  //Give the user a chance to hit enter
  

  
  return 0;

  /*cout << "Input file not opened!" << endl;
  exit(1);*/


    // Close our files to ensure we save our data
fin.close();
fout.close();

    // End program
return 0;
}


Point.cpp

#include "Point.h"
#include <iostream>

// Define your class's functions here
Point::Point()  //Constructor
{
//initialize the variables to zero
x = 0;
y = 0;
z = 0;

}

void Point::setCoordinates(double a, double b, double c )
{
//Set the values for x, y and z
x = a;
y = b;
z = c;
}


double Point::calcDistance()
{

//Use the Distance formula to calculate the distance of x,y,z from zero
double result = ((x-0)*(x-0))+((y-0)*(y-0))+((z-0)*(z-0));
double distance = sqrt(result); //Take the square root of the previous line

return distance;   //Return the result to the main() function


}

double Point::calcDistance(Point myPoint)
{

//Use the Distance formula to calculate the distance of x,y,z set of values
double result = ((x-myPoint.x)*(x-myPoint.x))+((y-myPoint.y)*(y-myPoint.y))+((z-myPoint.z)*(z-myPoint.z));
double distance = sqrt(result); //Take the square root of the previous line

return distance;    //Return the result to the main() function
}

Point.h

#ifndef POINT_H
#define POINT_H


// Define your class here

class Point
{
private:
  double x, y,z;  //Private variables


public:     //Public functions
  Point();   //Constructor
  void setCoordinates(double, double, double);
  double calcDistance();
  double calcDistance(Point);
  
};


#endif

input.txt
1 2 3 4 5 6
7 8 9 0 1 2
9 8 7 1 2 3
11 12 13 14 15 16
99 99 99 99 99 99

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