I want to put what I have in my main into the function plot(), which is in the Graph class, and then call it in my main to display it, so that my main does not have all the lines of code that it has in the example below. I also created a .h file and .cpp file for both classes. I just named it Point. Then I created a main.cpp file which I will sue to call plot() to display the output. The function plot() is supposed to display what I have in my main in the example below.
#pragma once
class Point
{
public:
Point(int x, int y);
void setX(int x);
void setY(int y);
int getX();
int getY();
double distanceFromOrigin();
private:
int m_x;
int m_y;
};
class Graph
{
public:
Graph();
void plot();
};
This is what is inside my Point.cpp (which includes both classes):
#include "Point.h"
#include <iomanip>
#include <cmath>
#include <random>
#include <ctime>
#include <algorithm>
#include <vector>
#include <iostream>
Point::Point(int x = 0, int y = 0) : m_x(x), m_y(y) // default contructor
{
}
void Point::setX(int x) // sets x value
{
m_x = x;
}
void Point::setY(int y) // sets y value
{
m_y = y;
}
int Point::getX() // returns x value
{
return m_x;
}
int Point::getY() // returns y value
{
return m_y;
}
double Point::distanceFromOrigin() // returns distance from origin
{
return sqrt(pow(m_x, 2) + pow(m_y, 2));
}
// start of second class below
Graph::Graph()
{
}
// this displays coordinates and distance from origin values
void Graph::plot()
{
constint SIZE = 25;
//Point *myPointer;
//myPointer = new Point();
std::default_random_engine engine{ static_cast<unsignedint>(std::time(0)) };
std::uniform_int_distribution<int> randomIntforXorY{ -25, 25 };
std::cout << "Graph " << std::endl;
std::cout << "----------------------------------------------- " << std::endl;
std::cout << std::fixed << std::showpoint << std::setprecision(2);
for (size_t i = 0; i < SIZE; i++)
{
Point myPointer = randomIntforXorY(engine);
myPointer = Point(randomIntforXorY(engine), randomIntforXorY(engine));
std::cout << std::setw(4) << "x: " << std::setw(4) << myPointer.getX();
std::cout << std::setw(4) << " y: " << std::setw(4) << myPointer.getY();
std::cout << std::setw(25) << " Distance from Origin: " << std::setw(4) << myPointer.distanceFromOrigin() << std::endl;
}
std::cout << "----------------------------------------------- " << std::endl;
std::cout << std::endl;
}
And this is my main.cpp right now (my driver code):
1 2 3 4 5 6 7 8 9
#include <iostream>
#include "Point.h"
usingnamespace std;
int main()
{
}
It is empty. So first step is to declare Graph. I will just call it "Graph man". Then I need to access the function in the class Graph, which contains stuff from the class Point.
1 2 3 4 5 6
int main()
{
Graph man; // declaring
man.plot(); // calling
cout << man.plot() << endl; // trying to display the call, but errors. it highlights the "<<" after "cout" and says, "no operator '<<' matches these operands. operand types are std::ostream void".
}