Trying to display output from functions inside a class.

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
#include <iomanip>
#include <cmath>
#include <random>
#include <ctime>
#include <algorithm>

using namespace std; // REAL VERSION

class Graph
{
public:
	Graph()
	{

	}

	void plot() // displays coordinates and distance from origin values
	{
		
	}

};

class Point
{
public:
	Point(int x = 0, int y = 0) : m_x(x), m_y(y) {}

	void setX(int x)
	{
		m_x = x;
	}

	void setY(int y)
	{
		m_y = y;
	}

	int getX()
	{
		return m_x;
	}

	int getY()
	{
		return m_y;
	}

	double distanceFromOrigin()
	{
		return sqrt(pow(m_x, 2) + pow(m_y, 2));
	}

private:
	int m_x;
	int m_y;
};

int main()
{
	const int SIZE = 15;
	Point *myPointer;
	myPointer = new Point();

	std::default_random_engine engine{ static_cast<unsigned int>(std::time(0)) };
	std::uniform_int_distribution<int> randomIntforXorY{ -15, 15 };

	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;

	delete myPointer;
	return 0;
}

So the output for the code above looks something like this:
[code]
Graph 
 x:    2  y:  -10   Distance from Origin: 10.20
 x:  -13  y:   10   Distance from Origin: 16.40
 x:    4  y:    8   Distance from Origin: 8.94
 x:   15  y:   -2   Distance from Origin: 15.13
 x:    5  y:   11   Distance from Origin: 12.08
 x:   13  y:   -6   Distance from Origin: 14.32
 x:   -5  y:   10   Distance from Origin: 11.18
 x:    4  y:  -10   Distance from Origin: 10.77
 x:   -5  y:   15   Distance from Origin: 15.81
 x:   -5  y:   11   Distance from Origin: 12.08
 x:    5  y:   -8   Distance from Origin: 9.43
 x:   13  y:   -6   Distance from Origin: 14.32
 x:    5  y:    6   Distance from Origin: 7.81
 x:   12  y:    8   Distance from Origin: 14.42
 x:  -12  y:    2   Distance from Origin: 12.17
----------------------------------------------- 


I want to make the function plot() display that instead of putting it all in my main.

This is what is inside my header (which includes both classes):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#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):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#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()
{
	const int SIZE = 25;
	//Point *myPointer;
	//myPointer = new Point();

	std::default_random_engine engine{ static_cast<unsigned int>(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"

using namespace 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".
}
Last edited on
Hi, plot() is a void and returns nothing. You do not have to cout it, that's most likely the error. Just call the function.
 
man.plot();

Like you did in line 4. Which is all you need. If you do not see the display then something else is the issue.
Lol. That helped. Thanks.
Topic archived. No new replies allowed.